我认为这段代码很好地解释了我想要做的事情
package us.benanderson;
public class MyClass {
private static final Logger LOG = LogManager.getLogger(MyClass.class);
...
// within a method
LOG.debug("only output to appFile");
LOG.error("output to both appFile and errorFile");
这就是我猜应该是我的配置
<Loggers>
<Logger name="us.benanderson" level="debug" additivity="true">
<AppenderRef ref="appFile" />
</Logger>
<Root level="error">
<AppenderRef ref="errorFile" />
</Root>
</Loggers>
但是,我发现当additivity =&#34; true&#34;时,调试消息也会输出到errorFile。当可加性=&#34;假&#34;没有输出到errorFile。有没有办法做我想做的事情?
答案 0 :(得分:6)
我认为实现你想要的最简单方法是直接在appender-ref上放一个级别。这样就无需在配置中使用命名记录器,从而简化了配置。
如果要将某个包的日志事件重定向到单独的appender,则命名记录器仍然有用。
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="appFile" fileName="logs/appFile.log" append="true">
<PatternLayout pattern="%-5p %d{ABSOLUTE} [%t] %c - %m%n" />
</File>
<File name="errorFile" fileName="logs/errorFile.log" append="true">
<PatternLayout pattern="%-5p %d{ABSOLUTE} [%t] %c - %m%n" />
</File>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="appFile" level="debug" />
<AppenderRef ref="errorFile" level="error" />
</Root>
</Loggers>
</Configuration>
如果要进行更多不寻常的过滤,阈值过滤器和过滤器组合尤其有用。例如,如果您只想将INFO和WARN级别事件发送到某个appender,不包括TRACE / DEBUG,还要排除ERROR和FATAL级别事件(!),您可以这样做:
<Console name="only-info-warn">
<PatternLayout pattern="%-5p %c %message %n" />
<Filters>
<!-- First deny error and fatal messages -->
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>
<!-- Then accept info, warn, error, fatal and deny debug/trace -->
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
</Console>
答案 1 :(得分:3)
使用过滤器。
这是一个将所有内容输出到C:/Logs/Log.log
的配置,另外只将调试输出到C:/Logs/Debug.log
:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
<appender name="general-out" class="org.apache.log4j.FileAppender">
<param name="File" value="C:/Logs/Log.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1} : %m%n"/>
</layout>
</appender>
<appender name="debug-out" class="org.apache.log4j.FileAppender">
<param name="File" value="C:/Logs/Debug.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1} : %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>
<root>
<appender-ref ref="debug-out" />
<appender-ref ref="general-out" />
</root>
</log4j:configuration>
这里的关键部分是:
<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"/>
这允许使用debug
记录的内容,然后拒绝其他任何内容。
答案 2 :(得分:2)
由于您使用的是Log4j 2,因此您可能需要使用ThresholdFilter
。使用此过滤器,您的配置文件可以如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="appFile" fileName="logs/appFile.log" append="true">
<PatternLayout pattern="%d %-5p (%c.java:%L).%M - %m%n" />
<ThresholdFilter level="DEBUG" />
</File>
<File name="errorFile" fileName="logs/errorFile.log" append="true">
<PatternLayout pattern="%d %-5p (%c.java:%L).%M - %m%n" />
<ThresholdFilter level="ERROR" />
</File>
</Appenders>
<Loggers>
<Logger name="us.benanderson" level="debug">
<AppenderRef ref="appFile" />
</Logger>
<Root level="error">
<AppenderRef ref="errorFile" />
</Root>
</Loggers>
</Configuration>
答案 3 :(得分:1)
根据您的要求;不应该配置如下(带注释): -
<Loggers>
<!-- Do not trickle down 'debug' logs to parent logger using additivity as false; keeping them only in appFile -->
<Logger name="us.benanderson" level="debug" additivity="false">
<AppenderRef ref="appFile" />
</Logger>
<!-- Error messages by default go to both appfile and errorFile -->
<Root level="error">
<AppenderRef ref="errorFile" />
<AppenderRef ref="appFile" />
</Root>
</Loggers>
如果您特别担心来自&#34; us.benanderson&#34;的错误日志包;您可以尝试使用单独的记录器来查找错误&#39;水平。所以配置如下: -
<Loggers>
<Logger name="us.benanderson" level="debug" additivity="false">
<AppenderRef ref="appFile" />
</Logger>
<!-- Not specifying additivity here would default to 'true'; hence the error logs would also be relayed to the root/errorFile -->
<Logger name="us.benanderson" level="error">
<AppenderRef ref="appFile" />
</Logger>
<Root level="error">
<AppenderRef ref="errorFile" />
</Root>
</Loggers>