logback-spring.xml 中根据不同的业务表示,分类打印到不同的文件夹、时区动态设置
logback-spring.xml 完整配置
xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="true" scanPeriod="60 seconds">
<springProperty scop="context" name="spring.application.name" source="spring.application.name" defaultValue=""/>
<springProperty scop="context" name="timeZone" source="timezone.defaultTimeZone" defaultValue=""/>
<springProperty scop="context" name="spring.application.cloud.discovery.namespace"
source="spring.application.cloud.discovery.namespace" defaultValue="dev"/>
<property name="log.path" value="logs"/>
<!-- 从环境变量获取容器编号, 为处理多容器部署在同一服务, 挂载日志同写一个文件 -->
<property name="container.id" value="${VERSION:-0}"/>
<!-- %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %file:%line - %msg%n-->
<property name="CONSOLE_LOG_PATTERN"
value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS, ${timeZone}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} [%5L] %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<!-- 彩色日志格式 -->
<property name="log.pattern"
value="%d{yyyy-MM-dd HH:mm:ss.SSS, ${timeZone}} [%21.21t] %-5level [%-40.40logger{36}] [%line] - %msg%n"/>
<!-- 彩色日志依赖的渲染类 -->
<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"/>
<!-- Console log output -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="INFO" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator>
<key>appCode</key>
<defaultValue>default</defaultValue>
</discriminator>
<sift>
<appender name="FILE-${appCode}" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>
${log.path}/${spring.application.name}/${appCode}/${spring.application.name}-${container.id}.%d{yyyy-MM-dd,${timeZone}}.%i.log
</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
</appender>
</sift>
</appender>
<appender name="ERROR" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator>
<key>appCode</key>
<defaultValue>default</defaultValue>
</discriminator>
<sift>
<appender name="FILE-${appCode}" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>
${log.path}/${spring.application.name}/${appCode}/error/${spring.application.name}-error-${container.id}.%d{yyyy-MM-dd,${timeZone}}.%i.log
</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
</appender>
</sift>
</appender>
<appender name="android-appender" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator>
<key>appCode</key>
<defaultValue>default</defaultValue>
</discriminator>
<sift>
<appender name="FILE-${appCode}" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>
${log.path}/${spring.application.name}/${appCode}/android/${spring.application.name}-${container.id}.%d{yyyy-MM-dd,${timeZone}}.%i.log
</fileNamePattern>
<maxFileSize>100MB</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
</appender>
</sift>
</appender>
<logger name="android" additivity="false">
<appender-ref ref="android-appender"/>
</logger>
<root level="info">
<appender-ref ref="INFO"/>
<appender-ref ref="ERROR"/>
<appender-ref ref="console"/>
</root>
</configuration>
根据appCode存储日志
我的是根据不同的appCode,打印不同的文件夹中,如果没有拿到appCode,就打印到default中
在拦截器中,网mdc中put对应的appCode,在xml中使用${appCode}获取即可
时区动态设置
application.yml
yaml
timezone:
defaultTimeZone: 时区
定义属性关键代码:
xml
<springProperty scop="context" name="timeZone" source="timezone.defaultTimeZone" defaultValue=""/>
使用取值:
xml
${log.path}/${spring.application.name}/${appCode}/${spring.application.name}-${container.id}.%d{yyyy-MM-dd,${timeZone}}.%i.log