它要比log4j的速度要快。但最主要的原因是它能在控制台打印SQL语句。而spring boot已经将它整合,只需在resourse下新建一个logback即可。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="debug" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--输出文件位置-->
<File>../crm-admin-log/server-debug.log</File>
<!--编码-->
<encoder>
<pattern>%d{yyyy-MM-dd.HH:mm:ss} [%5p] %c{50} %3L %M %m%n</pattern>
<immediateFlush>true</immediateFlush>
</encoder>
<!--日志级别-->
<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>../crm-admin-log/server-debug.log.%d{yyyy-MM-dd}</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>../crm-admin-log/server-info.log</File>
<encoder>
<pattern>%d{yyyy-MM-dd.HH:mm:ss} [%5p] %c{50} %3L %M %m%n</pattern>
<immediateFlush>true</immediateFlush>
</encoder>
<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>../crm-admin-log/server-info.log.%d{yyyy-MM-dd}</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="warn" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>../crm-admin-log/server-warn.log</File>
<encoder>
<pattern>%d{yyyy-MM-dd.HH:mm:ss} [%5p] %c{50} %3L %M %m%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>warn</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>../crm-admin-log/server-warn.log.%d{yyyy-MM-dd}</fileNamePattern>
</rollingPolicy>
</appender>
<!--输出到控制台-->
<appender name="consoleApp" class="ch.qos.logback.core.ConsoleAppender">
<!--输出样式-->
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
</pattern>
</layout>
</appender>
<!--设置某个包或者类使用哪个appender-->
<logger name="com.duoku.crmpackageadmin" additivity="false">
<appender-ref ref="debug" />
<appender-ref ref="info"/>
<appender-ref ref="warn"/>
<appender-ref ref="consoleApp"/>
</logger>
<!--全局日志级别-->
<root level="debug"></root>
</configuration>
最后在application.yml文件中引入logback-spring.xml
logging:
config: classpath:logback-spring.xml