cons
cons

Reputation: 15313

log4j directs all log output to stdout even though it's not supposed to

In my log4j.properties I have:

log4j.rootLogger=DEBUG,stdout

log4j.logger.notRootLogger=DEBUG,somewhereelse

The appenders stdout and somewhereelse are both configured properly, stdout writes to the console and somewhereelse writes to a file.

In my code in each class either I set either:

static Logger log =  Logger.getLogger("notRootLogger);

^ When I don't want stuff going to the console.

-OR-

static Logger log = Logger.getRootLogger();

^ When I do.

What do I have to do in log4.properties to stop the things that are written to notRootLogger ending up in stdout? Is there some sort of inheritance of wherever the root logger writes to going on that needs to be turned off somehow?

I don't want to have to configure a logger for every single class individually that I just want to log to the console.

Upvotes: 10

Views: 14240

Answers (3)

ashutosh5111
ashutosh5111

Reputation: 11

If the properties of logger have been defined in java class, you can call logger.shutdown() method in the end, preferebly in the finally block to prohibit the additive nature of logger.

Upvotes: 0

cons
cons

Reputation: 15313

Hmm, should have read the short intro to log4j more carefully

log4j.additivity.notRootLogger=false

fixes it, because it inherits appenders from the loggers above it in the hierarchy, and the root logger is at the top of the hierarchy obviously.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499770

You need to set additivity = false, IIRC. From the log4j manual:

Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy. In other words, appenders are inherited additively from the logger hierarchy. For example, if a console appender is added to the root logger, then all enabled logging requests will at least print on the console. If in addition a file appender is added to a logger, say C, then enabled logging requests for C and C's children will print on a file and on the console. It is possible to override this default behavior so that appender accumulation is no longer additive by setting the additivity flag to false.

Try this:

log4j.rootLogger=DEBUG,stdout
log4j.logger.notRootLogger=DEBUG,somewhereelse
log4j.additivity.notRootLogger=false

Upvotes: 24

Related Questions