SpringBoot and logging -slf4j
- The logging framework
- The use of slf4j
- SpringBoot log relationship
- Log to use
-
-
- Switching logging Frameworks
-
The logging framework
Log facade (interface, log abstraction layer) | The logging implementation |
---|---|
JCL(Jakarta Commons Logging), SLF4J (Simple Logging Facade for Java), jboss-Logging | Log4j, JUL(java.util.logging), log4j2, Logback |
- Log facade: SLF4J
- Logback SpringBoot uses SLF4J and Logback
The use of slf4j
- Slf4j: 1. Logging methods should not call the logging implementation class, should call the log abstraction layer in the method. Import slF4J JAR and Logback implementation JAR to the system. 2. Each log implementation framework has its own configuration file. After using SLF4J, the configuration file is still the one that implements the framework using logging.
- Problem: Using SLf4J + LogBack requires a unified transformation of logging, and other frameworks using other logging frameworks need to use SLF4J output together: 1. Replace other framework logs in the system first, 2. Replace the original logging framework with slF4J oriented JAR, 3. Import the SLF4J JAR and Logback implementation JAR.
SpringBoot log relationship
- The bottom layer of SpringBoot also uses slF4J + Logback for logging
- SpringBoot replaces all other logs with SLF4J
- If you need to introduce another framework, be sure to remove that framework’s default logging framework dependency. SpringBoot can automatically adapt all logging, and the underlying use of SLF4J +logback logging, when introducing other frameworks, just need to exclude the logging framework that this framework depends on.
Log to use
- The default configuration
logging.file | logging.path | Example | Description |
---|---|---|---|
none | (none) | Output only at the console | |
Specify file name | (none) | my.log | Output logs to the my.log file |
(none) | The specified directory | /var/log | To the spring.log file in the specified directory |
- Specify the configuration file specified for each logging framework on the classpath, and SpringBoot does not use its own default configuration.
Logging System | Customization |
---|---|
Logback | logback-spring.xml, logback-spring.groovy, logback.xml or logback.groovy |
Log4j2 | log4j2-spring.xml or log4j2.xml |
JDK(Java Util Logging) | logging.properties |
- Logback. XML: will be recognized and loaded directly by the logging framework
- Logback-spring. XML (recommended) : The log framework cannot load log configuration items directly. SpringBoot parses log configuration by using only springBoot’s advanced function SpringProfile
<springProfile name="dev"></springProfile>
Copy the code
Settings Specifies that a certain configuration takes effect only in a certain environment
Switching logging Frameworks
- You can follow slF4J’s log adaptation diagram for the relevant switch-replace plus introduction
- Switch log4j2: Since SpringBoot has a log4j2 framework underneath, just exclude the previous starter-logging dependency in the dependency.