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

2022-04-12 00:00:00 java log4j log4j2

我对log4j不熟悉。我正在将应用程序从log4j1.2转换为log4j2。在log4j.properties文件中,我找到了以下配置。

#############################################################
#      Default Logging Configuration File
############################################################

############################################################
#      Global properties
############################################################
handlers= java.util.logging.ConsoleHandler
.level= WARNING
############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter

# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################

# For example, set the com.xyz.foo logger to only log SEVERE
# messages:
com.xyz.foo.level = SEVERE

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

谢谢!


解决方案

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

类为org.apache.log4j.config.Log4j1ConfigurationConverter。除了log4j-1.2-api模块之外,您还需要在类路径上使用JCommand(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的文件中,并将其放入应用程序的类路径中。

相关文章