Logback是由log4j创始人设计的另一个开源日志组件,性能比log4j要好。
官方网站:https://logback.qos.ch/index.html
Logback主要分为三个模块:
后续的日志代码都是通过SLF4J日志门面搭建日志系统,所以在代码是没有区别,主要是通过修改配置文件和pom.xml依赖
- <dependency>
- <groupId>org.slf4jgroupId>
- <artifactId>slf4j-apiartifactId>
- <version>1.7.25version>
- dependency>
- <dependency>
- <groupId>ch.qos.logbackgroupId>
- <artifactId>logback-classicartifactId>
- <version>1.2.3version>
- dependency>
- public class logbackTest {
-
- public static final Logger LOGGER = LoggerFactory.getLogger(logbackTest.class);
-
- // 快速入门
- @Test
- public void testLogback() throws Exception {
- for (int i = 0; i < 10000; i++) {
- // 日志输出
- LOGGER.error("error");
- LOGGER.warn("warn");
- LOGGER.info("info");
- LOGGER.debug("debug");
- LOGGER.trace("trace");
- }
- }
- }
logback会依次读取以下类型配置文件:
logback组件之间的关系
基本配置信息
- <configuration>
-
- <property name="pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %c [%thread]%-5level %msg%n"/>
-
- <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
-
- <target>System.errtarget>
-
- <encoder
- class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
- <pattern>${pattern}pattern>
- encoder>
- appender>
-
-
- <root level="ALL">
- <appender-ref ref="console"/>
- root>
- configuration>
FileAppender、RollingFileAppender、Filter和异步日志配置汇总
- <configuration>
-
- <property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c %M %L [%thread] %m %n"/>
-
-
- <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
-
- <target>System.errtarget>
-
- <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
- <pattern>${pattern}pattern>
- encoder>
- appender>
-
-
- <property name="log_dir" value="logs" />
-
-
- <appender name="file" class="ch.qos.logback.core.FileAppender">
-
- <file>
- ${log_dir}/logback.log
- file>
-
- <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
- <pattern>${pattern}pattern>
- encoder>
- appender>
-
-
- <appender name="html" class="ch.qos.logback.core.FileAppender">
- <file>
- ${log_dir}/logback.html
- file>
-
- <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
- <layout class="ch.qos.logback.classic.html.HTMLLayout">
- <pattern>%-5level%d{yyyy-MM-dd HH:mm:ss}%c%M%L%thread%m%npattern>
- layout>
- encoder>
- appender>
-
-
-
- <appender name="rollFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <file>
- ${log_dir}/roll_logback.log
- file>
-
- <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
- <pattern>${pattern}pattern>
- encoder>
-
- <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
-
- <fileNamePattern>
- ${log_dir}/rolling.%d{yyyy-MM-dd-HH-mm-ss}.log%i.gz
- fileNamePattern>
-
-
- <maxFileSize>1MBmaxFileSize>
- rollingPolicy>
-
-
- <filter class="ch.qos.logback.classic.filter.LevelFilter">
-
- <level>ERRORlevel>
- <onMatch>ACCEPTonMatch>
- <onMissMatch>DENYonMissMatch>
- filter>
- appender>
-
-
- <appender name="async" class="ch.qos.logback.classic.AsyncAppender">
-
- <appender-ref ref="rollFile"/>
- appender>
-
-
-
- <root level="all">
- <appender-ref ref="console"/>
- <appender-ref ref="async"/>
- root>
-
-
- <logger name="com.itheima" level="info" additivity="false">
- <appender-ref ref="console"/>
- logger>
- configuration>
注:这里面定义了几种输出方式,可根据自生喜爱挑选喜欢的输出方式
官方提供的log4j.properties转换成logback.xml
https://logback.qos.ch/translator/
logback-access模块与Servlet容器(如Tomcat和Jetty)集成,以提供HTTP访问日志功能。我们可以使用logback-access模块来替换tomcat的访问日志。
<Valve className="ch.qos.logback.access.tomcat.LogbackValve" />
- <configuration>
-
- <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener"/>
- <property name="LOG_DIR" value="${catalina.base}/logs"/>
- <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <file>${LOG_DIR}/access.logfile>
- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
- <fileNamePattern>access.%d{yyyy-MM-dd}.log.zipfileNamePattern>
- rollingPolicy>
- <encoder>
-
- <pattern>combinedpattern>
- encoder>
- appender>
- <appender-ref ref="FILE"/>
- configuration>
官方配置: https://logback.qos.ch/access.html#configuration