logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "SMITH, CURTIS" <cs0...@att.com>
Subject Log4j2: Programmatically setting log4j2 log level
Date Mon, 22 Jul 2013 14:14:43 GMT
>From a thread back in May:

My question to the below snips of the thread are;

My app has many Catagories (using 1.x terminology).  To setLevel in 1.x I had to getCurrentCatagories()
and iterate over the list setting level.

In 2.x, does the code below set all Appenders regardless of what Level they were set to in
log4j.xml?

loggerConfig.setLevel(Level.DEBUG);
ctx.updateLoggers();  

The next question is how to set the level of just one Appender (Logger??)  ?   Is there a
way to get the list / iterator of Appenders(Logger?)   like you could get a a list of Catagories
in 1.x?  

It appears that LogManager.getLogger("foo") you need to know the logger names at coding time.


Thanks, curt

=======

Ralph Goers said:
      >> No, the X Logger does not inherit its level from the root Logger. It inherits
its level from
      the root LoggerConfig.  See the picture at http://logging.apache.org/log4j/2.x/manual/architecture.html.

      The level you are modifying is brought into each Logger so that the level can be tested
very
      quickly.  That is why the note on the setLevel method says it is there primarily for
unit
      testing.

      To do what you are attempting below you would need to do:

      LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
      Configuration config = ctx.getConfiguration();
      LoggerConfig loggerConfig = config.getRootLogger();
      /* You could also specify the actual logger name as below and it will return the LoggerConfig
      used by the Logger.
          LoggerConfig loggerConfig = getLoggerConfig("X");
      */
      loggerConfig.setLevel(Level.DEBUG);
      ctx.updateLoggers();  // This causes all Loggers to refetch information from their LoggerConfig.

      Ralph

Eric Scheie said:
      >> This
      is the actual code I used.

        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);

        Configuration config = ctx.getConfiguration();

        LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.
      ROOT_LOGGER_NAME);

        loggerConfig.setLevel(level);

        ctx.updateLoggers();




---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message