0

I want to generate logging in two logging file with different logging level using log4j config file.

For example usual.log with INFO level and debug.log with DEBUG level

Note: I already referred link1 and Link2. I do not want to change existing java code so above links are not related to my question.

Community
  • 1
  • 1
Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74

1 Answers1

3

I assume you want debug.log to include INFO as well as DEBUG.

log4j.appender.usual=org.apache.log4j.DailyRollingFileAppender
log4j.appender.usual.DatePattern=${roll.pattern.daily}
log4j.appender.usual.layout=org.apache.log4j.PatternLayout
log4j.appender.usual.layout.ConversionPattern=%d{${datestamp}} [%t] %-5p %C{2} - %m%n
log4j.appender.usual.File=usual.log
log4j.appender.usual.Threshold=INFO

log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.DatePattern=${roll.pattern.daily}
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=%d{${datestamp}} [%t] %-5p %C{2} - %m%n
log4j.appender.debug.File=debug.log
log4j.appender.debug.Threshold=DEBUG

log4j.logger.customlogger=DEBUG, usual, debug

The key is the Threshold filter for the appenders themselves. You can send all the logs to multiple appenders and have them filtered there.

The syntax above might be a touch off though, I'm more used to the XML configuration.

Grexis
  • 1,502
  • 12
  • 15