Yes, that is correct.  The getRootLogger method is not included in the 
Configuration interface.  I created a unit test to verify the code worked and I 
should have pasted the code from the test.

Ralph

On May 18, 2013, at 7:26 PM, Eric Scheie wrote:

> I should add that I didn't find the getRootLogger method in the API.  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();
> 
> 
> Thanks,
> 
> -Eric
> 
> 
> On Sat, May 18, 2013 at 9:13 AM, Nick Williams <
> nicho...@nicholaswilliams.net> wrote:
> 
>> I agree, Scott.
>> 
>> On May 18, 2013, at 11:11 AM, Scott Deboy wrote:
>> 
>>> From looking at this, seems like config.getRootLoggerConfig() would be a
>>> better name.
>>> On May 17, 2013 10:40 PM, "Ralph Goers" <ralph.go...@dslextreme.com>
>> wrote:
>>> 
>>>> 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
>>>> 
>>>> 
>>>> On May 17, 2013, at 11:56 AM, Eric Scheie wrote:
>>>> 
>>>>> Here is a simple program that can reproduce my problem:
>>>>> 
>>>>> 
>>>>> import org.apache.logging.log4j.Level;
>>>>> import org.apache.logging.log4j.LogManager;
>>>>> import org.apache.logging.log4j.Logger;
>>>>> 
>>>>> 
>>>>> public class Main
>>>>> {
>>>>>  public static void main(String[] args)
>>>>>  {
>>>>>      Logger rootLogger =
>>>>> LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
>>>>>      Logger xLogger = LogManager.getLogger("X");
>>>>> 
>>>>>      xLogger.info("This is an INFO log message.");
>>>>> 
>>>>> 
>>>>> 
>> ((org.apache.logging.log4j.core.Logger)rootLogger).setLevel(Level.DEBUG);
>>>>> 
>>>>>      rootLogger.debug("This is a debug message from ROOT.");
>>>>>      xLogger.debug("This is a debug message from X.");
>>>>>  }
>>>>> 
>>>>> }
>>>>> 
>>>>> 
>>>>> log4j-api-2.0-beta5.jar and log4j-core-2.0-beta4.jar are the only jars
>> on
>>>>> the class path.  I'm using the same log4j2.xml file as above.
>>>>> 
>>>>> 
>>>>> When I run this program, I see the following output:
>>>>> 
>>>>> 
>>>>> 2013-05-17 11:50:54,192 INFO  [main] {} Main.main(14) - This is an INFO
>>>> log
>>>>> message.
>>>>> 
>>>>> 2013-05-17 11:50:54,194 DEBUG [main] {} Main.main(18) - This is a debug
>>>>> message from ROOT.
>>>>> 
>>>>> 
>>>>> The debug message from the X logger is missing.  Shouldn't the X logger
>>>> be
>>>>> inheriting the level from the Root Logger since an explicit level has
>> not
>>>>> been set?
>>>>> 
>>>>> 
>>>>> Thanks,
>>>>> 
>>>>> -Eric
>>>>> 
>>>>> 
>>>>> 
>>>>> 
>>>>> On Thu, May 16, 2013 at 11:49 PM, Ralph Goers <
>>>> ralph.go...@dslextreme.com>wrote:
>>>>> 
>>>>>> Can you provide a sample application that demonstrates this?
>>>>>> 
>>>>>> Ralph
>>>>>> 
>>>>>> On May 16, 2013, at 9:44 PM, Eric Scheie wrote:
>>>>>> 
>>>>>>> I'm developing a web application and would like to be able to
>>>> dynamically
>>>>>>> change log levels from within my application instead of editing the
>>>>>> log4j2
>>>>>>> configuration file.   I'm using log4j 2.0 beta5.
>>>>>>> 
>>>>>>> I'm currently setting the log level after a form submit using:
>>>>>>> 
>>>>>>> ((org.apache.logging.log4j.core.Logger)
>>>> LogManager.getLogger(LogManager.
>>>>>>> ROOT_LOGGER_NAME)).setLevel(Level.XXXXX);
>>>>>>> 
>>>>>>> 
>>>>>>> And when I call:
>>>>>>> 
>>>>>>> 
>>>>>>> ((org.apache.logging.log4j.core.Logger)
>>>> LogManager.getLogger(LogManager.
>>>>>>> ROOT_LOGGER_NAME)).getLevel()
>>>>>>> 
>>>>>>> 
>>>>>>> I get the level that I previously set as expected but I don't see the
>>>>>> level
>>>>>>> reflected in the logs.  For example, when changing the level to DEBUG
>>>> on
>>>>>>> the root logger I do not see debug messages written to the console.
>>>> If I
>>>>>>> change the level of the root logger in my log4j2.xml configuration
>>>> file I
>>>>>>> do see the debug messages written to the console.
>>>>>>> 
>>>>>>> 
>>>>>>> Here is my log4j2.xml file.
>>>>>>> 
>>>>>>> 
>>>>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>>>>> 
>>>>>>> <configuration status="DEBUG" monitorInterval="10">
>>>>>>> 
>>>>>>> 
>>>>>>> <appenders>
>>>>>>> 
>>>>>>>     <Console name="Console" target="SYSTEM_OUT">
>>>>>>> 
>>>>>>>         <PatternLayout pattern="%d %-5p [%t] %X %C{2}.%M(%L) -
>>>> %m%n"/>
>>>>>>> 
>>>>>>>     </Console>
>>>>>>> 
>>>>>>> </appenders>
>>>>>>> 
>>>>>>> 
>>>>>>> <loggers>
>>>>>>> 
>>>>>>>     <root level="INFO">
>>>>>>> 
>>>>>>>         <appender-ref ref="Console"/>
>>>>>>> 
>>>>>>>     </root>
>>>>>>> 
>>>>>>> </loggers>
>>>>>>> 
>>>>>>> </configuration>
>>>>>>> 
>>>>>>> 
>>>>>>> Any ideas I how I can change the root logger level after startup
>>>>>>> programmtically?  I've been searching the web but the only things
>> that
>>>>>> come
>>>>>>> up are related to log4j 1.2, I haven't found anything applicable to
>>>> log4j
>>>>>>> 2.0.
>>>>>>> 
>>>>>>> 
>>>>>>> Thanks,
>>>>>>> 
>>>>>>> -Eric
>>>>>> 
>>>>>> 
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
>>>>>> For additional commands, e-mail: log4j-user-h...@logging.apache.org
>>>>>> 
>>>>>> 
>>>> 
>>>> 
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
>> For additional commands, e-mail: log4j-user-h...@logging.apache.org
>> 
>> 


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

Reply via email to