title: Log4j: Change log level programmatically that works for to-be created loggers
body: <p>I declare loggers in my code like this:</p>
  
  <pre><code>Logger logger = LoggerFactory.getLogger(MyClass.class);
  </code></pre>
  
  <p>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).</p>
  
  <p>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:</p>
  
  <pre><code>Enumeration&lt;Logger&gt; currentLoggers = LogManager.getCurrentLoggers();
  while(currentLoggers.hasMoreElements()) {
      Logger logger = currentLoggers.nextElement();
      if (StringUtils.startsWith(logger.getName(), "com.my.company")) {
          logger.setLevel(logLevel);
      }
  }
  </code></pre>
  
  <p>I am using Log4j 1.2.17.</p>