SpringBoot Learning Notes 6-SpringBoot log

Keywords: Java Spring Boot Back-end


Based on the official spring documentation, the official address is: Spring Boot
At present, the latest official version is: 2.5.6 (this series of study notes is basically based on translation, so don't waste time expecting to get more. It's more a note made to show yourself the official documents)
Content of the current chapter: introduce the spring application feature in Chapter 7 of the official document 2.5.6_ journal
Reference articles are: 1 SpringBoot official documentation 2. SpringBoot Logging

1. Default settings

  • Default console output log
  • Log back is used by default (Java Util Logging, Log4J2 and Logback can be selected, or)
  • Default log level: INFO (log level: ERROR, WARN, INFO, DEBUG, or TRACE)

Different log frames have different default configuration file names.

Generally, logs are built by a combination of abstraction layer and implementation layer. The abstraction layer includes JCL, SLF4J and JBoss logging, and the implementation layer includes jul, log4j, log4j2 and logback. SpringBoot selects the combination of SLF4J+Logback.
When we use other frameworks, the framework itself may also bring its own logging framework. You can choose to exclude from the corresponding frame of the pom file

If you want to switch the default logging framework of SpringBoot, you need to exclude the dependency of Logback in starter and the dependency of Log4J replacement package

            <!-- Exclude for Logback Dependence of-->
                <!-- Exclude for Log4j Replacement package dependency for -->

The adaptation package of SLF4J for Log4j is introduced

<!-- increase SLF4J about Log4J Is the adapter dependency -->

2. Log format

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

Date and time: millisecond precision and easy to sort.
Process ID.
A separator is used to distinguish the beginning of the actual log message.
Thread Name: enclosed in square brackets (may be truncated for console output).
Recorder Name: This is usually the source class name (usually abbreviated).
Log messages.

Default log output color:

3. File output

By default, Spring Boot records only to the console and does not write to the log file. If you want to write log files outside the console output, you need to set a logging.file.name or logging.file.path property (for example, configured in application.properties). If there is no special setting, the output file log level is also INFO.
logging.file.path and logging.file.name do not exist at the same time. Logging.file.name mainly exists at the same time.

# Generate to the specified path, and the log name is spring.log.
# If it is not specified, it will be generated in the project root directory by default (you can also specify an absolute path), and the log name can be set at will

4. Log level

All supported log systems can specify the log LEVEL through the Spring Environment, such as application.properties. You can use "logging.level.*=LEVEL" to specify the log LEVEL. The values of "LEVEL" can be trace, debug, info, warn, error, fatal and off. Configuration examples are as follows:

#The root log is output at the WARN level

#The logs under the org.springframework.web package are output at the DEBUG level 

#The logs under the org.hibernate package are output at the ERROR level

If we need to specify our application log level, we can also use the same method as follows (com.example is the package name):


5. Customize log output format

The log output format we need can be configured through logging.pattern.file and logging.pattern.level, for example (only for logback):

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

6. Log configuration combined with Profile

<springProfile name="staging">
    <!-- When profile be known as staging When, the current log configuration is enabled -->

<springProfile name="dev | staging">
    <!-- When profile be known as dev or staging When, the current log configuration is enabled -->

<springProfile name="!production">
    <!-- When profile Name is not production When, the current log configuration is enabled -->

Posted by lucidfx on Wed, 10 Nov 2021 06:14:29 -0800