logback-spring.xml配置示例
1 <?xml version="1.0" encoding="UTF-8"?> 2 <configuration> 3 <include resource="org/springframework/boot/logging/logback/defaults.xml" /> 4 5 <springProperty scope="context" name="springAppName" source="spring.application.name" /> 6 <!-- Example for logging into the build folder of your project --> 7 <property name="LOG_FILE" value="/data/logs/crm/${springAppName}" /> 8 9 <!-- You can override this to have a custom pattern --> 10 <property name="CONSOLE_LOG_PATTERN" value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" /> 11 12 <springProfile name="local,dev,test"> 13 <!-- Appender to log to console --> 14 <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> 15 <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> 16 <!-- Minimum logging level to be presented in the console logs --> 17 <level>DEBUG</level> 18 </filter> 19 <encoder> 20 <pattern>${CONSOLE_LOG_PATTERN}</pattern> 21 <charset>utf8</charset> 22 </encoder> 23 </appender> 24 <root level="INFO"> 25 <appender-ref ref="console" /> 26 </root> 27 <!--打印sql语句--> 28 <!-- <logger name="com.tomato.boss.alarm.mapper" level="debug" additivity="false"> 29 <appender-ref ref="console" /> 30 </logger> --> 31 32 </springProfile> 33 34 <springProfile name="test,pre,prod"> 35 <!-- Appender to log to file --> 36 <appender name="flatfile" class="ch.qos.logback.core.rolling.RollingFileAppender"> 37 <file>${LOG_FILE}.log</file> 38 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 39 <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.gz</fileNamePattern> 40 <maxHistory>30</maxHistory> 41 </rollingPolicy> 42 <encoder> 43 <pattern>${CONSOLE_LOG_PATTERN}</pattern> 44 <charset>utf8</charset> 45 </encoder> 46 </appender> 47 48 <appender name="logstash" 49 class="ch.qos.logback.core.rolling.RollingFileAppender"> 50 <file>${LOG_FILE}.json</file> 51 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 52 <fileNamePattern>${LOG_FILE}.json.%d{yyyy-MM-dd}.gz 53 </fileNamePattern> 54 <maxHistory>7</maxHistory> 55 </rollingPolicy> 56 <encoder 57 class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder"> 58 <providers> 59 <timestamp> 60 <timeZone>UTC+8</timeZone> 61 </timestamp> 62 <pattern> 63 <pattern> 64 { 65 "severity": "%level", 66 "service": "${springAppName:-}", 67 "trace": "%X{X-B3-TraceId:-}", 68 "span": "%X{X-B3-SpanId:-}", 69 "parent": "%X{X-B3-ParentSpanId:-}", 70 "exportable": "%X{X-Span-Export:-}", 71 "pid": "${PID:-}", 72 "thread": "%thread", 73 "class": "%logger{40}", 74 "rest": "%message" 75 } 76 </pattern> 77 </pattern> 78 </providers> 79 </encoder> 80 </appender> 81 82 <root level="INFO"> 83 <appender-ref ref="flatfile" /> 84 <!-- uncomment this to have also JSON logs --> 85 <appender-ref ref="logstash"/> 86 </root> 87 </springProfile> 88 </configuration>