title: log4j2 Dynamic log file name in Java
body: <p>I have simple question. </p>
  
  <p>How to have log file name dynamically supplied from code? </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Configuration status="warn" name="MyApp" packages=""&gt;
  
  &lt;Appenders&gt;
      &lt;RollingFile name="RollingFile" fileName="${logfilename}.log"
                   filePattern="logs/$${date:yyyy-MM}/app-%d{yyyy-MM-dd-HH}-%i.log.gz"&gt;
          &lt;PatternLayout&gt;
              &lt;Pattern&gt;%d %p %c{1.} [%t] %m%n&lt;/Pattern&gt;
          &lt;/PatternLayout&gt;
          &lt;Policies&gt;
              &lt;TimeBasedTriggeringPolicy interval="6" modulate="true"/&gt;
              &lt;SizeBasedTriggeringPolicy size="250 MB"/&gt;
          &lt;/Policies&gt;
      &lt;/RollingFile&gt;
  &lt;/Appenders&gt;
  
  &lt;Loggers&gt;
      &lt;Root level="error"&gt;
          &lt;AppenderRef ref="RollingFile"/&gt;
      &lt;/Root&gt;
  &lt;/Loggers&gt;
  </code></pre>
  
  <p></p>
  
  <p>Here file name is <strong>logs/app.log</strong>. </p>
  
  <ul>
  <li>How to make it dynamic with date and time appended as well in file name? Though pattern is applied but it doesn't work.</li>
  <li>Here <strong>logs</strong> is the directory which log4j library automatically creates, can it be dynamic as well?</li>
  </ul>
  
  <p><strong>EDIT</strong> In parameter <code>fileName</code> I have placed <code>${logfilename}.log</code> and setting system property as below:</p>
  
  <pre><code>System.setProperty("logfilename", "a_cool_logname");
  </code></pre>
  
  <p>Now it is creating file with name <code>${logfilename}.log</code> which is definitely not required.</p>
  
  <p>Thanks</p>