Springboot 日志的默认输出格式如下:
2014-03-05 10:57:51.112 INFO 45469 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.52
输出的信息分别为:时间、日志级别、进程id、线程名称、日志名称(一般为类名称)、日志信息。
日志文件
默认情况下,springboot只会在控制台输出日志,如果需要将日志写入文件,可以配置logging.file
或 logging.path
。具体说明如下:
logging.file | logging.path | Example | Description |
---|---|---|---|
(none) | (none) | Console only logging. | |
Specific file | (none) | my.log | Writes to the specified log file. Names can be an exact location or relative to the current directory. |
(none) | Specific directory | /var/log | Writes spring.log to the specified directory. Names can be an exact location or relative to the current directory. |
logging.file.max-size
每个日志文件的大小默认为10MB,如果设置了另一个属性logging.file.max-history
,则会自动删除比较久远的日志文件。
日志级别
日志级别有:FATAL
, ERROR
, WARN
, INFO
, DEBUG
, or TRACE
。其中在logback中,FATAL级别被归类于ERROR。
Springboot中,可以通过以下方式设置:
logging.level.<logger-name>=<level>
logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
其中logger-name可以通过日志组(logging group)进行指定,如下指定tomcat日志的输出级别为trace:
logging.group.tomcat=org.apache.catalina, org.apache.coyote, org.apache.tomcat
logging.level.tomcat=TRACE
Springboot中预定义的日志组有:
Name | Loggers |
---|---|
web | org.springframework.core.codec , org.springframework.http , org.springframework.web |
sql | org.springframework.jdbc.core , org.hibernate.SQL |
自定义springboot日志配置
Springboot 2.0 默认支持的日志有:logback、log4j2、JDK(Java Util Logging ),如果要指定使用某种日志实现方式,可以通过以下任一方式实现:
- 加入对应的依赖库;
- 在classpath根目录放置对应的日志配置文件(或者位置由
logging.config
指定); - 强制指定
org.springframework.boot.logging.LoggingSystem
系统属性,值为一个具体的实现类名称。
根据日志实现的不同,springboot将会加载不同的日志配置文件:
Logging System | Customization |
---|---|
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 |