slf4j日志使用
Posted jht_newbie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了slf4j日志使用相关的知识,希望对你有一定的参考价值。
scala中
trait LogSupport { protected val log = LoggerFactory.getLogger(this.getClass) }
需要要到的类 extends LogSupport即可
public class BootTest { public static void main(String args[]){ Logger log = LoggerFactory.getLogger(BootTest.class); log.info("BootTest"); //打印日志 } }
java中非静态的使用:
private Logger log = LoggerFactory.getLogger(this.getClass());//传入的参数this.getClass() 影响打印信息%logger{35} 例如: c.z.b.v.a.l.k.test.LogTest
logback.xml典型配置:
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="60 seconds" debug="true"> <!-- 定义日志文件 输入位置 --> <property name="log_dir" value="./log" /> <!-- 日志最大的历史 7天 --> <property name="maxHistory" value="7"/> <!-- definition of appender STDOUT --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} +[%thread] %-5level - %msg%n</pattern> </encoder> </appender> <appender name="FILE_WARN" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>WARN</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/test.warn.log</fileNamePattern> <maxHistory>${maxHistory}</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/test.info.log</fileNamePattern> <maxHistory>${maxHistory}</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <appender name="FILE_DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>DEBUG</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/test.debug.log</fileNamePattern> <maxHistory>${maxHistory}</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/test.error.log</fileNamePattern> <maxHistory>${maxHistory}</maxHistory> </rollingPolicy> <encoder> <!--%d{yyyy-MM-dd HH:mm:ss.SSS}%-4relative [%thread] %-5level %logger{35} - --> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <root level="INFO"> <!-- appender referenced after it is defined --> <appender-ref ref="STDOUT"/> <appender-ref ref="FILE_ERROR"/> <appender-ref ref="FILE_WARN"/> <appender-ref ref="FILE_INFO"/> <appender-ref ref="FILE_DEBUG"/> </root> </configuration>
以上是关于slf4j日志使用的主要内容,如果未能解决你的问题,请参考以下文章