I declare loggers in my code like this:
Logger logger = LoggerFactory.getLogger(MyClass.class);
I would like to be able to change the log level of my loggers programmatically at runtime. I have tried iterating over the loggers in the repository via the LogManager, but it only applies the new log level to loggers that are instantiated in the code. If a new logger instance is created, it does not use the new log level. My requirement is such that the log level in my web application needs to be configurable via an Administrative GUI, and this new log level needs to apply to all loggers in my code (exclude third party library logging, e.g. JSF, Spring, Hibernate etc).
This is what I'm doing now which does not meet my requirements as it does not apply the log level to newly instantiated loggers:
Enumeration<Logger> currentLoggers = LogManager.getCurrentLoggers();
while(currentLoggers.hasMoreElements()) {
Logger logger = currentLoggers.nextElement();
if (StringUtils.startsWith(logger.getName(), "com.my.company")) {
logger.setLevel(logLevel);
}
}
I am using Log4j 1.2.17.
Didn't find the resource anymore, but as to my faded memory LogManager.getLogger("com.my.company").setLevel(whateverloglevel)
should do the job.
All loggers that are created with LogManager.getLogger(MyClass.class)
where MyClass is in com.my.company or in a subtree of that will be affected.
whateverloglevel
is one of Level:
Be aware that this probably does not work in log4j 2.
For (I think) version >= 2.4 see:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With