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.