Dynamically Adjust Log Levels with Arthas Logger and Log4j2

This guide explains how to use the Arthas logger command to change Java logger levels on‑the‑fly without restarting the JVM, demonstrates a complete Log4j2 XML configuration, and provides a runnable Java example that continuously emits logs at various levels.

FunTester
FunTester
FunTester
Dynamically Adjust Log Levels with Arthas Logger and Log4j2

The logger command in Arthas is designed for log‑related operations such as inspecting loggers, appenders, classloader information, log levels, code sources, and file names. Its most valuable feature is the ability to modify a logger’s level at runtime, allowing you to switch to debug for troubleshooting and back to info after the issue is resolved, all without restarting the JVM.

This capability assumes that the application already emits logs at the desired levels; if no debug statements exist, raising the level will have no effect. Arthas also offers additional commands for more advanced log manipulation, which will be covered in future posts.

The author’s project uses Log4j2, and although the XML configuration differs slightly from the official demo, the tutorial is based on Log4j2 and works equally well with Log4j.

Log4j2 configuration (XML) :

<?xml version="1.0" encoding="UTF-8"?>
<!-- Log level order: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<configuration status="WARN" monitorInterval="30">
    <!-- Define appenders -->
    <appenders>
        <!-- Console output -->
        <console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%-4p-> %m%n"/>
        </console>
        <!-- Rolling file for INFO and below -->
        <RollingFile name="RollingFileInfo" fileName="log/info.log"
                     filePattern="log/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%d{HH:mm:ss} %-4level %l - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="2 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="5"/>
        </RollingFile>
        <!-- Rolling file for WARN and above -->
        <RollingFile name="RollingFileWarn" fileName="log/warn.log"
                     filePattern="log/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">
            <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%d{HH:mm:ss} %-4level %l - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="2 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="5"/>
        </RollingFile>
    </appenders>
    <!-- Define loggers -->
    <loggers>
        <root level="info">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileInfo"/>
            <appender-ref ref="RollingFileWarn"/>
        </root>
    </loggers>
</configuration>

Demo Java code (uses the logger defined above):

package com.fun;

import com.fun.frame.httpclient.FanLibrary;
import org.slf4j.Logger;

public class TSSS extends FanLibrary {
    public static Logger logger = getLogger(TSSS.class);

    public static void main(String[] args) {
        while (true) {
            logger.debug("我是debug信息!");
            logger.info("我是info信息!");
            logger.warn("我是warn信息!");
            logger.error("我是error信息!");
            sleep(3000);
        }
    }
}

Running this program will continuously emit logs at debug, info, warn, and error levels every three seconds. By invoking arthas logger setLevel com.fun.TSSS DEBUG (or the appropriate logger name) you can instantly elevate the logging verbosity for troubleshooting, then revert to INFO once the issue is resolved.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaloggingArthaslog4j2Dynamic Log Level
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.