Log4j | Updating the Log Level for the Appender

2019-02-23 01:16发布

问题:

My log4j.properties file -

log4j.rootLogger=INFO, stdout

# =============== console output appender =====================
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{HH:mm:ss}] %5p: [%c{1}] %m%n

# =================== common logging =========================
# The log level for all classes that are not configured below.
log4j.logger.petascope=INFO

log4j.logger.petascope.wcps=DEBUG
log4j.logger.petascope.wcst=DEBUG
log4j.logger.petascope.wcs=DEBUG
log4j.logger.petascope.wcs2=TRACE

I want to display even DEBUG and TRACE messages on stdout, so I changed the following line

   log4j.rootLogger=TRACE, stdout

But I don't see any changes when I view the logs echoes on Tomcat Console, I still see only INFO, WARN ... messages.

回答1:

Well, even if you have your root logger as TRACE, your log4j.logger.petascope (pointing to INFO) will override the default root logger's TRACE for petascope.* packages, which is the reason you are not seeing any DEBUG and TRACE.

To keep things simple, try this... set the root logger to trace:-

log4j.rootLogger=TRACE, stdout

Then, comment out the following lines:-

#log4j.logger.petascope=INFO
#log4j.logger.petascope.wcps=DEBUG
#log4j.logger.petascope.wcst=DEBUG
#log4j.logger.petascope.wcs=DEBUG
#log4j.logger.petascope.wcs2=TRACE

Let me know if that works for you.



回答2:

Try this:

log4j.appender.stdout.Threshold=TRACE

(as described in another StackOverflow question).



标签: java log4j