How to set filter in the log4j.xml

2020-03-01 08:32发布

问题:

HI,

In our web application it prints all the logs are printed like Spring and JSF jars files. Which not required for us. How can I set in the log4j.xml file to filter only for our application?

Updated

The following is the configuration we are using for the logging. Can you please tell me whether it is correct? Where I have to add the logger element in the file?

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!-- Log4j Configuration file to log the messages into different logfiles -->
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

  <appender name="DEBUG" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="D:/logs/DEBUGFILE.log"/>
     <param name="Append" value="true"/>
     <param name="MaxFileSize" value="500KB"/>
     <param name="MaxBackupIndex" value="2"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
     <param name="LevelMin" value="DEBUG" />
      <param name="LevelMax" value="DEBUG" />
    </filter>
  </appender>

  <appender name="INFO" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="D:/logs/AUDIT_TRAIL.log"/>
    <param name="Append" value="true"/>
     <param name="MaxFileSize" value="500KB"/>
     <param name="MaxBackupIndex" value="2"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
      <param name="LevelMin" value="INFO" />
      <param name="LevelMax" value="INFO" />
    </filter>
  </appender>

  <appender name="ERROR" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="D:/logs/ERRORFILE.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="500KB"/>
     <param name="MaxBackupIndex" value="2"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
    </layout>
     <filter class="org.apache.log4j.varia.LevelRangeFilter">
      <param name="LevelMin" value="ERROR" />
      <param name="LevelMax" value="ERROR" />
    </filter>
  </appender>

  <appender name="FATEL" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="D:/logs/FATELFILE.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="500KB"/>
     <param name="MaxBackupIndex" value="2"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
    </layout>
     <filter class="org.apache.log4j.varia.LevelRangeFilter">
      <param name="LevelMin" value="FATAL" />
      <param name="LevelMax" value="FATAL" />
    </filter>
  </appender>

  <root>
    <priority value="debug"/>
      <appender-ref ref="DEBUG"/>
    <appender-ref ref="INFO"/>
    <appender-ref ref="ERROR"/>
    <appender-ref ref="FATEL"/>
  </root>

</log4j:configuration>

回答1:

Log only warning for Spring :

<logger name="org.springframework">
    <level value="warn"/>
    <appender-ref ref="socket"/>
    <appender-ref ref="stdout"/>
</logger>

Same for JSF with the jsf package name... javax.faces, org.apache.myfaces, com.sun.faces etc... also, the appender-ref refers to your appenders...



回答2:

You need to define the own logger and appender for your application. For example, if you need to log the trace message in com.xyz.http.RequestFilter:

log4j.com.xyz.http.RequestFilter=TRACE, TRACE_APPEND

log4j.appender.TRACE_APPEND=org.apache.log4j.RollingFileAppender
log4j.appender.TRACE_APPEND.File=example.log

log4j.appender.TRACE_APPEND.MaxFileSize=100KB
log4j.appender.TRACE_APPEND.MaxBackupIndex=1

log4j.appender.TRACE_APPEND.layout=org.apache.log4j.PatternLayout
log4j.appender.TRACE_APPEND.layout.ConversionPattern=%p %t %c - %m%n

See more at log4j official manual http://logging.apache.org/log4j/1.2/manual.html