1. 程式人生 > >log4j輸出到控制檯和檔案

log4j輸出到控制檯和檔案

log4j的xml配置檔案改為

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//LOGGER"
"http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <!-- 輸出日誌到控制檯 ConsoleAppender -->
    <appender name="console" class
="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%p:%d{dd HH:mm:ss,SSS\}ms- [%c{1}]%L行-%m%n" /> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMin" value="ALL"/> <param name="LevelMax" value="FATAL"/> </filter> <filter class
="org.apache.log4j.varia.StringMatchFilter"> <param name="StringToMatch" value="Configkeeper" /> <param name="AcceptOnMatch" value="false" /> </filter> </appender> <appender name="File" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="D:/log4j/production.log" /> <param name="Append" value="true"/> <param name="MaxFileSize" value="1MB"/> <param name="MaxBackupIndex" value="20"/> <layout class
="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5p: %c - %m%n" /> </layout> </appender> <!--開啟DEBUG級別日誌--> <root> <level value="DEBUG"/> <appender-ref ref="console" /> <appender-ref ref="File" /> </root> </log4j:configuration>