Logging Different Levels and Content with Log4j
Question:
Can log4j be configured to direct different logging levels to separate appenders? The goal is to create multiple log files with specific content, such as a main log catching all INFO and above messages, while another log captures DEBUG messages for only a selected group of classes.
Answer:
Configuring Log4j for Multiple Log Files
Log4j allows customization of logging to multiple destinations through appenders. To achieve the desired configuration:
Example Configuration:
log4j.rootLogger=QuietAppender, LoudAppender, TRACE # setup main log log4j.appender.QuietAppender=org.apache.log4j.RollingFileAppender log4j.appender.QuietAppender.Threshold=INFO log4j.appender.QuietAppender.File=quiet.log # setup specific log log4j.appender.LoudAppender=org.apache.log4j.RollingFileAppender log4j.appender.LoudAppender.Threshold=DEBUG log4j.appender.LoudAppender.File=loud.log # set logging level for specific classes log4j.logger.com.yourpackage.yourclazz=TRACE
With this configuration, the 'quiet.log' will contain all INFO and above messages for all classes, while 'loud.log' will only contain DEBUG messages for the specified class 'yourclazz'.
The above is the detailed content of How can I configure Log4j to send different logging levels to separate appenders and log files?. For more information, please follow other related articles on the PHP Chinese website!