Spring boot Logging configuration and Usage Details, springlogging

Source: Internet
Author: User
Tags java util

Spring boot Logging configuration and Usage Details, springlogging

This article is basically an official translation document!

Spring boot uses Commons Logging as the internal log system and provides default configurations for Java Util Logging, Log4J2, and Logback. If Starters of spring boot is used, Logback is used to record logs by default.

1. Log format

The default log output format in spring boot is as follows:

10:57:51. 112 INFO 45469 --- [main] org. apache. catalina. core. StandardEngine:
Starting Servlet Engine: Apache Tomcat/7.0.52
10:57:51. 253 INFO 45469 --- [ost-startStop-1] o. a. c. c. C. [Tomcat]. [localhost]. [/]:
Initializing Spring embedded WebApplicationContext
10:57:51. 253 INFO 45469 --- [ost-startStop-1] o. s. web. context. ContextLoader:
Root WebApplicationContext: initialization completed in 1358 MS
10:57:51. 698 INFO 45469 --- [ost-startStop-1] o. s. B. c. e. ServletRegistrationBean:
Mapping servlet: 'dispatcherservlet 'to [/]
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 sorted by Time
2. Log Level-ERROR, WARN, INFO, DEBUG, TRACE
3. process ID
4. log Content, separated by the "---" Separator
5. Thread name-enclosed in square brackets
6. Log name-usually corresponding to the class name

Note: Logback has no FATAL level (mapped to ERROR)

Ii. Console output

The default log configuration will display the message when writing it to the console. By default, the ERROR, WARN, and INFO messages will be displayed. You can also enable the debug mode when starting, run the following command: java-jar yourapp. jar -- debug

Note: You can also. in the properties configuration file, specify debug = true to enable debug. Once the debug mode is enabled, the console outputs container information, hibernate information, and spring boot information at the same time.

Iii. file output

By default, spring boot only outputs logs to the console, but not to the log file. If you want to write logs to the log file, you must. set logging in the properties configuration file. file or logging. path

Note: The relationship here is or, that is, you configure logging. file or logging. path, and the effect is the same.

The following table shows how to output the configuration file:

Logging. file Logging. path Example Description
      If neither of them is configured, It is output to the Console only.
Specified file   My. log Write 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. The directory can be an exact location or a relative directory.

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

Note: The log configuration is independent from the actual log component. That is to say, if you specify the Configuration Attribute Logback. configurationFile for logback, spring boot will not manage the log component.

Iv. Log Level

All supported log systems can use Spring Environment to specify the log level, such as application. properties, you can use "logging. level. * = LEVEL to specify the log LEVEL. The value of LEVEL can be TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF. The configuration example is as follows:

Logging. level. root = WARN # The root log outputs logging.level.org at the WARN level. springframework. web = DEBUG # org. springframework. logs under the web package output logging.level.org at the DEBUG level. hibernate = ERROR # org. logs in the hibernate package are output at the ERROR level.

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

logging.level.com.chhliu=INFO 

In the preceding configuration, "com. chhliu" is the package name of our application.

V. Custom log output format

We can configure the log output format 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{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n 

Note: The preceding configuration only applies to Logback.

6. log using Log4j

As mentioned above, we use Logback as the log system by default. What should we do if we want to use Log4j for logging, we need to add the Log4j starter in the pom file and exclude Logback at the same time, as shown below:

<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 for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.