How to set filter in the log4j.xml
Asked Answered
J

2

13

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>
Jell answered 28/1, 2011 at 5:13 Comment(0)
G
5

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

Gladi answered 28/1, 2011 at 5:21 Comment(2)
Between the last appender and the root tag. Also make sure to update the appender-ref with your own appender names.Gladi
can you send the non-xml format as well? is it like: log4j.logger.org.springframework=warnClaudioclaudius
C
1

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

Confined answered 28/1, 2011 at 5:19 Comment(2)
Using properties files are not encouraged anymore.Jell
Especially not when the OP asked about log4j.xml.Loisloise

© 2022 - 2024 — McMap. All rights reserved.