Log only if logger is explicitly turned on (set at a certain level - ignoring root/parents/hierarchy)

Viewed 19

I've spent quite a bit more time than necessary to figure this out, and came up with zilch! This may not be the most kosher way to do what I need, but in the legacy version, I found it easy enough to use in practice. Basically, I'd have a specially named logger, and I want it to be enabled (i.e. logging diagnostic) only if it's explicitly enabled (i.e. specified in the XML configuration file).

The hierarchical fashion the logger configuration works would cancel this out if ROOT logger is set at a lower level than my log lines. It would log this particular logger's lines, even if I don't want it to (yes, I know this is not the expected usage).

In legacy log4j, there were 2 ways to get the "level": getLevel and getEffectiveLevel. Effective traversed up the hierarchy, and the regular one returned the value at my particular logger. Using this, I could check the Level, and if it's null (i.e. not explicitly specified in my configuration), I'd programatically set the level to be OFF, and disable it.

There doesn't seem to be a way to access that piece of information in the straightforward part of the API. Looking through the object model in debugger, I've found the following map that could be checked if it contains the name of my logger or not, but it seems a bit convoluted:

LoggerContext.getContext(false).getConfiguration().getLoggers()

Is there another/simpler way to achieve this? Or should I just take my square peg, and move away from the round hole?! :)

0 Answers
Related