Skip to content
JackSparrow414
Go back

Using Log4j2 (Part 2): Changing Log Levels at Runtime

Table of contents

Open Table of contents

Changing Log4j2 Log Levels at Runtime

In production, we generally configure logging at INFO, WARN, or above. If a business operation suddenly fails and the WARN logs provide no clues, it is useful to switch from WARN to DEBUG without restarting the server, so that we can inspect more logs and understand what is happening.

This requires the ability to adjust Log4j2 log levels at runtime.

Using JMX

Log4j2 officially provides JMX support, which makes configuration changes convenient.

Steps

  1. Start the application.

  2. Start JConsole and click the MBeans tab.

  3. Locate the Log4j2 MBean and use getConfigText to retrieve the current configuration. JConsole invoking the Log4j2 MBean getConfigText operation to retrieve logging configuration

  4. Access the application. INFO messages appear in the logs, showing that the application’s default level is currently INFO.

    INFO com.example.log4j2.controller.LogController - Info level log message
    WARN com.example.log4j2.controller.LogController - Warn level log message
    ERROR com.example.log4j2.controller.LogController - Error level log message
  5. Change the log level of a particular Logger. Here, we change the level of com.example.log4j2.controller.LogController to DEBUG.

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration>
        <Appenders>
            <Console name="Console" target="SYSTEM_OUT">
                <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %level %logger - %msg%n"/>
            </Console>
            <RollingFile name="MyFile" fileName="logs/app.log" immediateFlush="true"
                         filePattern="logs/$${date:yyyy-MM-dd}/app-%d{yyyy-MM-dd}-%i.log.gz">
                <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
                <JsonTemplateLayout eventTemplateUri="classpath:EcsLayout.json"/>
                <Policies>
                    <TimeBasedTriggeringPolicy/>
                    <SizeBasedTriggeringPolicy size = "10KB"/>
                </Policies>
                <DefaultRolloverStrategy fileIndex="nomax"/>
            </RollingFile>
        </Appenders>
        <Loggers>
            <Root level="info">
                <AppenderRef ref="Console"/>
            </Root>
            <Logger name="com.example.log4j2.controller" level="debug" >
                <AppenderRef ref="MyFile"/>
            </Logger>
        </Loggers>
    </Configuration>
  6. Call setConfigText through JConsole. Log4j2 MBean setConfigText operation in JConsole

  7. Access the application again. DEBUG messages now appear in the logs.

    DEBUG com.example.log4j2.controller.LogController - Debug level log message
    INFO com.example.log4j2.controller.LogController - Info level log message
    WARN com.example.log4j2.controller.LogController - Warn level log message
    ERROR com.example.log4j2.controller.LogController - Error level log message
  8. You can also call getConfigText again to verify that it returns the configuration you just changed.

Using Code and a Web Page

We can change the level of a particular Logger programmatically by having a web page call an HTTP endpoint. The following code comes from the official documentation.

// org.apache.logging.log4j.core.config.Configurator;
Configurator.setLevel("com.example.Foo", Level.DEBUG);

// You can also set the root logger:
Configurator.setRootLevel(Level.DEBUG);

A more detailed discussion of this approach on Stack Overflow

Comparing the Two Approaches

If you only need to adjust logger levels and want a DevOps-friendly approach, I recommend the programmatic option. If you want to change other configuration settings at runtime, I recommend JMX.

Other Resources


Share this post:

Continue this series

Understanding and Using Log4j2

  1. Using Log4j2 (Part 1)
  2. Using Log4j2 (Part 2): Changing Log Levels at RuntimeYou are here
  3. Using Log4j2 (Part 3): Different Configurations for Different Environments

Comments

Questions, corrections, and experiences are welcome. Sign in with GitHub to comment; both language versions share this discussion.

Comments are available on the live site only.