Spring boot logging
文章目录
源码
源码链接 https://gitee.com/mayun_xiaodu/spring-boot-all
上一节 Spring boot 配置文件读取和yaml结构
首先我们搭一个web工程,集成spring-boot-starter-web;spring-boot-starter-web中依赖了spring-boot-starter-logging;
启动springboot
springboot 启动日志
springboot 提供的默认输出日志
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.6.RELEASE)
2021-03-29 09:55:06.451 INFO 80640 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-03-29 09:55:06.473 INFO 80640 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-03-29 09:55:06.473 INFO 80640 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.21]
2021-03-29 09:55:06.566 INFO 80640 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-03-29 09:55:06.566 INFO 80640 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1249 ms
2021-03-29 09:55:06.753 INFO 80640 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-29 09:55:06.882 INFO 80640 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
自定义logging
(这里使用 slf4j + logback 进行示例)
1.配置文件中设置日志参数
logging:
level:
root: info # 配置root 的级别
com.xiaodu.springboot.logging: warn # 配置具体包下的日志级别
file:
max-size: 10KB
max-history: 7
path: logging-logs
pattern:
## %d{日期格式} %level 日志级别 %t 线程 %logger{length} logger名 %m 日志内容 %L 日志输出的行数, %n 换行;
console: '%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [%15t] %logger{20} => %m Line: %L%n'
代码中输出日志
// 进行日志输出
log.trace(