Dynamically adjust log levels at runtime

更新时间:
复制 MD 格式

In certain scenarios, you may need to dynamically adjust log levels. This helps you output more log information to troubleshoot online issues or reduce the performance impact of logging. The Feature Switch lets you modify log levels while your application is running. You can adjust the log level at any time for different scenarios to obtain more useful log information.

Background information

When you develop Java applications, you often use various logging frameworks. You typically set a default log level to avoid outputting unnecessary information during normal runtime. When the application is running online, you may need to dynamically adjust the log level for specific scenarios.

Note
  • Supported logging frameworks: Log4j, Log4j2, and Logback.
  • Spring Boot and SDK version requirements: Version 1.0.3 or later.

Procedure

  1. Connect your application using Spring Boot or the Java SDK. For more information, see Connect using Spring Boot Starter and Connect using the SDK.
  2. Log on to the AHAS console. In the upper-left corner of the page, select a region.
  3. In the navigation pane on the left, choose Feature Switch. On the Application List page, click the resource card of the target application to open its Switch List page.
  4. On the Switch List page, search for the SYSTEM_LOG_CONFIG switch, which is the log level switch.
  5. In the Actions column, click Global Push or Single Machine Push. Enter the log runtime configuration in the <loggerName,loggerLevel> format, and then click Global Push or Single Machine Push again. This modifies the log runtime level for all machines or a single machine.
    Push value format: The Key is the LoggerName, and the Value is the log level. To modify the global log level, set LoggerName to root.
    {
     "root": "ERROR"
    }