SLF4J + Logback

Principle: There is a unified interface layer, which is the log facade. Pour concrete logging implementations into the project. During development, logging methods should not be called directly from the logging implementation class, but from methods in the logging abstraction layer.

Log facade (log abstraction layer) : JCL, SLF4j, jboss-Logging

Log implementation: JUL, log4j, logback, log4j2

The Spring framework uses JCL by default

Spring Boot uses SLF4j + Logback by default

Note that each logging implementation framework has its own configuration file. After using SLF4J, the configuration file is still made into the logging implementation framework’s own configuration file.

Non-native implementation of SLF4J-API for adaptation layer: Because spring Boot uses other frameworks, not all of the log abstraction layers embedded within each framework are SLF4J, so Spring Boot needs to unify all of the log abstraction layers to use SLF4J. Replace commons-logging.jar with jcl-over-slf4j.jar.

Steps to unify all logs on the system into SLF4J:

1. Exclude other log frameworks from the system

2. Replace the original log framework with a middle package

3. Import other implementations of SLF4J

The level of logging

trace < debug < info < warn < error
Copy the code

Modify the default logging configuration through the application.properties configuration file:

  • logging.file

logging.file.name:

If no path is specified, a log of ‘specify filename’ is generated under the current project (with a higher weight than logging.file.path).

You can specify the full path, for example, to generate the log file on disk G:

logging.file.name = G:/springboot.log
Copy the code

logging.file.path:

Output to the spring.log file in the specified directory. Note: When logging.file.name is also specified, the directory specified by path becomes invalid.

logging.file.path = /spring/log
Copy the code

Create the spring folder and log folder in the root directory of the current disk, using the default file name spring.log as the file name

  • loggign.pattern

Default format:

Time + log level + thread ID + three dash delimiters + thread name + full class name + message + newline

loggign.pattern.console:

The format of the log output on the console

logging.pattern.file

Specifies the format of the log output in the file

Customize log configuration files

Load the corresponding custom configuration file in the classpath. Spring Boot does not use its default configuration file. For details about the file name, see 👇

Explanation: If logback is used, load the corresponding custom configuration file logback-spring. XML or another default file name in the classpath.

Logback.xml is recognized directly by the logging framework, bypassing the Spring framework. You are advised to use logback-spring. XML as the configuration file name. The log framework does not recognize the file name. Therefore, the log configuration file is not directly loaded but executed by spring Boot. The benefit is that you can use features in configuration files to specify that a particular configuration takes effect only in a particular environment.

Reference:

SLF4J official documentation: www.slf4j.org/manual.html

Video tutorial: www.bilibili.com/video/BV1Et…