title: Spring Boot programmatic logging configuration
body: <p>How can I configure logging <strong>programmatically</strong> in a spring boot application?</p>
  
  <p>Using an xml or properties file is not flexible enough for my needs.</p>
  
  <p><strong>Update:</strong> I want to achieve something like this:</p>
  
  <pre><code>@Value("${logging.level.root}")
  private String loggingLevelRoot;
  
  @Value("${logging.level.myApp}")
  private String loggingLevelMyApp;
  
  @Value("${logging.file}")
  private boolean fileAppenderEnabled;
  
  ....
  
  setLevel(Logger.ROOT_LOGGER_NAME, Level.toLevel(loggingLevelRoot)));
  setLevel("com.myapp", Level.toLevel(loggingLevelMyApp)));
  setLevel("org.springframework", Level.WARN);
  setLevel("org.apache.coyote", Level.INFO);
  setLevel("org.apache.catalina", Level.INFO);
  setLevel("org.apache.catalina.startup.DigesterFactory", Level.ERROR);
  setLevel("org.apache.catalina.util.LifecycleMBeanBase", Level.ERROR);
  
  Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
  logger.addAppender(createConsoleAppender());
  if (fileAppenderEnabled) {
      logger.addAppender(createFileAppender());
  }
  </code></pre>
  
  <p>All I have per environment is:</p>
  
  <ul>
  <li>logging.level.root=[INFO, DEBUG, ..]</li>
  <li>logging.level.myApp=[INFO, DEBUG, ..]</li>
  <li>logging.file=[true | false]</li>
  </ul>
  
  <p>No duplication of XML, Groovy and other formats I really don't want to deal with.</p>
  
  <p>At the end of the day, this is really about achieving the same flexibility for logging as Spring JavaConfig did for beans. XML or other file formats are simply too static, require too much duplication and not integrated well enough with the rest of the configuration of the application.</p>
  
  <p><strong>Why should logging be configured differently than any other bean or service?</strong> It makes no sense.</p>