title: log4j2 dynamic file name based on command line argument
body: <p>We are using a fairly simple log4j2.xml configuration file to log to stdout. However there are cases where we want to change this configuration programmatically after the start of the application to use a log file that is handed over on the command line.</p>
  
  <p>For this I followed the suggestion on the log4j2 homepage and wrote the following method</p>
  
  <pre><code>static void divertLogging(String logFile, Level level) {
      ConfigurationBuilder&lt;BuiltConfiguration&gt; builder = ConfigurationBuilderFactory.newConfigurationBuilder();
  
      AppenderComponentBuilder appenderBuilder 
          = builder.newAppender("File", "FILE").addAttribute("fileName", logFile).addAttribute("append", "false");
  
      appenderBuilder.add(builder.newLayout("PatternLayout")
          .addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"));
  
      builder.add(appenderBuilder);
      builder.add(builder.newRootLogger(level).add(builder.newAppenderRef("File")));
  
      try {
          builder.writeXmlConfiguration(System.out);
      } catch (IOException e) {
          throw new ApplicationException(e);
      }
  
      BuiltConfiguration configuration = builder.build();
  
      Configurator.initialize(configuration);
      ((LoggerContext)LogManager.getContext(false)).updateLoggers(configuration);
  }
  </code></pre>
  
  <p>We get the following output </p>
  
  <pre><code>&lt;?xml version="1.0" ?&gt;
  &lt;Configuration&gt;
      &lt;Appenders&gt;
          &lt;FILE name="File" fileName="test.log" append="false"&gt;
              &lt;PatternLayout pattern="%d [%t] %-5level: %msg%n%throwable"/&gt;
          &lt;/FILE&gt;
      &lt;/Appenders&gt;
      &lt;Loggers&gt;
          &lt;Root level="INFO"&gt;
              &lt;AppenderRef ref="File"/&gt;
          &lt;/Root&gt;
      &lt;/Loggers&gt;
  &lt;/Configuration&gt;
  </code></pre>
  
  <p>and then the log message</p>
  
  <pre><code>ERROR Attempted to append to non-started appender File
  </code></pre>
  
  <p>After that logging continues to be output to stdout and the desired log file stays empty.</p>
  
  <p>Anyone having an idea what I am doing wrong?</p>