Log4j转换为Log4j2

时间:2016-06-13 11:49:22

标签: xml log4j log4j2

我有一个用于自定义日志记录的log4.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <!-- Console appender -->
    <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
        <param name="threshold" value="INFO"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="&lt;%-4d{DATE}&gt; &lt;%-4p&gt; &lt;%t&gt; &lt;%m&gt; %n" />
        </layout>
    </appender>

    <!-- File appender -->
    <appender name="FileAppender" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="threshold" value="INFO"/>
        <param name="File" value="test/logs/app.log" />
        <param name="Append" value="true" />
        <param name="MaxFileSize" value="64MB" />
        <param name="MaxBackupIndex" value="2" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="&lt;%-4d{DATE}&gt; &lt;%-5p&gt; &lt;%t&gt; &lt;%m&gt; %n" />
        </layout>
    </appender>

    <root>
        <priority value="INFO" />
        <appender-ref ref="FileAppender" />
        <appender-ref ref="ConsoleAppender" />
    </root>

</log4j:configuration>

我需要将此转换为log4j2并提出以下内容但我不确定这是否正确

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration>
    <!-- Console appender -->
    <Parameters>
        <param name="MaxBackupIndex" value="2"/>
    </Parameters>
    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT">
            <PatternLayout pattern="&lt;%-4d{DATE}&gt; &lt;%-4p&gt; &lt;%t&gt; &lt;%m&gt; %n"/>
        </Console>
        <!-- File appender -->
        <RollingFile name="DAILY_LOG" fileName="test/logs/app.log"
                     filePattern="app.log.%d{yyyy-MM-dd-hh-mm}.gz"
>
            <PatternLayout pattern="&lt;%-4d{DATE}&gt; &lt;%-5p&gt; &lt;%t&gt; &lt;%m&gt; %n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="64MB" />
                <TimeBasedTriggeringPolicy interval="1"/>
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="org.apache.log4j.xml" level="info"/>
        <Root level="info">
            <AppenderRef ref="ConsoleAppender"/>
            <AppenderRef ref="FileAppender"/>
        </Root>
    </Loggers>

    </Configuration>

任何人都知道我是否正确或在正确的轨道上?它似乎没有工作

1 个答案:

答案 0 :(得分:0)

如果你想每天滚动它,那么你就不应该使用带有小时和分钟的文件模式。但是,如果您确实希望文件包含小时和分钟,则使用CronTriggeringPolicy而不是TimeBasedTriggeringPolicy。