Search code examples
javalogginglog4jconsoleappender

log4j - Why doesn't my appender show message?


At my work, I have inherited of the maintenance of a satandalone application.
The following code configures Log4J but no messages can be seen on the console.

    LogManager.resetConfiguration();

    PatternLayout layout = new PatternLayout();
    layout.setConversionPattern("RECORD-BACKEND / (%-5p) %m (%F:%L)%n");

    ConsoleAppender stderr = new ConsoleAppender(); 
    stderr.setTarget(ConsoleAppender.SYSTEM_ERR);
    stderr.setLayout(layout);
    stderr.addFilter(new CurrentThreadLogFilter());
    stderr.setThreshold(Level.INFO);
    stderr.activateOptions();

    Logger loggerRECORD = getLoggerRECORD();
    loggerRECORD.setAdditivity(false);
    loggerRECORD.addAppender(stderr);

    Logger root = Logger.getRootLogger();
    root.setLevel(Level.WARN);
    root.addAppender(stderr);

    // some lines forward ...

    loggerRECORD.info("Process starting...");

What am I missing ?


Solution

  • Thanks to AlexR, here how I solved my problem:

        LogManager.resetConfiguration();
    
        PatternLayout layout = new PatternLayout();
        layout.setConversionPattern("RECORD-BACKEND / (%-5p) %m (%F:%L)%n");
    
        ConsoleAppender stderr = new ConsoleAppender(); 
        stderr.setTarget(ConsoleAppender.SYSTEM_ERR);
        stderr.setLayout(layout);
        stderr.addFilter(new CurrentThreadLogFilter());
        stderr.setThreshold(Level.INFO);
        stderr.activateOptions();
    
        Logger loggerRECORD = getLoggerRECORD();
        loggerRECORD.setLevel( /* get Log Level from env. */ );
        loggerRECORD.setAdditivity(false);
        loggerRECORD.addAppender(stderr);
    
        Logger root = Logger.getRootLogger();
        root.setLevel(Level.WARN);
        root.addAppender(stderr);
    
        // some lines forward ...
    
        loggerRECORD.info("Process starting...");
    

    Since additivity is set to false for loggerRECORD, the appender sucessfully print any message from INFO to ERROR.