Search code examples
javalogginglog4jruntime

Dynamically Changing log4j log level


What are the different approaches for changing the log4j log level dynamically, so that I will not have to redeploy the application. Will the changes be permanent in those cases?


Solution

  • Changing the log level is simple; modifying other portions of the configuration will pose a more in depth approach.

    LogManager.getRootLogger().setLevel(Level.DEBUG);
    

    The changes are permanent through the life cyle of the Logger. On reinitialization the configuration will be read and used as setting the level at runtime does not persist the level change.

    UPDATE: If you are using Log4j 2 you should remove the calls to setLevel per the documentation as this can be achieved via implementation classes.

    Calls to logger.setLevel() or similar methods are not supported in the API. Applications should remove these. Equivalent functionality is provided in the Log4j 2 implementation classes but may leave the application susceptible to changes in Log4j 2 internals.