Log4j2¶
Log4j2 is a Java logging utility that supports configuration through XML files to define log behaviors, layouts, and appenders.^[600-developer__java__3-party__java-log4j2.md]
Configuration¶
Log4j2 configurations are typically defined in an XML file, often starting with a monitorInterval attribute that specifies how often the configuration file should be checked for changes (e.g., in seconds).^[600-developer__java__3-party__java-log4j2.md]
The configuration is generally structured into three main sections:
- Properties: Used to define reusable variables, such as a
LOG_PATTERNfor formatting log messages.^[600-developer__java__3-party__java-log4j2.md] - Appenders: Define where logs are sent, such as the console or files.^[600-developer__java__3-party__java-log4j2.md]
- Loggers: Define the relationship between loggers (often corresponding to Java classes or components) and appenders.^[600-developer__java__3-party__java-log4j2.md]
Appenders¶
Appenders are the destination components for logging events. The framework supports various types, including Console and RollingFile.^[600-developer__java__3-party__java-log4j2.md]
Console Appender¶
This appender outputs log messages to the standard output stream (usually the console).^[600-developer__java__3-party__java-log4j2.md]
Rolling File Appender¶
This appender writes logs to a file and supports rollover policies based on file size or time.^[600-developer__java__3-party__java-log4j2.md]
* Attributes: Configurable attributes include fileName (the current log file) and filePattern (the naming convention for rolled-over archives).^[600-developer__java__3-party__java-log4j2.md]
* Policies: A SizeBasedTriggeringPolicy can be configured to trigger rollover when the file reaches a specific size (e.g., 19500KB).^[600-developer__java__3-party__java-log4j2.md]
* Strategy: A DefaultRolloverStrategy determines the max number of archived files to keep (e.g., max="10").^[600-developer__java__3-party__java-log4j2.md]
Loggers¶
In the XML configuration, loggers are defined using the <Loggers> section, which contains specific <Logger> elements and a mandatory <Root> logger.^[600-developer__java__3-party__java-log4j2.md]
Logger Attributes¶
- name: Identifies the logger, often matching the name used in the Java code (e.g., "main", "bolt", "spout").^[600-developer__java__3-party__java-log4j2.md]
- additivity: A boolean attribute (often set to
false) that determines whether the logger should inherit appenders from its ancestors.^[600-developer__java__3-party__java-log4j2.md] - level: Defines the logging threshold (e.g.,
all,debug,info).^[600-developer__java__3-party__java-log4j2.md]
Appender References¶
Loggers use <AppenderRef> tags to link to specific appenders defined in the configuration.^[600-developer__java__3-party__java-log4j2.md]
Java Usage¶
In the application code, Logger instances are typically obtained using a factory method, often with a string name that corresponds to the logger configuration in XML.^[600-developer__java__3-party__java-log4j2.md]
public class LogUtils {
public static final Logger main = LoggerFactory.getLogger("main");
public static final Logger bolt = LoggerFactory.getLogger("bolt");
public static final Logger spout = LoggerFactory.getLogger("spout");
}
Related Concepts¶
- Java
- [[Logging]]
Sources¶
^[600-developer__java__3-party__java-log4j2.md]