3

I am using log4j for one of my projects and I know it is possible to set thresholds for each appender.

My objective is to log only INFO messages into a file and only ERROR messages in another file.

The problem of using threshold's for this is that if i set a appender threshold to INFO and the other to ERROR, when I log an ERROR message, that message goes to both files (appenders).

How can I achieve this?

Thanks in advance

RedEagle
  • 4,418
  • 9
  • 41
  • 64
  • Is this what you want? http://stackoverflow.com/questions/728295/creating-multiple-log-files-of-different-content-with-log4j – donnior Jan 03 '12 at 17:22

3 Answers3

6

We use differents logger for different levels using:

log4j.appender.XXXTracking.filter.LevelToMatch=INFO

with the option filter.LevelToMatch you could write the level that you want. This in the log4j.properties

jenaiz
  • 547
  • 2
  • 15
2

You'll want to use org.apache.log4j.varia.LevelMatchFilter

<filter class="org.apache.log4j.varia.LevelMatchFilter">
            <param name="LevelToMatch" value="ERROR" />
            <param name="AcceptOnMatch" value="true" />
        </filter>
smp7d
  • 4,947
  • 2
  • 26
  • 48
0

Thanks for the response guys,

What i was doing wrong was that I was trying to define the filter on a properties file. Since the use of this is only suported on an XML configuration file, I have changed to an XML file.

To have exactly what I needed I also had to add a DenyAllFilter filter type:

        <filter class="org.apache.log4j.varia.LevelMatchFilter"> 
            <param name="LevelToMatch" value="INFO" /> 
            <param name="AcceptOnMatch" value="true" />                        
        </filter>
        <filter class="org.apache.log4j.varia.DenyAllFilter" />

Thanks for the help guys

RedEagle
  • 4,418
  • 9
  • 41
  • 64
  • Filters are supported in properties file, see javadoc for [PropertyConfigurator](http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html). Correct syntax is (notice ID): `log4j.appender.appenderName.filter.ID=fully.qualified.name.of.filter.class log4j.appender.appenderName.filter.ID.option1=value1 log4j.appender.appenderName.filter.ID.optionN=valueN` – rozky Feb 15 '12 at 11:21