Java from 0 to architect Directory: [Java from 0 to architect] learning record
Gitee Code: https://gitee.com/szluyu99/mj_java_frame/tree/master/04_SpringBoot
In the actual development process, the log systems used in different libraries (projects) are not necessarily the same
- When multiple projects with a logging system are mixed together, the logging system may become a little complex or even conflict
In the figure above, the log systems of the three projects can operate independently without affecting each other
Staggered complex log system ① - multiple projects realize SLF4J facade
In the figure above, SLF4J finds that there are three implementations, which will conflict. Finally, SLF4J will choose one of them to overwrite the other implementations
You can eliminate two implementations according to your needs, leaving one desired implementation:
- The remaining Logback implementation:
*Log4j 2 X implementation
Interleaving complex logging system ② - unified bottom layer implementation as Logback
If you want to unify all log systems, for example, Logback is used at the bottom, the solution is:
- Exclude log4j 1 x,Log4j 2.x
- Add log4j 1 X to the package calling SLF4J: log4j-over-slf4j
- Add log4j 2 X to the package calling SLF4J: log4j-to-slf4j
<!-- log4j 1.x Pirated implementation of, which will be called internally SLF4J --> <dependency> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> <version>1.7.30</version> </dependency> <!-- log4j 2.x Pirated implementation of, which will be called internally SLF4J --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-to-slf4j</artifactId> <version>2.13.3</version> </dependency>
Finally realized structure:
Interleaving complex logging system ③ - unified bottom layer implementation is log4j 2 x
If you want to unify all log systems, for example, the underlying layer uses log4j 2 x
Solution 1
- Exclude log4j 1 x,Logback
- Add log4j 1 X to the package calling SLF4J: log4j-over-slf4j
- Add log4j 2 X and SLF4J adaptation package: log4j-slf4j-impl
<dependency> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> <version>1.7.30</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.13.3</version> </dependency>
Finally realized structure:
Solution 2
- Exclude log4j 1 x,Logback
- Add log4j 1 X to call log4j 2 X package: log4j-1.2-api
- Add log4j 2 X and SLF4J adaptation package: log4j-slf4j-impl
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-1.2-api</artifactId> <version>2.14.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.13.3</version> </dependency>
Final implementation structure:
Log dependency summary
Facade interface:
- Log4j API: facade interface (log4j 2.x)
- Slf4j API: facade interface
Log implementation:
- Logback classic: a log implementation framework that implements the slf4j API facade interface
- Log4j core: a log implementation framework that implements the log4j API facade interface (log4j 2.x)
- log4j: log4j 1.x log implementation framework
Adaptation package with SLF4J:
- slf4j-log4j12: the slf4j API facade interface is implemented, and log4j will be called internally
- log4j-slf4j-impl: it implements the slf4j API facade interface and calls log4j core internally
logback implements the slf4j API facade interface by default
- Log4j over slf4j: the pirated implementation of log4j, which calls slf4j API internally
- log4j-1.2-api: the pirated implementation of log4j, which calls log4j core internally
- Log4j to slf4j: the pirated implementation of log4j core, which calls slf4j API internally
SpringBoot integrated logging framework
In SpringBoot, the suggestions of the log framework are as follows:
- SLF4J + Logback
- SLF4J + Log4j 2.x
reference resources: Official documents
SpringBoot integration Logback
SpringBoot inherits Logback by default. There is no need to add the dependency of Logback. The location of the configuration file is:
- classpath:logback.xml
- classpath:logback-spring.xml (recommended by SpringBoot)
SpringBoot built in Logback configuration
- spring-boot.jar
- org/springframework/boot/logging/logback/defaults.xml
SpringBoot integrates log4j 2 x
Method 1: exclude packages through the < exclusion > tag
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <!-- Remove logging --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- add to Log4j2 of starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> </dependencies>
Method 2: overwrite the original dependency with a dependency whose version number does not exist:
<dependencies> <!-- Overwrite dependency with nonexistent version number --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> <version>0</version> </dependency> <!-- add to Log4j2 of starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> </dependencies>
The location of the configuration file should be:
- classpath:log4j2.xml
- classpath:log4j2-spring.xml (recommended by SpringBoot)
SpringBoot built in Log4j 2.x default configuration
- spring-boot.jar
- org/springframework/boot/logging/log4j2/log4j2.xml
SpringBoot logging configuration (application)
Set log level:
logging: level: root: info com.mj.dao: debug com.mj.controller: debug
Path to custom profile:
logging: config: classpath:log4j2-spring.xml
definition Log group:
logging: level: root: info project: debug group: project: - com.mj.dao - com.mj.controller