1.Logback使用
logback 是log4j的替代者,其需要slf4j。
其配置文件主要有以下三种
1. logback.groovy
2. logback-test.xml
3. logback.xml
在加载配置文件的过程汇总,会按照1.2.3的顺序去加载,首先找的groovy文件。
所以如果启动的时候打印了info日志会看到:
11:06:45,631 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy] 11:06:45,631 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml] 11:06:45,631 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/D:/ideaspace/untitled/ran-frontend/target/classes/logback.xml]
可以看出来,首先去classpath下找前两个文件,如果找不到的化,再去找第三个。
其使用没有什么区别:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.core.util.StatusPrinter; public class HelloWorld2 { public static void main(String[] args) { Logger logger = LoggerFactory.getLogger("chapters.introduction.HelloWorld2"); logger.debug("Hello world."); // print internal state, 我们可以监控到其内部的状态 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); StatusPrinter.print(lc); } }
日志的自动加载
<!-- 表示的启动自动扫描, 其中时间单位默认是milliseconds ,默认时间是60秒-->
<configuration scan="true" scanPeriod="30 seconds" >
...
</configuration>
如果scan配置的是true,则会启用ReconfigureOnChangeFilter,此Filter会在任何调用日志记录(任何级别)的时候都会被执行。
由于每次都被执行,因此这个会有比较严重的性能问题,
In order to improve performance, ReconfigureOnChangeFilter is in reality "alive" only once every N logging operations.
Depending on how often your application logs, the value of N can be modified on the fly by logback.
By default N is 16, although it can go as high as 2^16 (= 65536) for CPU-intensive applications.
因此如果配置文件修改了,需要生效的话,必要要达到指定的调用次数而且达到重新加载的时间才会生效。
其中这个次数默认是16次,最高可以配置到2^16次。
logback.xml配置:
1. 简单文件配置
<configuration> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>D:deploylogs anger.log</file> <append>true</append> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread] %logger{35} - %msg%n</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="FILE" /> </root> </configuration>
2. 简单的控制台配置;
<configuration> <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread] %logger{35} - %msg%n</pattern> </encoder > </appender> <root level="INFO"> <appender-ref ref="stdout" /> </root> </configuration>
3. 滚动日志配置:
<configuration scan="true" scanPeriod="30 seconds"> <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>D:deploylogs anger.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>ranger.%d{yyyy-MM-dd}.log</fileNamePattern> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread] %logger{35} - %msg%n</pattern> </encoder > </appender> <root level="INFO"> <appender-ref ref="file" /> </root> </configuration>
4. 控制台和文件同时记录
<configuration scan="true" scanPeriod="30 seconds"> <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>D:deploylogs anger.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>ranger.%d{yyyy-MM-dd}.log</fileNamePattern> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread] %logger{35} - %msg%n</pattern> </encoder > </appender> <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread] %logger{35} - %msg%n</pattern> </encoder > </appender> <root level="INFO"> <appender-ref ref="file" /> <appender-ref ref="stdout" /> </root> </configuration>
5. 为某个类或者包单独设置日志级别
<configuration scan="true" scanPeriod="30 seconds"> <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>D:deploylogs anger.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>ranger.%d{yyyy-MM-dd}.log</fileNamePattern> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread] %logger{35} - %msg%n</pattern> </encoder > </appender> <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread] %logger{35} - %msg%n</pattern> </encoder > </appender> <!-- 默认日志级别是Error --> <root level="ERROR"> <appender-ref ref="file" /> <appender-ref ref="stdout" /> </root> <!-- 单独的日志级别设置 HibernateLogInterceptor的日志级别是DEBUG--> <logger name="com.ranger.utils.orm.HibernateLogInterceptor" additivity="false"> <level value="DEBUG" /> <appender-ref ref="file" /> </logger> </configuration>