Amit Patil
Amit Patil

Reputation: 73

log4j configuration level error

My log4j configuration is as follows

log4j.rootLogger=INFO, CA, FA, DA

#Console Appender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

#File Appender
log4j.appender.FA=org.apache.log4j.FileAppender
log4j.appender.FA.File=/home/admin/logs/sysout.log
log4j.appender.FA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.FA.Threshold = WARN



#File Appender 2
log4j.appender.DA=org.apache.log4j.FileAppender
log4j.appender.DA.File=/home/admin/logs/debug.log
log4j.appender.DA.layout=org.apache.log4j.PatternLayout
log4j.appender.DA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.DA.Threshold = TRACE

My understading is

  1. INFO will be logged to console
  2. WARN will be logged to sysout.log
  3. TRACE will logged to debug.log

But WARN is getting logged to both debug.log and sysout.log. Also TRACE is not logging in any of the file.

Console is having TRACE and WARN both.

Can you please tell me what am I doing wrong

Upvotes: 1

Views: 348

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122414

You need to separate the logger and appender concepts in your mind.

For the three appenders, remember that the threshold is the lowest level of message that the appender will process. An appender will process messages at its threshold level or any higher level.

CA has no threshold set, so it will log all messages that are sent to it regardless of level. Similarly DA has a threshold of TRACE so it will also log everything that is sent to it (since TRACE is the lowest level). FA has a threshold of WARN so it will filter out any messages at levels below WARN - it will contain only WARN, ERROR and FATAL messages.

The important part of that previous paragraph is "all messages that are sent to it". Since you have configured your root logger with a level of INFO and have not configured any specific loggers to a lower level, only messages at INFO and above will be sent to the appenders - DEBUG and TRACE messages will be silently dropped. This is why you see no TRACE output in any of your loggers.

Upvotes: 1

Related Questions