2

I want to write only INFO logs to the logfile, below is my log4j configuration

log4j.properties file is :

log4j.rootLogger = INFO, FILE,
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=logs.out
log4j.appender.FILE.ImmediateFlush=true
log4j.appender.FILE.Threshold=INFO
log4j.appender.FILE.Append=true
log4j.appender.FILE.MaxFileSize=100KB
log4j.appender.FILE.MaxBackupIndex=2
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss} %5p - %m%n

Java file is:

import org.apache.log4j.Logger;

public class Log4jDemo {

    private static Logger log=Logger.getLogger(Logger.class.getName());

    public static void main(String[] args) {
        log.info("info");
        log.error("error");
        log.fatal("fatal");
        log.warn("warn");
    }
}

logs.out file is:

12 Apr 2016 15:38:35  INFO - info
12 Apr 2016 15:38:35 ERROR - error
12 Apr 2016 15:38:35 FATAL - fatal
12 Apr 2016 15:38:35  WARN - warn

Here I am getting logs in logs.out file are INFO, ERROR, FATAL, WARN but I want to write only INFO logs to logs.out file?

Anyone please suggest, how to achieve this objective?

my question is unique, it's not about log4j logging hierarchy order log4j logging hierarchy order

this shows the default configuration of log4j framework, but I want to implement the custom one, can you please help me out to resolve the issue.

Community
  • 1
  • 1
Girdhar Singh Rathore
  • 5,030
  • 7
  • 49
  • 67
  • So you don't want to see ERROR, FATAL, or WARN nor when they occurred? – Peter Lawrey Apr 12 '16 at 10:18
  • 1
    Possible duplicate of [log4j logging hierarchy order](http://stackoverflow.com/questions/7745885/log4j-logging-hierarchy-order) – Sanjeev Apr 12 '16 at 10:18
  • This [link](http://stackoverflow.com/questions/17442877/how-to-configure-log4j-to-log-only-info-messages-and-higher) might be in good help for you. – javadev Apr 12 '16 at 10:19
  • 2
    @javadev that's not my scenario, to show INFO and higher, i want only INFO logs – Girdhar Singh Rathore Apr 12 '16 at 10:21
  • You need a [filter](https://logging.apache.org/log4j/2.0/manual/filters.html) then ... Scroll down to ThresholdFilter. – Fildor Apr 12 '16 at 10:28
  • In Log4J older than 2.0 there is "LevelRangeFilter" (org.apache.log4j.varia.LevelRangeFilter) See also: http://stackoverflow.com/q/24932539/982149 – Fildor Apr 12 '16 at 10:38
  • 1
    But why would you ever want to hide _errors_? Program crashing and having error logs is a **good thing**. – Tunaki Apr 12 '16 at 13:57
  • 1
    @Tunaki Once I worked for a company where there were devs who blew out **any** log as ERROR. Things are not always used as intended ... so this is one reason to want this, that I have seen. OP may have completely different reasons, though. – Fildor Apr 12 '16 at 14:18

1 Answers1

2

I just found the Filter you are looking for: org.apache.log4j.varia.LevelMatchFilter

BUT :

"Note that filtering is only supported by the DOMConfigurator. The PropertyConfigurator does not support filters. "

(Source: https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/spi/Filter.html)

So you'll probably have to switch to XML-Configuration-File.

For Log4j2 there is ThresholdFilter as already mentioned in the comments.

Fildor
  • 14,510
  • 4
  • 35
  • 67