Java Mailing List Archive

http://www.java2.5341.com/

Home » Log4J Users List »

Programmatically setting log4j2 log level

Eric Scheie

2013-05-17

Replies: Find Java Web Hosting

Author LoginPost Reply
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
©2008 java2.5341.com - Jax Systems, LLC, U.S.A.