我想问一下log4j。我在activemq上有log4j的配置文件。我的问题是我想记录我所拥有的每个类的所有INFO级别消息,但我想将所有DEBUG级别消息从“TransportConnection”类记录到另一个文件,同时记录所有更大的消息或者等于WARN级别,到rootLogger。
此配置的问题是我从rootLogger中的“TransportConnection”类记录INFO级别消息。我想只将WARN及以上级别传递给rootLogger。
我不想将阈值设置为“out”appender,因为我想要来自其他类的INFO级别消息。
log4j.rootLogger=INFO,out
# Log these warnings
log4j.logger.org.apache.activemq.broker.BrokerRegistry=INFO
log4j.logger.org.apache.activemq.broker.TransportConnection=DEBUG,tc
# Standard logging
log4j.appender.out=org.apache.log4j.RollingFileAppender
log4j.appender.out.file=/var/lib/activemq/log/activemq.log
log4j.appender.out.maxFileSize=10240KB
log4j.appender.out.maxBackupIndex=100
log4j.appender.out.append=true
log4j.appender.out.layout=org.apache.log4j.PatternLayout
log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
# Transport Connections logging
log4j.appender.tc=org.apache.log4j.RollingFileAppender
log4j.appender.tc.file=/var/lib/activemq/log/tc.log
log4j.appender.tc.maxFileSize=10240KB
log4j.appender.tc.maxBackupIndex=100
log4j.appender.tc.append=true
log4j.appender.tc.layout=org.apache.log4j.PatternLayout
log4j.appender.tc.layout.ConversionPattern=%d [%t] %-5p %-30.30c{1} - %m%n
答案 0 :(得分:1)
您可以编写自己的自定义过滤器。我建议实现可能是这样的:
public class MinLevelForParticularClassFilter extends Filter {
private boolean acceptOnMatch = false;
private Level level;
private String className;
@Override
public int decide(LoggingEvent event) {
if (this.className != null && this.level != null) {
if (event.getLocationInformation().getClassName().startsWith(className)) {
// this is event for specified class
if (!event.getLevel().isGreaterOrEqual(this.level)) {
// level of event is less than specified level
return Filter.DENY;
}
}
}
if (acceptOnMatch) {
return Filter.ACCEPT;
} else {
return Filter.NEUTRAL;
}
}
public boolean isAcceptOnMatch() { return acceptOnMatch; }
public void setAcceptOnMatch(boolean acceptOnMatch) { this.acceptOnMatch = acceptOnMatch; }
public Level getLevel() { return level; }
public void setLevel(Level level) { this.level = level; }
public String getClassName() { return className; }
public void setClassName(String className) { this.className = className; }
}
请注意,如果将“className”变量更改为“packageName”变量,则特定包的过滤实现方式相同。
XML中的log4j配置(因为属性文件中的配置不支持过滤器):
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="out" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="activemq.log" />
<param name="MaxFileSize" value="10240KB" />
<param name="MaxBackupIndex" value="100" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%-15.15t] %-5p %-30.30c{1} - %m%n"/>
</layout>
<!-- Apply filter to appender that's destined for root logger -->
<filter class="com.foo.log4j.filters.MinLevelForParticularClassFilter">
<param name="Level" value="WARN" />
<param name="ClassName" value="org.apache.activemq.broker.TransportConnection" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<appender name="tc" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="tc.log" />
<param name="MaxFileSize" value="10240KB" />
<param name="MaxBackupIndex" value="100" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %-30.30c{1} - %m%n"/>
</layout>
</appender>
<logger name="org.apache.activemq.broker.BrokerRegistry">
<level value="INFO" />
</logger>
<logger name="org.apache.activemq.broker.TransportConnection">
<level value="DEBUG" />
<appender-ref ref="tc" />
</logger>
<root>
<priority value ="INFO" />
<appender-ref ref="out" />
</root>
</log4j:configuration>