springboot 日志使用了apache common logging项目来完成内部的日志记录,同时它能自己实现日志接口。springboot已经为 Java Util Logging, Log4J2, and Logback提供了默认实现。一般情况下不用自己去改变依赖,默认配置就可以运行了。
By default, if you use the “Starters”, Logback is used for logging.使用spring starter 则默认使用了logback日志实现
2019-03-05 10:57:51.112 INFO 45469 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.52
2019-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1358 ms
2019-03-05 10:57:51.698 INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2019-03-05 10:57:51.702 INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
日志默认是上面的格式
在运行的时候添加参数 --debug
或者 --trace
,也可以在配置文件里添加一行debug=true
或者trace=true
,可以输出有关embedded container, Hibernate, and Spring Boot 等模块的更多信息
终端如果支持ANSI的话,通过设置属性
spring.output.ansi.enabled=ALWAYS(总是)/DETECT(检测是否支持)/NEVER(从不)
来输出颜色
默认情况下 springboot是不会将日志写入文件的,想写入文件的话,需要配置以下属性:
logging.file.name: xxx.log
logging.file.path: /path/to/save
日志文件会在10MB时轮换,默认下只记录 error, warn, info级别
在配置文件里添加以下配置可以改变日志文件存储规则
属性 | 描述 |
---|---|
logging.logback.rollingpolicy.file-name-pattern | 文件命名规则 |
logging.logback.rollingpolicy.clean-history-on-start | 应用启动时是否清空 |
logging.logback.rollingpolicy.max-file-size | 归档最大大小 |
logging.logback.rollingpolicy.total-size-cap | 最大文件数 |
logging.logback.rollingpolicy.max-history | 日志保存时间(默认7天) |
最重要的地方!!!
通常可以在配置文件里写。使用logging.level.
来确定一个模块的等级
示例:
logging:
level:
root: "warn"
org.springframework.web: "debug" #这里写模块的引用路径
org.hibernate: "error" # 可以不加双引号, 直接写成 error 或者 ERROR都行
The above approach will only work for package level logging. Since relaxed binding always converts environment variables to lowercase, it is not possible to configure logging for an individual class in this way. If you need to configure logging for a class, you can use the SPRING_APPLICATION_JSON variable.
上诉这种方式只能工作在包级别,不能用这种方式为单独的类配置日志级别
通常springboot使用内部已经实现的日志模块来控制。如果想要配置更为复杂的日志,这时候就需要写xml文件了。配置xml文件时有两种方法,一种是直接命名为 xxx-spring.xml并放在classpath下,另外一种是在配置文件里配置属性logging.config
来指定文件路径。要注意一点,日志模块的初始化是在上下文创建之前的,所以在spring的配置中无法生效,只能通过系统属性来修改日志模块的配置。
When possible, we recommend that you use the -spring variants for your
logging configuration (for example, logback-spring.xml rather than
logback.xml). If you use standard configuration locations, Spring
cannot completely control log initialization.
spring建议使用 -spring的命名方式
you need to set the debug attribute of the configuration element, i.e. the top-most element in the configuration file
设置debug属性开启状态信息的输出,基本上打开这个后,日志路径的设置,日志文件名的设置,模块级别的设备信息都能看到,十分方便调试,同时日志配置文件还可以动态更改并加载。示例
18:37:49,621 |-WARN in Logger[org.springframework.core.env.PropertySourcesPropertyResolver] - No appenders present in context [default] for logger [org.springframework.core.env.PropertySourcesPropertyResolver].
18:37:49,622 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
18:37:49,622 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [console]
18:37:49,628 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
18:37:49,644 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
18:37:49,646 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [rollingFile]
18:37:49,654 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1704188880 - No compression will be used
18:37:49,655 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1704188880 - Will use the pattern .//demo-log.%d{yyyy-MM-dd}.log for the active file
18:37:49,657 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern './/demo-log.%d{yyyy-MM-dd}.log'.
18:37:49,657 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
18:37:49,662 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Mon Jun 07 16:03:58 CST 2021
18:37:49,662 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
18:37:49,664 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[rollingFile] - Active log file name: .//demo-log.log
18:37:49,664 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[rollingFile] - File property is set to [.//demo-log.log]
18:37:49,665 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.example.demo] to INFO
18:37:49,665 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@52ba9da0 - Propagating INFO level on Logger[com.example.demo] onto the JUL framework
18:37:49,666 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
18:37:49,666 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [console] to Logger[ROOT]
18:37:49,666 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [rollingFile] to Logger[ROOT]
18:37:49,666 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
18:37:49,666 |-INFO in org.springframework.boot.logging.logback.SpringBootJoranConfigurator@64d84da1 - Registering current configuration as safe fallback point
18:37:49,673 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@52ba9da0 - Propagating INFO level on Logger[ROOT] onto the JUL framework
18:37:49,674 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@52ba9da0 - Propagating DEBUG level on Logger[com.example.demo] onto the JUL framework
同时
标签是有继承的,没有设置的情况下,下级目录的模块可以继承它上级目录模块日志级别,有指定的话则是指定的级别。
另外,springboot的配置文件里指定的日志级别是会覆盖自定义文件中的日志级别的。
参考连接:
spring特性说明
logback模块