我正在尝试在我的Java应用中使用log4j
。我只需要将DEBUG
日志写入debug.log文件,只有INFO
日志记录到info.log文件,只有ERROR
日志记录到error.log文件。我设法将调试和错误日志写入适当的文件,但信息不起作用。我无法理解为什么它不起作用。我想请求您帮助是否也可以同时将调试和信息日志写入控制台。
的 RESULT
为每个级别的日志创建3个文件(debug.log,info.log和error.log) DEBUG日志写入debug.log,ERROR日志写入error.log,但INFO日志既不写入info.log也不写入debug.log或error.log!
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- only DEBUG Appender -->
<appender name="file1" class="org.apache.log4j.FileAppender">
<param name="File" value="D:\\debugFile.log "/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-4r %d{dd MMM yyyy HH:mm:ss} [%t] %-5p %c %x - %m%n"/>
</layout>
<!-- Use LevelMatchFilter to write only DEBUG messages to error.log -->
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="debug" />
<param name="AcceptOnMatch" value="true"/>
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<!-- only INFO Appender -->
<appender name="file2" class="org.apache.log4j.FileAppender">
<param name="File" value="D:\\infoFile.log "/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-4r %d{dd MMM yyyy HH:mm:ss} [%t] %-5p %c %x - %m%n"/>
</layout>
<!-- Use LevelMatchFilter to write only INFO messages to error.log -->
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="info" />
<param name="AcceptOnMatch" value="true"/>
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<!-- only ERROR Appender -->
<appender name="file3" class="org.apache.log4j.FileAppender">
<param name="File" value="D:\\errorFile.log "/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-4r %d{dd MMM yyyy HH:mm:ss} [%t] %-5p %c %x - %m%n"/>
</layout>
<!-- Use LevelMatchFilter to write only ERROR messages to error.log -->
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="error" />
<param name="AcceptOnMatch" value="true"/>
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<root>
<level value="info"/>
<appender-ref ref="file1" /> <!-- sadece DEBUG -->
<appender-ref ref="file2" /> <!-- sadece INFO -->
<appender-ref ref="file3" /> <!-- sadece ERROR -->
</root>
</log4j:configuration>
答案 0 :(得分:0)
我略微修改了它,效果很好
<?xml version="1.0" encoding="UTF-8"?>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<param name="target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<appender name="info" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="INFO" />
<param name="MaxFileSize" value="3KB" />
<param name="MaxBackupIndex" value="10" />
<param name="File" value="D:\\info.txt" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM-dd-yyyy HH:mm:ss:SSS} %-5p %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="info" />
<param name="AcceptOnMatch" value="true"/>
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<appender name="debug" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="DEBUG" />
<param name="MaxFileSize" value="3KB" />
<param name="MaxBackupIndex" value="10" />
<param name="File" value="D:\\debug.txt" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM-dd-yyyy HH:mm:ss:SSS} %-5p %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="debug" />
<param name="AcceptOnMatch" value="true"/>
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<appender name="error" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="ERROR" />
<param name="MaxFileSize" value="3KB" />
<param name="MaxBackupIndex" value="10" />
<param name="File" value="D:\\error.txt" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM-dd-yyyy HH:mm:ss:SSS} %-5p %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="error" />
<param name="AcceptOnMatch" value="true"/>
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<root>
<level value="all"/>
<appender-ref ref="info" />
<appender-ref ref="debug" />
<appender-ref ref="error" />
</root>