SpringBoot 日志配置
Spring Boot
内部采用的是 Commons Logging
进行日志记录,但在底层为 Java Util Logging
、 Log4J2
、 Logback
等日志框架提供了默认配置 。
学习 SpringBoot
,使用 SpringBoot
默认的 Logback
就可以了。
Spring Boot
对各种日志框架都做了支持,我们可以通过配置来修改默认的日志的配置:
日志格式
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.0.RELEASE)
2018-12-02 18:29:25.023 INFO 11968 --- [ main] cn.ylx.SpringbootHelloworldApplication : Starting SpringbootHelloworldApplication on DESKTOP-10FVA01 with PID 11968 (C:\ylx\Git\springboot\springboot-helloworld\target\classes started by ys951 in C:\ylx\Git\springboot)
2018-12-02 18:29:25.028 INFO 11968 --- [ main] cn.ylx.SpringbootHelloworldApplication : No active profile set, falling back to default profiles: default
2018-12-02 18:29:26.059 INFO 11968 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-12-02 18:29:26.076 INFO 11968 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-12-02 18:29:26.076 INFO 11968 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.12
2018-12-02 18:29:26.083 INFO 11968 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Java\jdk1.8.0_181\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;.;C:\Java\jdk1.8.0_181\bin;C:\Java\jdk1.8.0_181\jre\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin;C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin;C:\ylx\apache-maven-3.3.9\bin;G:\ys\hadoop-2.6.0-cdh5.7.6\bin;C:\Scala\scala\bin;D:\JAVAEE\VisualSVN Server\bin;D:\JAVAEE\TortoiseSVN\bin;C:\Users\ys951\AppData\Local\Microsoft\WindowsApps;C:\Program Files (x86)\SSH Communications Security\SSH Secure Shell;C:\Users\ys951\AppData\Local\GitHubDesktop\bin;.]
2018-12-02 18:29:26.388 INFO 11968 --- [ main] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2018-12-02 18:29:26.396 INFO 11968 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-12-02 18:29:26.396 INFO 11968 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1314 ms
2018-12-02 18:29:26.421 INFO 11968 --- [ main] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-12-02 18:29:26.426 INFO 11968 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-12-02 18:29:26.427 INFO 11968 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-12-02 18:29:26.427 INFO 11968 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'formContentFilter' to: [/*]
2018-12-02 18:29:26.427 INFO 11968 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-12-02 18:29:26.601 INFO 11968 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2018-12-02 18:29:26.785 INFO 11968 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-12-02 18:29:26.789 INFO 11968 --- [ main] cn.ylx.SpringbootHelloworldApplication : Started SpringbootHelloworldApplication in 2.187 seconds (JVM running for 3.153)
日志格式说明:
Logback
是没有 FATAL
级别的日志,它将被映射到 ERROR
- 时间日期:精确到毫秒,可以用于排序
- 日志级别:ERROR、WARN、INFO、DEBUG、TRACE
- 进程ID
- 分隔符:采用 — 来标识日志开始部分
- 线程名:方括号括起来(可能会截断控制台输出)
- Logger名:通常使用源代码的类名
- 日志内容:我们输出的消息
日志级别
SpringBoot
默认为我们输出的日志级别为 INFO
、 WARN
、 ERROR
,如需要输出更多日志的时候,可以通过以下方式开启:
- 命令模式配置:
java -jar app.jar --debug=true
, 这种命令会被SpringBoot
解析,且优先级最高 - 资源文件配置:
application.properties
配置debug=true
即可。该配置只对 嵌入式容器、Spring
、Hibernate
生效,我们自己的项目想要输出DEBUG
需要额外配置。
配置规则:logging.level.<logger-name>=<level>
日志输出级别配置
在这里插入代码片