Skip to content

Log4j2 XML configuration structure

The XML configuration for Log4j2 is organized into a hierarchical structure defined within a root <configuration> element.^[600-developer__java__3-party__java-log4j2.md] This element accepts a monitorInterval attribute, which is used to specify the frequency (in seconds) that the configuration file should be checked for changes.^[600-developer__java__3-party__java-log4j2.md]

Properties

Global constants are defined within the <Properties> section.^[600-developer__java__3-party__java-log4j2.md] Individual properties are declared using the <Property> tag, utilizing a name attribute to define the key and the element content to define the value.^[600-developer__java__3-party__java-log4j2.md] These properties can then be referenced elsewhere in the configuration (e.g., in layouts) using the ${PROPERTY_NAME} syntax.^[600-developer__java__3-party__java-log4j2.md]

Appenders

The <Appenders> section contains the various destinations for log events, such as files or the console.^[600-developer__java__3-party__java-log4j2.md]

Common appender configurations include:

  • Console: Defined by the <Console> element, it typically includes a target attribute (e.g., SYSTEM_OUT) and a <PatternLayout> for formatting the output.^[600-developer__java__3-party__java-log4j2.md]
  • RollingFile: Defined by the <RollingFile> element, this appender writes to a file specified by the fileName attribute and creates backup files based on the filePattern (often using date codes like %d{yyyy-MM-dd}).^[600-developer__java__3-party__java-log4j2.md]
  • Policies: Rolling policies are configured within a <Policies> block.^[600-developer__java__3-party__java-log4j2.md] For example, <SizeBasedTriggeringPolicy> triggers a rollover when the file reaches a specified size.^[600-developer__java__3-party__java-log4j2.md]
  • Rollover Strategy: The <DefaultRolloverStrategy> determines how to handle old files during rollover, such as the max attribute which limits the number of retained files.^[600-developer__java__3-party__java-log4j2.md]

Loggers

The <Loggers> section defines the relationship between loggers in the code and the configured appenders.^[600-developer__java__3-party__java-log4j2.md]

Named Loggers

Specific loggers are defined using the <Logger> tag.^[600-developer__java__3-party__java-log4j2.md]

  • name: Corresponds to the name used when retrieving the logger (e.g., LoggerFactory.getLogger("main")).^[600-developer__java__3-party__java-log4j2.md]
  • additivity: A boolean attribute (often set to false) that determines whether the logger should propagate events to the parent Root logger.^[600-developer__java__3-party__java-log4j2.md]
  • AppenderRef: Nested within the logger, this element links the specific logger to an appender defined earlier via its ref attribute.^[600-developer__java__3-party__java-log4j2.md]

Root Logger

The <Root> element defines the global logging configuration.^[600-developer__java__3-party__java-log4j2.md] It typically includes a level attribute (e.g., all, debug, info) and accepts <AppenderRef> elements to route output to appenders like the console.^[600-developer__java__3-party__java-log4j2.md]

Sources

^[600-developer__java__3-party__java-log4j2.md]