有一种力量无人能抵挡,它永不言败生来倔强。有一种理想照亮了迷茫,在那写满荣耀的地方。
参考文档:Logging
这里顺便引用以下部分原文,当然看不明白也没关系,我们有有道翻译,如果翻译的不准确,后面会提供详细配置分享给大家。
Spring Boot uses Commons Logging for all internal logging, but leaves the underlying log implementation open. Default configurations are provided for Java Util Logging, Log4J2 and Logback. In each case loggers are pre-configured to use console output with optional file output also available.
By default, If you use the ‘Starters’, Logback will be used for logging. Appropriate Logback routing is also included to ensure that dependent libraries that use Java Util Logging, Commons Logging, Log4J or SLF4J will all work correctly.
pom.xml相关配置就不跟大家在这里扯了,依赖自行添加。
配置日志的相关参数也只需要写在 application.properties或者application.yml中就可以了,当然,这仅仅是基本的配置。
#官方文档中有提到, SpringBoot 的 Logging 配置的级别有7个:TRACE , DEBUG , INFO , WARN , ERROR , FATAL , OFF
#root日志以INFO级别输出
logging.level.root=INFO
#springframework.web日志以WARN级别输出
logging.level.org.springframework.web=WARN
#hibernate日志以ERROR级别输出
logging.level.org.hibernate=ERROR
加入以上配置后,我们启动项目,就可以在控制台打印Log信息了。
但是,在生产环境中,日志往往要以文件形式存放到服务器,下面介绍一下spring-boot日志的文件输出方式。
logging.file=spring_boot.log
logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n
logging.pattern.file=%d{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n
配置完成以后,我们再次启动项目,这时候会在根目录下生成一个spring_boot.log日志文件。
但是,经历过项目上线的小伙伴,其实这样配置远远达不到生产要求。比如,区分普通日志和错误日志,按照日期存储日志,配置单个日志文件最大容量,删除多少天之前的文件等等!下面与大家分享一款更高级的配置。
Depending on your logging system, the following files will be loaded:
Logback:logback-spring.xml, logback-spring.groovy, logback.xml or logback.groovy
Log4j2:log4j2-spring.xml or log4j2.xml
JDK (Java Util Logging):logging.properties
spring-boot日志管理支持Logback,Log4j2以及Log4j,根据以上说明,我们可以定义文件命名。
下面我们使用Logback的指定配置文件实现更高级的日志配置。
logback-spring.xml:
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<contextName>spring-boot-logcontextName>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>log/debug.logfile>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>log/spring-boot-log-debug-%d{yyyy-MM-dd}.%i.logfileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>40MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30maxHistory>
rollingPolicy>
<append>trueappend>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%npattern>
<charset>utf-8charset>
encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUGlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>log/info.logfile>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>log/spring-boot-log-info-%d{yyyy-MM-dd}.%i.logfileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>40MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30maxHistory>
rollingPolicy>
<append>trueappend>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%npattern>
<charset>utf-8charset>
encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFOlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>log/error.logfile>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>log/spring-boot-log-error-%d{yyyy-MM-dd}.%i.logfileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>2MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>180maxHistory>
rollingPolicy>
<append>trueappend>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%npattern>
<charset>utf-8charset>
encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERRORlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%npattern>
<charset>utf-8charset>
encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFOlevel>
filter>
appender>
<logger name="com.wego" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT"/>
<appender-ref ref="DEBUG_FILE"/>
<appender-ref ref="INFO_FILE"/>
<appender-ref ref="ERROR_FILE"/>
logger>
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="ERROR_FILE" />
root>
configuration>
如果我们使用了 Logback 的指定配置文件的话,那么application.properties 中的配置可以取消了。
配置完成后,我们做一个测试,为了测试方便把info和error日志容量maxFileSize都改为2MB。
然后启动程序:
@EnableAutoConfiguration
public class Application {
private static final Logger logger = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(Application.class, args);
while(true){
logger.info("普通日志");
logger.error("错误日志");
}
}
}
执行一段时间,如果在项目路径下生成以下日志文件说明配置成功。