从Log4j 1.x迁移
使用Log4j 1.x桥接
转换到Log4j 2的最简单方法,也许就是把log4j 1.x的jar文件替换为Log4j 2的log4j-1.2-api.jar。然而,要让这个方式可以使用,应用必须满足以下要求:
转换为Log4j 2的API
在大多数情况下,从Log4j 1.x的API转换为Log4j 2应该是相当简单的。许多日志语句将不需要任何调整。然而,以下改变必须调整。
配置Log4j 2
虽然Log4j 2的配置语法不同于Log4j 1.x, 但是即使不是全部,也是具备大多数同样的功能的。
需要注意的是,通过${foo}插入系统属性的功能,已经扩展成支持从许多不同的来源中查找属性。详细信息可以查阅Lookups的文档。例如,要查找名为catalina.base的系统属性,在Log4j 1.x,语法应该是${catalina.base},而在Log4j 2, 语法就应该是${sys:catalina.base}。
下面是Log4j 1.x和相对应的Log4j 2的配置示例。
实例1 – 使用Console Appender的简单配置
Log4j 1.x 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=“%d %-5p [%t] %C{2} (%F:%L) – %m%n”/>
- </layout>
- </appender>
- <category name=“org.apache.log4j.xml”>
- <priority value=“info” />
- </category>
- <Root>
- <priority value =“debug” />
- <appender-ref ref=“STDOUT” />
- </Root>
- </log4j:configuration>
Log4j 2 XML配置
-
- <?xml version=“1.0” encoding=“UTF-8”?>
- <Configuration>
- <Appenders>
- <Console name=“STDOUT” target=“SYSTEM_OUT”>
- <PatternLayout pattern=“%d %-5p [%t] %C{2} (%F:%L) – %m%n”/>
- </Console>
- </Appenders>
- <Loggers>
- <Logger name=“org.apache.log4j.xml” level=“info”/>
- <Root level=“debug”>
- <AppenderRef ref=“STDOUT”/>
- </Root>
- </Loggers>
- </Configuration>
实例2 – 使用File Appender的简单配置
Log4j 1.x 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=“A1” class=“org.apache.log4j.FileAppender”>
- <param name=“File” value=“A1.log” />
- <param name=“Append” value=“false” />
- <layout class=“org.apache.log4j.PatternLayout”>
- <param name=“ConversionPattern” value=“%t %-5p %c{2} – %m%n”/>
- </layout>
- </appender>
- <appender name=“STDOUT” class=“org.apache.log4j.ConsoleAppender”>
- <layout class=“org.apache.log4j.PatternLayout”>
- <param name=“ConversionPattern” value=“%d %-5p [%t] %C{2} (%F:%L) – %m%n”/>
- </layout>
- </appender>
- <category name=“org.apache.log4j.xml”>
- <priority value=“debug” />
- <appender-ref ref=“A1” />
- </category>
- <root>
- <priority value =“debug” />
- <appender-ref ref=“STDOUT” />
- </Root>
- </log4j:configuration>
Log4j 2 XML配置
-
- <?xml version=“1.0” encoding=“UTF-8”?>
- <Configuration>
- <Appenders>
- <File name=“A1” fileName=“A1.log” append=“false”>
- <PatternLayout pattern=“%t %-5p %c{2} – %m%n”/>
- </File>
- <Console name=“STDOUT” target=“SYSTEM_OUT”>
- <PatternLayout pattern=“%d %-5p [%t] %C{2} (%F:%L) – %m%n”/>
- </Console>
- </Appenders>
- <Loggers>
- <Logger name=“org.apache.log4j.xml” level=“debug”>
- <AppenderRef ref=“A1”/>
- </Logger>
- <Root level=“debug”>
- <AppenderRef ref=“STDOUT”/>
- </Root>
- </Loggers>
- </Configuration>
实例2 – SocketAppender
Log4j 1.x XML配置。Log4j 1.x的例子容易令人误解。SocketAppender实际上并不使用Layout,配置一个Layout并不会有任何效果。
-
- <?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=“A1” class=“org.apache.log4j.net.SocketAppender”>
- <param name=“RemoteHost” value=“localhost”/>
- <param name=“Port” value=“5000”/>
- <param name=“LocationInfo” value=“true”/>
- <layout class=“org.apache.log4j.PatternLayout”>
- <param name=“ConversionPattern” value=“%t %-5p %c{2} – %m%n”/>
- </layout>
- </appender>
- <appender name=“STDOUT” class=“org.apache.log4j.ConsoleAppender”>
- <layout class=“org.apache.log4j.PatternLayout”>
- <param name=“ConversionPattern” value=“%d %-5p [%t] %C{2} (%F:%L) – %m%n”/>
- </layout>
- </appender>
- <category name=“org.apache.log4j.xml”>
- <priority value=“debug”/>
- <appender-ref ref=“A1”/>
- </category>
- <root>
- <priority value=“debug”/>
- <appender-ref ref=“STDOUT”/>
- </Root>
- </log4j:configuration>
Log4j 2 XML配置
-
- <?xml version=“1.0” encoding=“UTF-8”?>
- <Configuration>
- <Appenders>
- <Socket name=“A1” host=“localHost” port=“5000”>
- <SerializedLayout/>
- </Socket>
- <Console name=“STDOUT” target=“SYSTEM_OUT”>
- <PatternLayout pattern=“%d %-5p [%t] %C{2} (%F:%L) – %m%n”/>
- </Console>
- </Appenders>
- <Loggers>
- <Logger name=“org.apache.log4j.xml” level=“debug”>
- <AppenderRef ref=“A1”/>
- </Logger>
- <Root level=“debug”>
- <AppenderRef ref=“STDOUT”/>
- </Root>
- </Loggers>
- </Configuration>
实例 4 – AsyncAppender
使用AsyncAppender的Log4j 1.x 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/” configDebug=“true”>
- <appender name=“ASYNC” class=“org.apache.log4j.AsyncAppender”>
- <appender-ref ref=“TEMP”/>
- </appender>
- <appender name=“TEMP” class=“org.apache.log4j.FileAppender”>
- <param name=“File” value=“temp”/>
- <layout class=“org.apache.log4j.PatternLayout”>
- <param name=“ConversionPattern” value=“%d %-5p [%t] %C{2} (%F:%L) – %m%n”/>
- </layout>
- </appender>
- <root>
- <priority value=“debug”/>
- <appender-ref ref=“ASYNC”/>
- </Root>
- </log4j:configuration>
Log4j 2 XML配置
-
- <?xml version=“1.0” encoding=“UTF-8”?>
- <Configuration status=“debug”>
- <Appenders>
- <File name=“TEMP” fileName=“temp”>
- <PatternLayout pattern=“%d %-5p [%t] %C{2} (%F:%L) – %m%n”/>
- </File>
- <Async name=“ASYNC”>
- <AppenderRef ref=“TEMP”/>
- </Async>
- </Appenders>
- <Loggers>
- <Root level=“debug”>
- <AppenderRef ref=“ASYNC”/>
- </Root>
- </Loggers>
- </Configuration>
Sample 5 – AsyncAppender with Console and File
使用AsyncAppender的Log4j 1.x 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/” configDebug=“true”>
- <appender name=“ASYNC” class=“org.apache.log4j.AsyncAppender”>
- <appender-ref ref=“TEMP”/>
- <appender-ref ref=“CONSOLE”/>
- </appender>
- <appender name=“CONSOLE” class=“org.apache.log4j.ConsoleAppender”>
- <layout class=“org.apache.log4j.PatternLayout”>
- <param name=“ConversionPattern” value=“%d %-5p [%t] %C{2} (%F:%L) – %m%n”/>
- </layout>
- </appender>
- <appender name=“TEMP” class=“org.apache.log4j.FileAppender”>
- <param name=“File” value=“temp”/>
- <layout class=“org.apache.log4j.PatternLayout”>
- <param name=“ConversionPattern” value=“%d %-5p [%t] %C{2} (%F:%L) – %m%n”/>
- </layout>
- </appender>
- <root>
- <priority value=“debug”/>
- <appender-ref ref=“ASYNC”/>
- </Root>
- </log4j:configuration>
Log4j 2 XML配置。需要注意的是,Async Appender需要在Appender引用的后面配置,这样才能让它正确的关闭。
-
- <?xml version=“1.0” encoding=“UTF-8”?>
- <Configuration status=“debug”>
- <Appenders>
- <Console name=“CONSOLE” target=“SYSTEM_OUT”>
- <PatternLayout pattern=“%d %-5p [%t] %C{2} (%F:%L) – %m%n”/>
- </Console>
- <File name=“TEMP” fileName=“temp”>
- <PatternLayout pattern=“%d %-5p [%t] %C{2} (%F:%L) – %m%n”/>
- </File>
- <Async name=“ASYNC”>
- <AppenderRef ref=“TEMP”/>
- <AppenderRef ref=“CONSOLE”/>
- </Async>
- </Appenders>
- <Loggers>
- <Root level=“debug”>
- <AppenderRef ref=“ASYNC”/>
- </Root>
- </Loggers>
- </Configuration>
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/113881.html