logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marcel Stör <mar...@frightanic.com>
Subject Re: Switching log levels at runtime with log4j2
Date Wed, 30 Nov 2016 10:40:37 GMT
On 29.11.16 05:31, Matt Sicker wrote:
> You could use the log4j-core implementation of LoggerContext.getLoggers()
> to list the current known loggers.
> https://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/LoggerContext.html#getLoggers()

Yep, that was also suggested in one of the SO answers in my initial
post. I currently implemented it that way. However,
LoggerContext#getLoggers() does not return parent loggers. So, if the
configuration is like

<Logger name="com.company" level="debug"/>
<Root level="info">
    <AppenderRef ref="CONSOLE"/>

I'd get all com.company.* loggers but neither the com.company logger nor
the root logger. I could recursively traverse that list and call
getParent() on each entry (and clean duplicates) to eventually arrive at
the complete set.

> You could also use the JMX API for LoggerConfigAdminMBean to query the list
> of loggers. You could look at the log4j-jmx-gui source code for ideas.

It uses ctx.getConfiguration().getLoggers() which, although called
getLoggers(), actually returns logger configs.

My conclusion is that the quickest way to obtain the complete set is to
combine the results of LoggerContext#getLoggers() and
LoggerContext#getConfiguration()#getLoggers(). The fact that the data
types of course "don't match" is not relevant as I just the name of each

Marcel Stör, http://frightanic.com
Couchsurfing: http://www.couchsurfing.com/people/marcelstoer
O< ascii ribbon campaign - stop html mail - http://j.mp/1OThh9L

View raw message