前言
日志在线上运维是重要的一环, 它会帮我们重现问题场景, 排除bug, 所以日志的配置在开发中是重中之重。
环境
SpringBoot配置
配置日志输出路径
logback-spring.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 <?xml version="1.0" encoding="utf-8"?> <configuration > <contextName > idea360-logs</contextName > <property name ="pattern" value ="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg %n" /> <property name ="pattern-color" value ="%yellow(%d{yyyy-MM-dd HH:mm:ss.SSS}) [%thread] %highlight(%-5level) %green(%logger{50}) - %highlight(%msg) %n" /> <property name ="LOG_HOME" value ="logs" /> <appender name ="CONSOLE" class ="ch.qos.logback.core.ConsoleAppender" > <encoder class ="ch.qos.logback.classic.encoder.PatternLayoutEncoder" > <pattern > ${pattern}</pattern > </encoder > </appender > <appender name ="CONSOLE-WITH-COLOR" class ="ch.qos.logback.core.ConsoleAppender" > <encoder class ="ch.qos.logback.classic.encoder.PatternLayoutEncoder" > <pattern > ${pattern-color}</pattern > </encoder > </appender > <appender name ="DEBUG" class ="ch.qos.logback.core.rolling.RollingFileAppender" > <filter class ="ch.qos.logback.classic.filter.LevelFilter" > <level > DEBUG</level > <onMatch > ACCEPT</onMatch > <onMismatch > DENY</onMismatch > </filter > <encoder class ="ch.qos.logback.classic.encoder.PatternLayoutEncoder" > <pattern > %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n </pattern > <charset > UTF-8</charset > </encoder > <rollingPolicy class ="ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > <fileNamePattern > ${LOG_HOME}/ai5.debug.%d.%i.log</fileNamePattern > <timeBasedFileNamingAndTriggeringPolicy class ="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP" > <maxFileSize > 10MB</maxFileSize > </timeBasedFileNamingAndTriggeringPolicy > <maxHistory > 30</maxHistory > </rollingPolicy > <encoder > <pattern > ${pattern}</pattern > </encoder > </appender > <appender name ="INFO" 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 > <encoder class ="ch.qos.logback.classic.encoder.PatternLayoutEncoder" > <pattern > %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n </pattern > <charset > UTF-8</charset > </encoder > <rollingPolicy class ="ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > <fileNamePattern > ${LOG_HOME}/ai5.info.%d.%i.log</fileNamePattern > <timeBasedFileNamingAndTriggeringPolicy class ="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP" > <maxFileSize > 10MB</maxFileSize > </timeBasedFileNamingAndTriggeringPolicy > <maxHistory > 30</maxHistory > </rollingPolicy > <encoder > <pattern > ${pattern}</pattern > </encoder > </appender > <appender name ="ERROR" 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 > <encoder class ="ch.qos.logback.classic.encoder.PatternLayoutEncoder" > <pattern > %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n </pattern > <charset > UTF-8</charset > </encoder > <rollingPolicy class ="ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > <fileNamePattern > ${LOG_HOME}/ai5.error.%d.%i.log</fileNamePattern > <timeBasedFileNamingAndTriggeringPolicy class ="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP" > <maxFileSize > 10MB</maxFileSize > </timeBasedFileNamingAndTriggeringPolicy > <maxHistory > 30</maxHistory > </rollingPolicy > <encoder > <pattern > ${pattern}</pattern > </encoder > </appender > <root level ="INFO" > <appender-ref ref ="CONSOLE-WITH-COLOR" /> <appender-ref ref ="DEBUG" /> <appender-ref ref ="INFO" /> <appender-ref ref ="ERROR" /> </root > </configuration >
配置控制台日志输出等级
1 2 3 logging: level: root: info
SpringMVC配置
maven
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <dependency > <groupId > ch.qos.logback</groupId > <artifactId > logback-classic</artifactId > <version > 1.2.3</version > </dependency > <dependency > <groupId > org.logback-extensions</groupId > <artifactId > logback-ext-spring</artifactId > <version > 0.1.4</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > jcl-over-slf4j</artifactId > <version > 1.7.25</version > </dependency >
web.xml
1 2 3 4 5 6 7 <context-param > <param-name > logbackConfigLocation</param-name > <param-value > classpath:logback-spring.xml</param-value > </context-param > <listener > <listener-class > ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class > </listener >
Non-Web配置
非web项目没有web.xml配置,所以需要把日志配置加在启动参数上。
启动参数
1 -Dlogback.configurationFile=conf/logback.xml
全局异常
以上日志当出现异常时无法输出堆栈信息到error.log中, 可以按如下配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 @RestControllerAdvice @Slf4j public class ApiExceptionHandler { @ExceptionHandler(Throwable.class) @ResponseBody public R handleException (Throwable e) { log.error(getStackTrace(e)); return R.failed(e.getMessage()); } private String getStackTrace (Throwable throwable) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); try { throwable.printStackTrace(pw); return "\n" +sw.toString(); } finally { pw.close(); } } }
最后
本文到此结束,感谢阅读。祝大家工作愉快。如果您觉得不错,请关注公众号【当我遇上你】,您的支持是我写作的最大动力。