一、Spring Boot日志介绍 Spring Boot对所有内部日志记录使用了Commons Logging,但是底层日志实现是开放的。为Java Util日志记录、Log4J2和Logback提供了缺省配置。在每种情况下,日志记录器都预先配置为使用控制台输出和可选的文件输出。
默认情况下,如果使用Spring Boot的“Starters”坐标,则默认使用Logback进行日志记录。还包括适当的Logback路由,以确保使用Java Util日志记录、Commons日志记录、Log4J或SLF4J的依赖库都能正确工作。
Logback是log4j框架的作者开发的新一代日志框架,它效率更高、能够适应诸多的运行环境,同时天然支持SLF4J。
二、相关maven坐标 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > org.codehaus.janino</groupId > <artifactId > janino</artifactId > <version > 2.6.1</version > </dependency >
三、引入logback.properties属性文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 logback.dir =/Users/zhangboqing/Downloads/logs logback.all.filename =springboot-base-frame-all logback.info.filename =springboot-base-frame-info logback.warn.filename =springboot-base-frame-warn logback.error.filename =springboot-base-frame-error logback.maxhistory =10 logback.maxfilesize =100MB logback.level =debug logback.charset =UTF-8 CONSOLE_LOG_PATTERN =%d{yyyy-MM-dd HH:mm:ss.SSS} %boldYellow([%thread]) %highlight(%-5level) %boldGreen(%logger{50}) - %msg%n FILE_LOG_PATTERN =%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n ifOpenConsol =true
四、引入logback.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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 <?xml version="1.0" encoding="UTF-8"?> <configuration xmlns ="http://ch.qos.logback/xml/ns/logback" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://ch.qos.logback/xml/ns/logback https://raw.githubusercontent.com/enricopulatzo/logback-XSD/master/src/main/xsd/logback.xsd" scan ="true" > <conversionRule conversionWord ="clr" converterClass ="org.springframework.boot.logging.logback.ColorConverter" /> <conversionRule conversionWord ="wex" converterClass ="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" /> <contextName > springboot-base-frame</contextName > <property resource ="logback.properties" /> <if condition ='p("ifOpenConsol").contains("true")' > <then > <appender name ="STDOUT" class ="ch.qos.logback.core.ConsoleAppender" > <encoder > <pattern > ${CONSOLE_LOG_PATTERN}</pattern > <charset > ${logback.charset}</charset > </encoder > <target > System.out</target > </appender > </then > </if > <appender name ="FILE-INFO" class ="ch.qos.logback.core.rolling.RollingFileAppender" > <file > ${logback.dir}/${logback.info.filename}.log</file > <append > true</append > <filter class ="ch.qos.logback.classic.filter.ThresholdFilter" > <level > INFO</level > </filter > <rollingPolicy class ="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy" > <fileNamePattern > ${logback.dir}/${logback.info.filename}.%d{yyyy-MM-dd}.%i.log</fileNamePattern > <maxHistory > ${logback.maxhistory}</maxHistory > | <maxFileSize > ${logback.maxfilesize}</maxFileSize > </rollingPolicy > <encoder class ="ch.qos.logback.classic.encoder.PatternLayoutEncoder" > <pattern > ${FILE_LOG_PATTERN}</pattern > <charset > ${logback.charset}</charset > </encoder > </appender > <appender name ="FILE-ALL" class ="ch.qos.logback.core.rolling.RollingFileAppender" > <file > ${logback.dir}/${logback.all.filename}.log</file > <append > true</append > <rollingPolicy class ="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy" > <fileNamePattern > ${logback.dir}/${logback.all.filename}.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern > <maxHistory > ${logback.maxhistory}</maxHistory > <maxFileSize > ${logback.maxfilesize}</maxFileSize > </rollingPolicy > <encoder class ="ch.qos.logback.classic.encoder.PatternLayoutEncoder" > <pattern > ${FILE_LOG_PATTERN}</pattern > <charset > ${logback.charset}</charset > </encoder > </appender > <appender name ="FILE-ERROR" class ="ch.qos.logback.core.rolling.RollingFileAppender" > <file > ${logback.dir}/${logback.error.filename}.log</file > <append > true</append > <filter class ="ch.qos.logback.classic.filter.ThresholdFilter" > <level > ERROR</level > </filter > <rollingPolicy class ="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy" > <fileNamePattern > ${logback.dir}/${logback.error.filename}.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern > <maxHistory > ${logback.maxhistory}</maxHistory > <maxFileSize > ${logback.maxfilesize}</maxFileSize > </rollingPolicy > <encoder class ="ch.qos.logback.classic.encoder.PatternLayoutEncoder" > <pattern > ${FILE_LOG_PATTERN}</pattern > <charset > ${logback.charset}</charset > </encoder > </appender > <root level ="${logback.level}" > <appender-ref ref ="STDOUT" /> <appender-ref ref ="FILE-ALL" /> <appender-ref ref ="FILE-INFO" /> <appender-ref ref ="FILE-ERROR" /> </root > </configuration >