Thomas M.
Thomas M.

Reputation: 1566

log4j: Change threshold for one appender based on class

I want to achieve the following:

All log-messages with TRACE or higher should be logged to log-trace.log
All log-messages with DEBUG or higher should be logged to log-debug.log

In general messages with DEBUG or higher should be logged to stdout, but:
  for Class de.foo only INFO or higher should be logged to stdout,
  for Class de.bar also TRACE and higher should be logged to stdout.

I manged to get the splitting into files to work:

## Root logger
log4j.rootLogger=trace,STDOUT,FILE_DEBUG,FILE_TRACE
## Appenders
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.Threshold=DEBUG
# File appenders
log4j.appender.FILE_DEBUG=org.apache.log4j.RollingFileAppender
log4j.appender.FILE_DEBUG.File=logs/log-debug.log
log4j.appender.FILE_DEBUG.Threshold=DEBUG

log4j.appender.FILE_TRACE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE_TRACE.File=logs/log-trace.log
log4j.appender.FILE_TRACE.Threshold=TRACE

# [..] Layouts

But if I try to change the debug-level for stdout, I also change what goes into the files, e.g.

log4j.logger.de.foo=info 

makes the TRACE messages from foo disappear from the trace-logfile. What I'm looking for is probably something like:

log4j.appender.FILE_DEBUG.de.foo.Threshold=DEBUG

but that doesn't work. Thanks for any help :)

Upvotes: 1

Views: 1765

Answers (2)

xeye
xeye

Reputation: 1256

not possible in log4j, use logback, it's superior anyway (btw with slf4j facade)

Upvotes: -1

Artem Shafranov
Artem Shafranov

Reputation: 2673

I think the correct approach will be to write custom filter for appender STDOUT. Thus you'll be able to set minimum level of console logs for particular packages. Here's an example of similar filter: log4j isolating certain level from a class.

Notice that you then should use XML configuration, because filters are unsupported in properties configuration.

Upvotes: 2

Related Questions