title: log4j vs log4j2: log to different file
body: <p>Using log4j I can log to different files invoking this method: </p>
  
  <pre><code>Logger.getLogger("test")
  </code></pre>
  
  <p>where test is an appender defined in log4j.properties. How can I log to different files with log4j2? This is my configuration file and I would choose programmatically where to log:</p>
  
  <p>
  </p>
  
  <pre><code>&lt;Properties&gt;
      &lt;Property name="log-path"&gt;C:/logs&lt;/Property&gt;
  &lt;/Properties&gt;
  
  &lt;Loggers&gt;
      &lt;Logger name="it.mypackage" level="debug" additivity="false"&gt;
          &lt;appender-ref ref="file" level="debug" /&gt;
          &lt;appender-ref ref="file2" level="error" /&gt;
      &lt;/Logger&gt;
  &lt;/Loggers&gt;
  
  &lt;Appenders&gt;
  
      &lt;!-- file.log --&gt;
      &lt;RollingFile name="file" fileName="${log-path}/file.log" filePattern="${log-path}/file-%d{yyyy-MM-dd}.log"&gt;
          &lt;PatternLayout&gt;
              &lt;pattern&gt;[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n&lt;/pattern&gt;
          &lt;/PatternLayout&gt;
          &lt;Policies&gt;
              &lt;TimeBasedTriggeringPolicy interval="1" modulate="true" /&gt;
          &lt;/Policies&gt;
      &lt;/RollingFile&gt;
  
      &lt;!-- file2.log --&gt;
      &lt;RollingFile name="file2" fileName="${log-path}/file2.log" filePattern="${log-path}/file2-%d{yyyy-MM-dd}.log"&gt;
          &lt;PatternLayout&gt;
              &lt;pattern&gt;[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n&lt;/pattern&gt;
          &lt;/PatternLayout&gt;
          &lt;Policies&gt;
              &lt;TimeBasedTriggeringPolicy interval="1" modulate="true" /&gt;
          &lt;/Policies&gt;
      &lt;/RollingFile&gt;
  &lt;/Appenders&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>[SOLUTION]</strong>
  Oh Yes, I'm missing a logger:</p>
  
  <pre><code>&lt;Properties&gt;
      &lt;Property name="log-path"&gt;C:/logs&lt;/Property&gt;
  &lt;/Properties&gt;
  
  &lt;Loggers&gt;
      &lt;Logger name="logger1" level="debug" additivity="false"&gt;
          &lt;appender-ref ref="file" level="debug" /&gt;
      &lt;/Logger&gt;
      &lt;Logger name="logger2" level="debug" additivity="false"&gt;
          &lt;appender-ref ref="file" level="debug" /&gt;
          &lt;appender-ref ref="file2" level="error" /&gt;
      &lt;/Logger&gt;
  &lt;/Loggers&gt;
  
  &lt;Appenders&gt;
  
      &lt;!-- file.log --&gt;
      &lt;RollingFile name="file" fileName="${log-path}/file.log" filePattern="${log-path}/file-%d{yyyy-MM-dd}.log"&gt;
          &lt;PatternLayout&gt;
              &lt;pattern&gt;[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n&lt;/pattern&gt;
          &lt;/PatternLayout&gt;
          &lt;Policies&gt;
              &lt;TimeBasedTriggeringPolicy interval="1" modulate="true" /&gt;
          &lt;/Policies&gt;
      &lt;/RollingFile&gt;
  
      &lt;!-- file2.log --&gt;
      &lt;RollingFile name="file2" fileName="${log-path}/file2.log" filePattern="${log-path}/file2-%d{yyyy-MM-dd}.log"&gt;
          &lt;PatternLayout&gt;
              &lt;pattern&gt;[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n&lt;/pattern&gt;
          &lt;/PatternLayout&gt;
          &lt;Policies&gt;
              &lt;TimeBasedTriggeringPolicy interval="1" modulate="true" /&gt;
          &lt;/Policies&gt;
      &lt;/RollingFile&gt;
  &lt;/Appenders&gt;
  </code></pre>
  
  <p>I can choose logger in this way:</p>
  
  <pre><code>private static Logger logger = LogManager.getLogger("logger1");
  </code></pre>