Common logging frameworks
- java.util.logging: a Java Native logging framework introduced by JDK in version 1.4
- Log4j: an open source project of Apache, which can control the destination of log information transmission to console, files, GUI components, etc. it can control the output format of each log. These can be flexibly configured through a configuration file without modifying the application code. Although the maintenance has been stopped, most enterprises use log4j.
- LogBack: an improved version of Log4j
- Log4j2: log4j2 is not just an upgraded version of log4j. It has been rewritten from beginning to end.
Slf4j is a log output interface, which has no specific implementation. Only with the help of log framework (Log4j, LogBack, Log4j2) can log output be better realized. Using slf4j can better enable users to switch the log framework without modifying the code.
Spring integration Log4j2
Log4j2 is log4j 1 X, in log4j 1 X, which provides many i available functions in Logback and fixes some inherent problems in Logback.
Maven configuration
First, check whether Spring's own logging system is based on the Commons logging interface. If so, in order to avoid conflicts, you need to cancel the dependency of Commons logging in Spring in the configuration dependency, as follows:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency>
2. Dependencies required for configuring Slf4j: (select "1.7.25" version)
<!-- slf4j config start --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${slf4j}</version> <scope>runtime</scope> </dependency> <!-- slf4j config end -->
Slf4j API: the core package of slf4j has only a log interface and is not implemented.
JCL over Slf4j: a bridge that redirects the log output of JCL to Slf4j.
3. Dependencies required to configure log4j2: (select "2.17.0" version)
<!-- log4j2 config start --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${log4j2}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-web</artifactId> <version>${log4j2}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>${log4j2}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>${log4j2}</version> <scope>runtime</scope> </dependency> <!-- log4j2 config end -->
Log4j slf4j impl: bind log4j2 and slf4j.
Write Log4j2 log configuration file
1. Configuration file format and read priority
The Log4j2 configuration file suffix must be ". xml", "json" or ". jsn".
The priority of the system selection profile is (first to last):
Note: in maven project, the configuration file is placed in the resources directory.
2. Detailed configuration file resolution
Log level: all < trace < debug < info < warn < error < fatal < off.
- Properties module: configure some global property values.
- Appenders module: log output related configuration.
- Logger s module: only when Loggers are defined and introduced according to the name attribute of the Appender can the Appender take effect.
log4j2.xml example:
Test:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; private static final Logger logger = LoggerFactory.getLogger(Test.class); .... logger.info("fastjson parse error, the json msg is {}", tempStr);