title: Log4j2 failed to roll over if the same log file is been used by 2 different app
body: <p>I have my app running as 2 instance in Tomcat say client and server, I have to capture the user activity of logged user in a unique file specific to user like john.log. I was able to achieve this using Routing in Log4j2 and pointing the log4j2 log file location in both app to same directory.</p>
  
  <p>Here comes the problem, when I try to roll over the log file every day night / every 1 hr its throws exception, stating that file is been accessed by another program.</p>
  
  <p>When I tried to write the log separately instead of one file, its working fine and gets roll over as per cron condition, but when pointing to same directory and accessing same file, roll over throws exception during roll over process.
  I was using the latest Log4j2 version "2.11.2"</p>
  
  <p>Client Log4j xml file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE xml&gt;
  &lt;Configuration status="WARN" name="MyApp-Client" monitorInterval="30"&gt;
      &lt;Properties&gt;
  
          &lt;!-- Log File location--&gt;
          &lt;Property name="BaseDir"&gt;C:\\Log_Files\webapp&lt;/Property&gt;
  
          &lt;!-- Log pattern --&gt;
          &lt;Property name="pattern"&gt;[%-5level] [%d] [%c{1}] - %msg%n&lt;/Property&gt;
  
          &lt;!-- Routing File --&gt;
          &lt;Property name="RoutFileName"&gt;${BaseDir}\${ctx:logFileName}.log&lt;/Property&gt;
          &lt;Property name="RoutFilePattern"&gt;${BaseDir}\${ctx:logFileName}-%d{yyyy-MM-dd}-%i.log&lt;/Property&gt;
  
          &lt;!-- CRON pattern --&gt;
          &lt;Property name="Roll_perminute"&gt;0 * * * * ?&lt;/Property&gt;
          &lt;Property name="Roll_Daily"&gt;0 0 0 * * ?&lt;/Property&gt;
  
      &lt;/Properties&gt;
  
      &lt;Appenders&gt;
          &lt;Console name="Console" target="SYSTEM_OUT"&gt;
              &lt;PatternLayout pattern="${pattern}" /&gt;
          &lt;/Console&gt;
  
          &lt;Routing name="USERLEVEL"&gt;
              &lt;Routes pattern="$${ctx:logFileName}"&gt;
                  &lt;Route&gt;
                      &lt;RollingFile name="Rolling-${ctx:logFileName}" append="true" fileName="${RoutFileName}" filePattern="${RoutFilePattern}"&gt;
                          &lt;PatternLayout pattern="Client-LOG: ${pattern}" /&gt;
                          &lt;Policies&gt;
                              &lt;CronTriggeringPolicy schedule="${Roll_perminute}" evaluateOnStartup="true"/&gt;
                          &lt;/Policies&gt;
                      &lt;/RollingFile&gt;
                  &lt;/Route&gt;
                  &lt;Route ref="Console" key="${ctx:logFileName}" /&gt;
              &lt;/Routes&gt;
          &lt;/Routing&gt;
  
      &lt;/Appenders&gt;
  
      &lt;Loggers&gt;
          &lt;Logger name="com.myapp" level="ALL" additivity="false"&gt;
              &lt;Appender-Ref ref="USERLEVEL" level="ALL" /&gt;
          &lt;/Logger&gt;
          &lt;Root level="ALL" additivity="false"&gt;
              &lt;Appender-Ref ref="Console" level="ALL" /&gt;
          &lt;/Root&gt;
      &lt;/Loggers&gt;
  &lt;/Configuration&gt;
  </code></pre>
  
  <p>Server Log4j xml </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE xml&gt;
  &lt;Configuration status="WARN" name="MyApp-Server" monitorInterval="30"&gt;
      &lt;Properties&gt;
  
          &lt;!-- Log File location--&gt;
          &lt;Property name="BaseDir"&gt;C:\\Log_Files\webapp&lt;/Property&gt;
  
          &lt;!-- Log pattern --&gt;
          &lt;Property name="pattern"&gt;[%-5level] [%d] [%c{1}] - %msg%n&lt;/Property&gt;
  
          &lt;!-- Routing File --&gt;
          &lt;Property name="RoutFileName"&gt;${BaseDir}\${ctx:logFileName}.log&lt;/Property&gt;
          &lt;Property name="RoutFilePattern"&gt;${BaseDir}\${ctx:logFileName}-%d{yyyy-MM-dd}-%i.log&lt;/Property&gt;
  
          &lt;!-- CRON pattern --&gt;
          &lt;Property name="Roll_perminute"&gt;0 * * * * ?&lt;/Property&gt;
          &lt;Property name="Roll_Daily"&gt;0 0 0 * * ?&lt;/Property&gt;
  
      &lt;/Properties&gt;
  
      &lt;Appenders&gt;
          &lt;Console name="Console" target="SYSTEM_OUT"&gt;
              &lt;PatternLayout pattern="${pattern}" /&gt;
          &lt;/Console&gt;
  
          &lt;Routing name="USERLEVEL"&gt;
              &lt;Routes pattern="$${ctx:logFileName}"&gt;
                  &lt;Route&gt;
                      &lt;RollingFile name="Rolling-${ctx:logFileName}" append="true" fileName="${RoutFileName}" filePattern="${RoutFilePattern}"&gt;
                          &lt;PatternLayout pattern="SERVER-LOG: ${pattern}" /&gt;
                          &lt;Policies&gt;
                              &lt;CronTriggeringPolicy schedule="${Roll_perminute}" evaluateOnStartup="true"/&gt;
                          &lt;/Policies&gt;
                      &lt;/RollingFile&gt;
                  &lt;/Route&gt;
                  &lt;Route ref="Console" key="${ctx:logFileName}" /&gt;
              &lt;/Routes&gt;
          &lt;/Routing&gt;
  
      &lt;/Appenders&gt;
  
      &lt;Loggers&gt;
          &lt;Logger name="com.myapp" level="ALL" additivity="false"&gt;
              &lt;Appender-Ref ref="USERLEVEL" level="ALL" /&gt;
          &lt;/Logger&gt;
          &lt;Root level="ALL" additivity="false"&gt;
              &lt;Appender-Ref ref="Console" level="ALL" /&gt;
          &lt;/Root&gt;
      &lt;/Loggers&gt;
  &lt;/Configuration&gt;
  </code></pre>
  
  <p>Error thrown</p>
  
  <pre><code>2019-05-31 19:43:00,021 Log4j2-TF-3-Scheduled-4 ERROR Unable to move file C:\\Log_Files\webapp\User1.log to C:\\Log_Files\webapp\User1-2019-05-31-1.log: java.nio.file.FileSystemException C:\\Log_Files\webapp\User1.log -&gt; C:\\Log_Files\webapp\User1-2019-05-31-1.log: The process cannot access the file because it is being used by another process.
  2019-05-31 19:43:00,026 Log4j2-TF-3-Scheduled-4 ERROR Unable to delete file C:\\Log_Files\webapp\User1.log: java.nio.file.FileSystemException C:\\Log_Files\webapp\User1.log: The process cannot access the file because it is being used by another process.
  
  </code></pre>
  
  <p>Based on the Error thrown, I do understand when roll over is triggered still the log file is been accessed by either of the logger instance. But How can we safely roll over on such condition, am I doing something wrong in configuration. Do we have any other options in log4j2 to achieve this</p>