Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Log4j rootLogger not filtering log events according to event level?

Why is the Log4j rootLogger in my application not filtering log events according to level? In my log4j.properties, I have several loggers:

log4j.rootLogger=info,stdout
log4j.logger.com.name.myapp=debug,myapp
log4j.logger.org.castor=debug,castor
log4j.logger.org.exolab.castor=debug,castor
log4j.logger.org.hibernate=debug,hibernate
log4j.logger.org.springframework=debug,spring

Each of the loggers receive and record numerous log events at levels DEBUG and above, which is what I expect and desire. The rootLogger, however, despite being set to level INFO, is displaying all of these events, too, including the DEBUG events, which is not what I expect and not what I desire. Instead, I would expect it to filter the DEBUG events, but display only the events at level INFO and higher (WARN, ERROR, and FATAL), which is also what I want. Why is rootLogger displaying all of the events?

like image 972
Derek Mahar Avatar asked Jun 11 '10 14:06

Derek Mahar


People also ask

What is rootLogger in Log4j2?

This concept is known as Logger Hierarchy. Logger Hierarchy is made up of set of LoggerConfig objects with a parent-child relationship. The topmost element in every Logger Hierarchy is the Root Logger. If Log4j2 doesn't find the configuration file, only Root Logger will be used for logging with logging level as ERROR.

How do you change the log level in log4j 2?

You can set a logger's level with the class Configurator from Log4j Core. BUT be aware that the Configurator class is not part of the public API. If you wish to change the root logger level, do something like this : LoggerContext ctx = (LoggerContext) LogManager.

What's the correct order of log4j levels from most verbose to least verbose?

Log4j Level Order/Priority Trace is of the lowest priority and Fatal is having highest priority. Below is the log4j logging level order. Trace < Debug < Info < Warn < Error < Fatal. When we define logger level, anything having higher priority logs are also getting printed.

Is the default log level for rootLogger?

rootLogger = WARN, FILE.


3 Answers

See this answer to a similar question about logger chaining in Log4j:

The way Log4j chaining works is a bit counter intuitive (to me at least). If the request level is equal to or above the threshold of the most specific matching logger, it is accepted. Once the request is accepted, it gets handled by the complete chain of ancestors regardless of their thresholds!

This means that no matter to what level you set the threshold of the root logger, it will always accept and output the log event that any other logger accepts, unless you disable chaining for that child logger or explicitly set the threshold of its appender to a higher level.

So, in this case, there are two ways that you can prevent root logger from capturing the events from the other loggers. The first is the more selective approach of disabling log event chaining:

log4j.additivity.com.name.myapp=false
log4j.additivity.org.castor=false
log4j.additivity.org.exolab.castor=false
log4j.additivity.org.hibernate=false
log4j.additivity.org.springframework=false

The second way is simpler, but more restrictive since it suppresses all events on the console that are lower than INFO (DEBUG and TRACE):

log4j.appender.stdout.Threshold=info
like image 66
Derek Mahar Avatar answered Sep 21 '22 15:09

Derek Mahar


Check out the inheritance described in the intro. If you specify a level at the package level, it won't inherit the root logger's level. You're using debug in the packages you specify, not info. Specifying the level overrides whatever has been inherited.

If you want to inherit the root logger's level, get rid of the level specification in your logger configurations.

like image 21
Jonathon Faust Avatar answered Sep 22 '22 15:09

Jonathon Faust


To retrieve the rootlogger, are you using Logger.getRootLogger()? If not, you may not be getting the real root logger. If so, make sure the Threshold of stdout isnt at debug; the Threshold of appenders override that of the logger Levels.

like image 22
Mornedhel Avatar answered Sep 22 '22 15:09

Mornedhel