1. 程式人生 > >《Log4j2官方文件》從Log4j 1.x遷移

《Log4j2官方文件》從Log4j 1.x遷移

Sample 5 – AsyncAppender with Console and File

使用AsyncAppender的Log4j 1.x XML配置。

    1. <?xml version=“1.0” encoding=“UTF-8”?>
    2. <!DOCTYPE log4j:configuration PUBLIC “-//APACHE//DTD LOG4J 1.2//EN” “log4j.dtd”>
    3. <log4j:configuration xmlns:log4j=“http://jakarta.apache.org/log4j/” configDebug=“true”>
    4. <appender
      name=“ASYNC” class=“org.apache.log4j.AsyncAppender”>
    5. <appender-ref ref=“TEMP”/>
    6. <appender-ref ref=“CONSOLE”/>
    7. </appender>
    8. <appender name=“CONSOLE” class=“org.apache.log4j.ConsoleAppender”>
    9. <layout class=“org.apache.log4j.PatternLayout”>
    10. <param name=“ConversionPattern”
      value=“%d %-5p [%t] %C{2} (%F:%L) – %m%n”/>
    11. </layout>
    12. </appender>
    13. <appender name=“TEMP” class=“org.apache.log4j.FileAppender”>
    14. <param name=“File” value=“temp”/>
    15. <layout class=“org.apache.log4j.PatternLayout”>
    16. <param name=“ConversionPattern” value=“%d %-5p [%t] %C{2} (%F:%L) – %m%n”
      />
    17. </layout>
    18. </appender>
    19. <root>
    20. <priority value=“debug”/>
    21. <appender-ref ref=“ASYNC”/>
    22. </Root>
    23. </log4j:configuration>

Log4j 2 XML配置。需要注意的是,Async Appender需要在Appender引用的後面配置,這樣才能讓它正確的關閉。

    1. <?xml version=“1.0” encoding=“UTF-8”?>
    2. <Configuration status=“debug”>
    3. <Appenders>
    4. <Console name=“CONSOLE” target=“SYSTEM_OUT”>
    5. <PatternLayout pattern=“%d %-5p [%t] %C{2} (%F:%L) – %m%n”/>
    6. </Console>
    7. <File name=“TEMP” fileName=“temp”>
    8. <PatternLayout pattern=“%d %-5p [%t] %C{2} (%F:%L) – %m%n”/>
    9. </File>
    10. <Async name=“ASYNC”>
    11. <AppenderRef ref=“TEMP”/>
    12. <AppenderRef ref=“CONSOLE”/>
    13. </Async>
    14. </Appenders>
    15. <Loggers>
    16. <Root level=“debug”>
    17. <AppenderRef ref=“ASYNC”/>
    18. </Root>
    19. </Loggers>
    20. </Configuration>