logback的配置
springboot的application.yml:
logging:
config: classpath:logback-spring.xml
logback-spring.xml:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="log.filePath" value="./logs"/> <property name="log.context.name" value="private-library"/> <property name="log.charset" value="UTF-8"/> <property name="log.maxHistory" value="15"/> <property name="log.maxFileSize" value="100MB"/> <contextName>${log.context.name}</contextName> <!--0. 日志格式和颜色渲染 --> <!-- 彩色日志依赖的渲染类 --> <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/> <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/> <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/> <!-- 彩色日志格式 --> <property name="log.pattern" value="${log.pattern:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%${log.maxHistory}.${log.maxHistory}t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/> <!-- 日志文件中带thread的格式--> <property name="log.pattern2" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n"/> <!--1. 输出到控制台--> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <!--此日志appender是为开发使用,只配置最底级别,控制台输出的日志级别是大于或等于此级别的日志信息--> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>debug</level> </filter> <encoder> <Pattern>${log.pattern}</Pattern> <charset>${log.charset}}</charset> </encoder> </appender> <!--2. 输出到文档--> <!-- 2.1 level为 DEBUG 日志,时间滚动输出 --> <appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 此日志文档只记录debug级别的 --> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>debug</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <!-- 正在记录的日志文档的路径及文档名 --> <file>${log.filePath}/${log.context.name}-debug.log</file> <!--日志文档输出格式--> <encoder> <pattern>${log.pattern2}</pattern> <charset>${log.charset}</charset> </encoder> <!-- 日志记录器的滚动策略,按日期,按大小记录 --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 日志归档 --> <fileNamePattern>${log.filePath}/${log.context.name}-debug.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>${log.maxFileSize}</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <!--日志文档保留天数--> <maxHistory>${log.maxHistory}</maxHistory> </rollingPolicy> </appender> <!-- 2.2 level为 INFO 日志,时间滚动输出 --> <appender name="INFO_FILE" 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> <file>${log.filePath}/${log.context.name}-info.log</file> <encoder> <pattern>${log.pattern2}</pattern> <charset>${log.charset}</charset> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.filePath}/${log.context.name}-info.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>${log.maxFileSize}</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <maxHistory>${log.maxHistory}</maxHistory> </rollingPolicy> </appender> <!-- 2.3 level为 WARN 日志,时间滚动输出 --> <appender name="WARN_FILE" 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> <file>${log.filePath}/${log.context.name}-warn.log</file> <encoder> <pattern>${log.pattern2}</pattern> <charset>${log.charset}</charset> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.filePath}/${log.context.name}-warm.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>${log.maxFileSize}</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <maxHistory>${log.maxHistory}</maxHistory> </rollingPolicy> </appender> <!-- 2.4 level为 ERROR 日志,时间滚动输出 --> <appender name="ERROR_FILE" 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> <file>${log.filePath}/${log.context.name}-error.log</file> <encoder> <pattern>${log.pattern2}</pattern> <charset>${log.charset}</charset> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.filePath}/${log.context.name}-error.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>${log.maxFileSize}</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <maxHistory>${log.maxHistory}</maxHistory> </rollingPolicy> </appender> <appender name="INFO+FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 过滤掉 TRACE 和 DEBUG 级别的日志--> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> <file>${log.filePath}/${log.context.name}-info+.log</file> <encoder> <pattern>${log.pattern2}</pattern> <charset>${log.charset}</charset> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.filePath}/${log.context.name}-info+.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>${log.maxFileSize}</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <maxHistory>${log.maxHistory}</maxHistory> </rollingPolicy> </appender> <!-- <logger>用来设置某一个包或者具体的某一个类的日志打印级别、以及指定<appender>。 <logger>仅有一个name属性,一个可选的level和一个可选的addtivity属性。 name:用来指定受此logger约束的某一个包或者具体的某一个类。 level:用来设置打印级别,大小写无关:TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF, 还有一个特俗值INHERITED或者同义词NULL,代表强制执行上级的级别。 如果未设置此属性,那么当前logger将会继承上级的级别。 addtivity:是否向上级logger传递打印信息。默认是true。 <logger name="org.springframework.web" level="info"/> <logger name="org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor" level="INFO"/> --> <!-- 使用mybatis的时候,sql语句是debug下才会打印,而这里我们只配置了info,所以想要查看sql语句的话,有以下两种操作: 第一种把<root level="info">改成<root level="DEBUG">这样就会打印sql,不过这样日志那边会出现很多其他消息 第二种就是单独给dao下目录配置debug模式,代码如下,这样配置sql语句会打印,其他还是正常info级别: 【logging.level.org.mybatis=debug logging.level.dao=debug】 --> <!-- root节点是必选节点,用来指定最基础的日志输出级别,只有一个level属性 level:用来设置打印级别,大小写无关:TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF,不能设置为INHERITED或者同义词NULL。 默认是DEBUG可以包含零个或多个元素,标识这个appender将会添加到这个logger。 --> <!-- 4. 最终的策略 --> <!-- 4.1 开发环境:打印控制台--> <springProfile name="loc"> <logger name="com.xc.library" level="info"/> <root level="info"> <appender-ref ref="CONSOLE"/> <appender-ref ref="DEBUG_FILE"/> <appender-ref ref="INFO_FILE"/> <appender-ref ref="WARN_FILE"/> <appender-ref ref="ERROR_FILE"/> <appender-ref ref="INFO+FILE"/> </root> </springProfile> <!-- 4.2 生产环境:输出到文档--> <springProfile name="pro"> <logger name="com.xc.library" level="info"/> <root level="info"> <appender-ref ref="ERROR_FILE"/> <appender-ref ref="WARN_FILE"/> <appender-ref ref="INFO+FILE"/> </root> </springProfile> <!-- 打开sql的debug功能 --> <logger name="cn.chinaunicom.js.job.report.dao" level="INFO"/> <logger name="cn.chinaunicom.js.job.report.aspect" level="INFO"/> </configuration>
参考文章:https://blog.csdn.net/xu_san_duo/article/details/80364600