Preface: This article is basically the official document for translation!
Spring boot uses Commons Logging as the internal logging system, and provides default configurations for Java Util Logging, Log4J2 and Logback. If spring boot starters are used, Logback will be used by default for logging.
1. Log format
The default log output format in spring boot 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] oaccC[Tomcat].[localhost].[/] :
Initializing Spring embedded WebApplicationContext
2014-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] osweb.context.ContextLoader:
Root WebApplicationContext: initialization completed in 1358 ms
2014-03-05 10:57:51.698 INFO 45469 --- [ost-startStop-1] osbceServletRegistrationBean:
Mapping servlet: 'dispatcherServlet' to [/]
2014-03-05 10:57:51.702 INFO 45469 --- [ost-startStop-1] osbcembedded.FilterRegistrationBean:
Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
The following items will be output:
1. Date and time--accurate to milliseconds and simple sorting by time
2. Log level--ERROR,WARN,INFO,DEBUG,TRACE
3. Process ID number
4. Log contents are separated by the "---" separator
5. Thread name--enclosed in square brackets
6. The name of the log-used class name usually corresponds to
Note: Logback does not have a FATAL level (map to ERROR)
2. Console output
The default log configuration will echo messages when written to the console, and will echo messages at ERROR, WARN, and INFO levels by default. You can also enable debug mode when starting. The startup command is as follows: java -jar yourapp.jar --debug
Note: You can also specify debug=true in the application.properties configuration file to enable debug. Once the debug mode is turned on, the console will output container information, hibernate information and spring boot information at the same time.
3. File output
By default, spring boot will only output logs to console, not to log files. If you want to write logs to log files, you need to set logging.file or logging.path in the application.properties configuration file.
Note: Here is the relationship, that is, if you configure logging.file or logging.path, the effect is the same.
The following table will show how to perform configuration file output:
| logging.file | logging.path | Example | illustrate |
| Neither configuration is configured, and only output to Console | |||
| Specify the file | my.log | Writes to the specified log file. The file name can be an exact location or relative directory | |
| Specified directory | /var/log | Write log files to the specified directory, which can be an exact location or a relative directory |
By default, if the size of the log file reaches 10Mb, it will be truncated and output to the new log file.
Note: The log configuration is independent of the actual log component, that is, if the configured property specified for Logback is logback.configurationFile, then spring boot will not manage the log component.
4. Log level
All supported logging systems can specify log levels through Spring Environment, such as application.properties, and use "logging.level.*=LEVEL" to specify log levels. The value of "LEVEL" can be TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF. The configuration example is as follows:
logging.level.root=WARN #root log outputs at WARN level logging.level.org.springframework.web=DEBUG The log under #org.springframework.web package outputs at DEBUG level logging.level.org.hibernate=ERROR The log under #org.hibernate package outputs at ERROR level
If we need to specify our application log level, we can also use the same method as follows:
logging.level.com.chhliu=INFO
The "com.chhliu" in the above configuration is the package name of our application.
5. Custom log output format
We can configure the log output format we need through logging.pattern.file and logging.pattern.level, for example:
logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n logging.pattern.file=%d{yyyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n Note: The above configuration only works on Logback
6. Use Log4j to log
As mentioned earlier, our default is to use Logback as the log system. So, if we want to use Log4j to log, what should we do? We need to add Log4j starter to the pom file and exclude Logback, 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>
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.