spring boot Logging configuration and usage details

  • 2021-01-06 00:38:37
  • OfStack

Preface: This article is basically a translation of the official document!

spring boot uses Commons Logging as the internal logging system, and provides default configurations for Java Util Logging, Log4J2, and Logback. If spring is used as Starters for boot, then by default Logback is used for logging.

1. Log format

The default log output format in spring is as follows:

[

2014-03-05 10:57:51.112 INFO 45469 --- [ main] org.apache.catalina.core.StandardEngine :
Starting Servlet Engine: Apache Tomcat/7.0.52
2014-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] :
Initializing Spring embedded WebApplicationContext
2014-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader :
Root WebApplicationContext: initialization completed in 1358 ms
2014-03-05 10:57:51.698 INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean :
Mapping servlet: 'dispatcherServlet' to [/]
2014-03-05 10:57:51.702 INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean :
Mapping filter: 'hiddenHttpMethodFilter' to: [/*]

]

The following items will be output:

1. Date and time -- accurate to milliseconds and simply sorted by time
2, the log level -- ERROR WARN, INFO, DEBUG, TRACE
3. Process ID
4. Log contents should be separated with "-- "separator
Thread name -- enclosed in square brackets
The name of the log -- usually corresponds to the class name

Note: Logback does not have an FATAL level (mapped to ERROR)

2. Console output

The default logging configuration will echo messages written to the console. By default, messages at the ERROR, WARN, and INFO levels will be echoed. You can also enable debug mode on startup with the following command: java-jar yourapp.jar --debug

Note: You can also specify debug=true in the application.properties configuration file to enable debug. Once debug mode is enabled, the console will output container information, hibernate information, and spring information

3. File output

By default, spring boot will log output to console only, rather than the output to the log file, if you want the log written to the log file, you need to in application. properties configuration file Settings logging. file or is logging path

logging.file. logging.path. logging.file: logging.file: logging.file: logging. path: logging.file: logging.file: logging. path: logging.

The following table shows how to do configuration file output:

logging.file logging.path Example 说明
      2者都不配置,则只输出到Console
指定文件   my.log 写入指定的日志文件。文件名可以是1个确切的
位置或相对目录
  指定的目录 /var/log 将日志文件写入指定的目录,目录可以是1个确切的位置或者是1个相对目录

By default, if the log file size reaches 10Mb, it will be truncated and output to a new log file.

Note: The logging configuration is independent of the actual logging component, that is, if Logback is configured with the property logback.configurationFile, spring will not manage the logging component.

4. Log level

Spring.level.*=LEVEL. The values of "LEVEL" can be TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF. Examples of configuration are as follows:


logging.level.root=WARN #root Log in WARN Level of output  
logging.level.org.springframework.web=DEBUG #org.springframework.web Log under the package DEBUG Level of output  
logging.level.org.hibernate=ERROR #org.hibernate Log under the package ERROR Level of output  

If we need to specify our application log level, we can use the same method, as follows:


logging.level.com.chhliu=INFO 

The "com. chhliu" in the above configuration is the package name for our application.

5. Customize the log output format

logging.pattern.file and logging.pattern.level can be used to configure the required log output format, for example:


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 

Note: The above configuration only works for Logback

6. Log with Log4j

Said earlier, we the default is to use Logback for logging system, so, if we want to use Log4j to log, how to do, we need to add Log4j at pom file starter exclude Logback at the same time, as follows:


<dependency> 
 <groupId>org.springframework.boot</groupId> 
 <artifactId>spring-boot-starter</artifactId> 
 <exclusions> 
  <exclusion> 
   <groupId>org.springframework.boot</groupId> 
   <artifactId>spring-boot-starter-logging</artifactId> 
  </exclusion> 
 </exclusions> 
</dependency> 
<dependency> 
 <groupId>org.springframework.boot</groupId> 
 <artifactId>spring-boot-starter-log4j</artifactId> 
</dependency> 

Related articles: