Mircea Lemnaru created LOG4J2-3334:
--------------------------------------

             Summary: Configurator.setLevel adding an incomplete LoggerConfig
                 Key: LOG4J2-3334
                 URL: https://issues.apache.org/jira/browse/LOG4J2-3334
             Project: Log4j 2
          Issue Type: Bug
          Components: Core
    Affects Versions: 2.17.1
            Reporter: Mircea Lemnaru


I am trying to set the log levels for my application programatically and I was 
using the method: *Configurator.setLevel*

 
{code:java}
private static boolean setLevel(final String loggerName, final Level level, 
final Configuration config) {
    boolean set;
    LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
    if (!loggerName.equals(loggerConfig.getName())) {
        // TODO Should additivity be inherited?
        loggerConfig = new LoggerConfig(loggerName, level, true);
        config.addLogger(loggerName, loggerConfig);
        loggerConfig.setLevel(level);
        set = true;
    } else {
        set = setLevel(loggerConfig, level);
    }
    return set;
} {code}
In my applicationI have the following loggers structure:

 

*com.myapp.service* -> set on TRACE from XML config

Then I have the class: com.myapp.service.common.ServiceUtils with a logger 
defined for it like this: {{Logger log = 
LogManager.getLogger(this.getClass());}}

And in this class, a bunch of log statements on *TRACE*

Then I am trying to set the log level to *INFO* for the whole 
com.myapp.service.common package. To suppress the *TRACE* logs for example.

If I do this , Configurator will do the following:
 # will try to fetch a LoggerConfig for the *common* package
 # because there is nothing defined for that package only for the parent ... it 
will return the config for that package
 # because the names differ it will create a new LogConfig with the new logger 
name and level but {color:#FF0000}without any appenders or appenders refs{color}
 # then it will add this to the config and reload loggers

At this moment the logs from the end class are not printed anymore.

Now , I again set the level for com.myapp.service.common logger , but this time 
to {*}TRACE{*}. In order to re-enable the logs.

Still , I can't see anything in the logs.

After investigating it seems that adding the LoggerConfig without the appenders 
copied from parent , is causing this behaviour.

If for example I change the setLevel method to also add the appender refs and 
appenders when creating the LoggerConfig object , everything is good and 
logging works as expected.

 Thank you



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to