logback是SpringBoot内置的日志处理框架,你会发现spring-boot-starter其中包含了spring-boot-starter-logging,该依赖内容就是 Spring Boot 默认的日志框架 logback。而spring-boot-starter-web包含了spring-boot-starter,可通过引入spring-boot-starter-web后的项目JAR依赖关系很快得出这个结论。
-
在简单的SpringBoot项目中,我们并没有主动去配置过任何和日志打印的相关配置入logback.xml,但是控制台却打印了相关的启动日志。如下图:
-
我们没有配置任何其它配置,就可以看到来自logback root logger的输出信息。因为SpringBoot为logback提供了默认的配置文件base.xml(查看源码),base.xml文件里定义了默认的root输出级别为INFO,如下:
<!-- Base logback configuration provided for compatibility with Spring Boot 1.1 --> <included> <include resource="org/springframework/boot/logging/logback/defaults.xml" /> <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/> <include resource="org/springframework/boot/logging/logback/console-appender.xml" /> <include resource="org/springframework/boot/logging/logback/file-appender.xml" /> <root level="INFO"> <appender-ref ref="CONSOLE" /> <appender-ref ref="FILE" /> </root> </included>
-
默认情况下,Spring Boot将日志输出到控制台,不会写到日志文件
-
使用SpringBoot在application.properties或application.yml中只能配置简单的场景。保存路径、日志格式等,复杂的场景(区分 info 和 error 的日志、每天产生一个日志文件等)满足不了,只能自定义配置。
-
最后附上常用的logback-spring.xml模板:
<property name="MDC_LOG_PATTERN" value="%red(%d{yyyy-MM-dd'T'HH:mm:ss.SSS}) %green(%p filesystem %t) %blue(%logger{50}) %yellow([line:%L %msg]%n)"></property> <!--定义项目中日志输出位置: 控制台--> <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <!--定义项目的日志输出格式--> <layout class="ch.qos.logback.classic.PatternLayout"> <!-- <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%thread] %logger{35} - %msg%n</pattern>--> <pattern>${MDC_LOG_PATTERN}</pattern> </layout> </appender> <!--定义项目中日志输出位置: 文件存储--> <appender name="logFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>./log/ace/jpy.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>./log/ace/jpy.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%thread] %logger{35} - %msg%n</pattern> </encoder> </appender> <!--定义项目中日志输出位置: 文件存储 INFO--> <appender name="logFileInfo" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>./log/ace/info/jpy-info.log</file> <!--日志级别过滤--> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <!--只匹配对应级别日志--> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>./log/ace/info/jpy-info.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%thread] %logger{35} - %msg%n</pattern> </encoder> </appender> <!--定义项目中日志输出位置: 文件存储 WARN--> <appender name="logFileWarn" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>./log/ace/warn/jpy-warn.log</file> <!--日志级别过滤--> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <!--只匹配对应级别日志--> <level>WARN</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>./log/ace/warn/jpy-warn.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%thread] %logger{35} - %msg%n</pattern> </encoder> </appender> <!--定义项目中日志输出位置: 文件存储 ERROR--> <appender name="logFileError" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>./log/ace/error/jpy-error.log</file> <!--日志级别过滤--> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <!--只匹配对应级别日志--> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>./log/ace/error/jpy-error.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%thread] %logger{35} - %msg%n</pattern> </encoder> </appender> <!--项目中根日志控制--> <root level="INFO"> <appender-ref ref="logFile"/> <appender-ref ref="logFileInfo"/> <appender-ref ref="logFileWarn"/> <appender-ref ref="logFileError"/> <appender-ref ref="stdout"/> </root> <!--项目中指定包日志控制--> <!-- <logger name="cool.jpy" level="INFO"/>--> <logger name="cool.jpy" additivity="false"> <appender-ref ref="logFile"/> <appender-ref ref="logFileInfo"/> <appender-ref ref="logFileWarn"/> <appender-ref ref="logFileError"/> <appender-ref ref="stdout"/> </logger> </configuration>
-