Log4j 1 Stops Official Maintenance
Log4j2 earlier. XThe bug that popped out of xhas opened the pan. Some people open their own projects and see that they are using Log4j1.x, breathing instantly.
However, to upgrade or upgrade, life has to continue and Log4j has to look forward.
As early as 2015, the Apache Software Foundation announced Log4j 1.x No longer provides official support. We recommend upgrading to Log4j 2.x.
Complete official instructions here
Summary:
- Log4j 1 was first released in 1999 and will end maintenance in 2015.
- Twenty-one releases were made throughout the life cycle, and the last release was in 2012.
- Log4j 2 is not the next version of log4j 1, it is a completely rewritten project
- Authors are developing Log4j2, which is faster, more stable, and more powerful
In short, we strongly recommend Log4j 2.
Migration of Log4j
There may be many old projects using Log4j1. API for x, if we change to Log4j 2. What should x do?
1 Option 1: Use Log4.1 Bridge (log4j-1.2-api)
We can use this approach if we don't want to modify Java code at all.
Bridge mode used in this way! It tastes like selling dog meat with lamb heads.
1.1 Create Log4j 1 test project
- Introducing log4j 1 dependencies
dependencies { testCompile 'log4j:log4j:1.2.17' testCompile group: 'junit', name: 'junit', version: '4.12' }
- Place the configuration file src/test/resources/log4j for log4j 1 under the resource directory. XML
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd"> <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/' > <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%p %C{2} (%F:%L) - %m%n"/> </layout> </appender> <root> <priority value ="info" /> <appender-ref ref="STDOUT" /> </root> </log4j:configuration>
- Create a new test class and print the log using the api of log4j 1
import org.apache.log4j.Logger; import org.junit.Test; public class Log4jTest { Logger logger = Logger.getLogger(Log4jTest.class); @Test public void test(){ logger.info("Hello Log4j"); } }
- Run the test class because we are using ConsoleAppender, so the log is printed to the console
INFO com.pupu.testlab.log4j.Log4jTest - Hello Log4j
1.2 Use log4j 1.x Bridge
In addition to introducing log4j 2.x Necessary dependencies, and most importantly, log4j-1.2-api.jar
Below we can see log4j 1. We have removed the dependency of X.
dependencies { testImplementation 'org.apache.logging.log4j:log4j-1.2-api:2.17.1' testImplementation 'org.apache.logging.log4j:log4j-api:2.17.1' testImplementation 'org.apache.logging.log4j:log4j-core:2.17.1' testCompile group: 'junit', name: 'junit', version: '4.12' }
1.3 Configuration Migration
Because we only use log4j1 in Java code. X's API, but it's really log4j2, so of course, we'll also use log4j2. Configuration of X. There are some differences in the configuration of log4j1 and 2, please refer to the documentation for details.
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration> <Appenders> <Console name="STDOUT" target="SYSTEM_OUT"> <PatternLayout pattern="%p %C{2} (%F:%L) - %m%n"/> </Console> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="STDOUT"/> </Root> </Loggers> </Configuration>
Start the test class and view the log:
INFO log4j.Log4jTest (Log4jTest.java:11) - Hello Log4j
At this point, we don't need to modify Java code at all, just introduce the log4j2 dependency and modify the log4j configuration.
2 Option 2: Convert application to Log4j 2 API (log4j-api)
This is a more direct and thorough way to modify the Java code directly using the log4j2 api and implementation than the previous one.
The disadvantage, of course, is that you need to batch modify the package name and the declaration of the log variable in Java code, where you can customize regular expression batch modifications.
2.1 Introducing log4j 2 dependency
dependencies { testImplementation 'org.apache.logging.log4j:log4j-api:2.17.1' testImplementation 'org.apache.logging.log4j:log4j-core:2.17.1' testCompile group: 'junit', name: 'junit', version: '4.12' }
2.2 Modify the API in your code
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.junit.Test; public class Log4jTest { private static final Logger logger = LogManager.getLogger(Log4jTest.class); @Test public void test(){ logger.info("Hello Log4j"); } }