Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

log4j

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.

like image 1000
cons Avatar asked Aug 07 '09 10:08

cons


People also ask

Which log4j component is responsible for logging messages?

Appenders. Apache log4j provides Appender objects which are primarily responsible for printing logging messages to different destinations such as consoles, files, sockets, NT event logs, etc.

Where does log4j log to?

The Log4j logging settings are stored in the file app_data /conf/server/log4j. properties, where app_data is the application data folder. You can edit this file directly on the server or open it by clicking Settings > Logging.

What is the default log level in log4j?

Note that by default Log4j assigns the root logger to Level. ERROR .


1 Answers

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
like image 166
Jon Skeet Avatar answered Sep 25 '22 11:09

Jon Skeet