tags:

views:

685

answers:

2

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.

+4  A: 

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
Jon Skeet
A: 

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.

cons
What's this got to do with optimization?
skaffman
In what way is it "premature optimisation"? It's not an "optimisation" at all. It so happens you want a different option from the default. If the default were for additivity to be false, that would mean that a good number of other people would have to explicitly set it to true. I suspect more people want it "true" than "false".
Jon Skeet
the expectation is pretty clear if you've read the log4j intro docs or used it
matt b