2012-10-12 193 views
3

我想問一些關於log4j的問題。我在activemq上有log4j的這個配置文件。我的問題是我想記錄每個類中的所有INFO級別的消息,但是我想將所有來自「TransportConnection」類的DEBUG級別消息記錄到不同的文件中,並且同時記錄所有更大的消息或者等於WARN級別,傳遞給rootLogger。log4j隔離某個級別

該配置的問題是,我在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 

回答

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」變量「軟件包名」變量,實施將是由特定的包過濾器相同。

在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>