将log4j 1.2配置转换为log4j 2配置

时间:2017-01-24 09:14:21

标签: java log4j log4j2

我是log4j的新手。我将应用程序从log4j 1.2转换为log4j2。在log4j.properties文件中,我找到了以下配置。

String[] listviewTitle = new String[]{
        "lstviewitem",   "lstviewitem", "lstviewitem", "lstviewitem",
        "lstviewitem",  "lstviewitem",  "lstviewitem",  "lstviewitem9",
        "lstviewitem","lstviewitem",  "lstviewitem", "lstviewitem"

};//12




int[] listviewbutton = new int[]{
        R.drawable.ic_action_right, R.drawable.ic_action_right, R.drawable.ic_action_right, R.drawable.ic_action_right,
        R.drawable.ic_action_right, R.drawable.ic_action_right, R.drawable.ic_action_right, R.drawable.ic_action_right,
        R.drawable.ic_action_right, R.drawable.ic_action_right, R.drawable.ic_action_right, R.drawable.ic_action_right,
};

如何将此配置转换为log4j2配置?

谢谢!

1 个答案:

答案 0 :(得分:2)

Log4j2在log4j-1.2-api模块中有一个(实验性)转换工具。

该课程为org.apache.log4j.config.Log4j1ConfigurationConverter。除了log4j-1.2-api模块,您还需要类路径上的JCommander(http://jcommander.org)。

如果您正在寻找带有控制台和文件追加程序的示例Log4j2配置,请尝试以下操作:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <File name="MyFile" fileName="all.log" append="false">
            <!-- alternatively use XmlLayout (requires Jackson, see documentation for dependencies) -->
            <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Logger name="com.xyz.foo" level="warn" />
        <Root level="trace">
            <AppenderRef ref="Console" level="info" />
            <AppenderRef ref="MyFile"/>
        </Root>
    </Loggers>
</Configuration>

将其保存到名为log4j2.xml的文件中,并将其放在应用程序的类路径中。