title: Log4J2 - assigning file appender filename at runtime
body: <p>I have a log4j2.xml config file in the class path.  One of the appenders is a File appender, and I would like to set the target file name at run time in the Java application.</p>
  <p>According to the <a href="http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution" rel="noreferrer">docs</a> I should be able to use a double &quot;$&quot; and a context prefix in the log4j2.xml file:</p>
  <pre><code>&lt;appenders&gt;
      &lt;File name=&quot;MyFile&quot; fileName=&quot;$${sys:logFilename}&quot;&gt;
          &lt;PatternLayout pattern=&quot;%-4r %d{${datestamp}} [%t] %-5level %logger{36} - %msg%n&quot;/&gt;
      &lt;/File&gt;
  &lt;/appenders&gt;
  </code></pre>
  <p>where the &quot;sys&quot; prefix indicates that the Configurator will lookup the property &quot;logFilename&quot; in the system properties.  So in the application, I call (rather early on):</p>
  <pre><code>System.setProperty(&quot;logFilename&quot;, filename);
  </code></pre>
  <p>I have also turned on auto-reconfiguration for log4j2 in the xml file:</p>
  <pre><code>&lt;configuration status=&quot;debug&quot; monitorInterval=&quot;5&quot;&gt;&gt;
  </code></pre>
  <p>Unfortunately, this has no effect whatsoever, and the log file is never created.  Some of the log4j2 status output is below:</p>
  <blockquote>
  <p>2013-02-13 15:36:37,574 DEBUG Calling createAppender on class org.apache.logging.log4j.core.appender.FileAppender for element File with params(fileName=&quot;${sys:logFilename}&quot;, append=&quot;null&quot;, locking=&quot;null&quot;, name=&quot;MyFile&quot;, immediateFlush=&quot;null&quot;, suppressExceptions=&quot;null&quot;, bufferedIO=&quot;null&quot;, PatternLayout(%-4r %d{yyyy-MM-dd/HH:mm:ss.SSS/zzz} [%t] %-5level %logger{36} - %msg%n), null)</p>
  <p>2013-02-13 15:36:37,576 DEBUG Starting FileManager ${sys:logFilename}</p>
  </blockquote>
  <p><strong>How can I have the value of &quot;fileName&quot; in the File Appender be set at run time?</strong>  Alternatively, how can I simply add a new File Appender to the root logger at run time?  In Log4j 2.0 most of the API to change the configuration is hidden.</p>