SoFunction
Updated on 2025-04-24

Three ways to set the Log4j2 monitoring interval

introduction

In Log4j2, you can specify the time interval for monitoring configuration files to change by setting the monitorInterval property in the configuration file. This property tells Log4j2 how many seconds to check whether the configuration file has changed. If there is any change, the configuration will be reloaded automatically. The following are specific settings and examples:

1. XML configuration file example

Add the monitorInterval property to the root <Configuration> element of the configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

In this example, monitorInterval="30" means Log4j2 checks whether the configuration file has changed every 30 seconds. Log4j2 will automatically reload the configuration if the configuration file changes.

2. JSON configuration file example

In the configuration file, you can set the monitoring interval by adding the "monitorInterval" property to the configuration object:

{
  "configuration": {
    "status": "WARN",
    "monitorInterval": 30,
    "appenders": {
      "console": {
        "type": "Console",
        "target": "SYSTEM_OUT",
        "PatternLayout": {
          "pattern": "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"
        }
      }
    },
    "loggers": {
      "root": {
        "level": "info",
        "appenderRefs": [
          {
            "ref": "console"
          }
        ]
      }
    }
  }
}

3. YAML configuration file example

existIn the configuration file, you can use it inconfigurationAdd to the objectmonitorIntervalProperties to set monitoring interval:

configuration:
  status: WARN
  monitorInterval: 30
  appenders:
    console:
      type: Console
      target: SYSTEM_OUT
      PatternLayout:
        pattern: "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"
  loggers:
    root:
      level: info
      appenderRefs:
        - ref: console

4. Things to note

  • Monitoring interval unitsmonitorIntervalThe unit is seconds.
  • Minimum interval:Log4j2 RequirementsmonitorIntervalThe value of  is at least 1 second.
  • Performance impact: Although monitoring configuration file changes provides some convenience, frequent file checks may have a slight impact on performance. Therefore, it is recommended to set the monitoring interval reasonably according to actual needs.
  • File change detection:Log4j2 determines whether the file has changed by detecting the last-modified time of the configuration file. If the file system or editing tool does not update the last modification time of the file correctly, Log4j2 may not detect changes.

With these methods, you can easily set the monitoring interval for Log4j2 so that it automatically reloads the configuration when the configuration file changes without restarting the application.

This is the end of this article about three methods to set the Log4j2 monitoring interval. For more information about setting the Log4j2 monitoring interval, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!