将 log4j.properties 转换为 log4j2.xml

Converting log4j.properties into log4j2.xml

我从来没有处理过这类问题,现在我应该将 log4j.properties 文件移植到 log4j2.xml 中,但我遇到了很多问题。

有没有我可以使用的工具?或任何指南,因为我真的被困在这里。

我必须转移的一些 Log4j.properties 列表:

log4j.additivity.com.company.areas.audit.config.auditors.TransactionAuditor=false;
(Bunch of lines like these below, with different paths)
log4j.logger.net.sf=WARN, FILE,FILE_ERROR,CONSOLE
log4j.logger.xxxx.yyyy.zzzz = ERROR,FILE,FILE_ERROR,CONSOLE
log4j.logger.aaaa.bbbb.cccc = INFO, FILE,FILE_ERROR,CONSOLE
log4j.appender.CONSOLE.Threshold=OFF
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p - %m%n
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=${base.path}/logs/application.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p - %m%n
log4j.appender.FILE.MaxFileSize=100000KB
log4j.appender.FILE.MaxBackupIndex=5
log4j.appender.FILE_PERFORMANCE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE_PERFORMANCE.File=${base.path}/logs/application_performance.log
log4j.appender.FILE_PERFORMANCE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE_PERFORMANCE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p - %m%n
log4j.appender.FILE_PERFORMANCE.MaxFileSize=10000KB
log4j.appender.FILE_PERFORMANCE.MaxBackupIndex=2
(and bunch of similar stuff but for example instead of FILE_PERFORMACE I have things like: "FILE_ERROR" -> log4j.appender.FILE_ERROR = org.apache.log4j.RollingFileAppender", "FILE_INTEGRATION" etc"

如果有人能帮我从上面翻译这些属性,我相信我也能自己翻译其他的,因为它们与这些相似。

提前致谢!

我没有找到将 log4j.properties 文件翻译成 log4j2.xml 的工具,但手动翻译已结束很简单。

记录器示例:

log4j.additivity.com.exm.tst.audit.config.auditors.TransactionAuditor=false
log4j.logger.com.exm.tst.audit.config.auditors.TransactionAuditor=ERROR, FILE

将成为:

<Logger name="com.exm.tst.audit.config.auditors.TransactionAuditor" 
   additivity="false" level="ERROR">
   <appender-ref ref="FILE"/>
</Logger>

appender 示例:

log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=${catalina.base}/logs/application.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p - %m%n
log4j.appender.FILE.MaxFileSize=100000KB
log4j.appender.FILE.MaxBackupIndex=5

将成为:

<RollingFile name="FILE" fileName="${sys:catalina.home}/logs/application.log"
    filePattern="${sys:catalina.home}/logs/application.%i.log.gz" >
    <PatternLayout pattern="${LOG_PATTERN}"/>
    <Policies>
        <SizeBasedTriggeringPolicy size="10000KB" />
    </Policies>
    <DefaultRolloverStrategy max="5"/>
</RollingFile>

因此基于此示例的整个 log4j2.xml 文件应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
 
    <!-- Logging Properties -->
    <Properties>
        <Property name="LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p - %m%n</Property>
    </Properties>
     
    <Appenders>
         
        <RollingFile name="FILE" fileName="${sys:catalina.home}/logs/application.log"
            filePattern="${sys:catalina.home}/logs/application.%i.log.gz" >
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="10000KB" />
            </Policies>
            <DefaultRolloverStrategy max="5"/>
        </RollingFile>
         
    </Appenders>
 
    <Loggers>
     
        <Logger name="com.exm.tst.audit.config.auditors.TransactionAuditor" 
           additivity="false" level="ERROR">
           <appender-ref ref="FILE"/>
        </Logger>
 
    </Loggers>
 
</Configuration>