log4J: Failure in post-close rollover action using

2019-06-22 17:59发布

问题:

I have setup TimeBasedRollingPolicy to rollout the file every minute (for test purpose) and the problem I am facing is a warning and no zip or gz file is being created. Warning is:

log4j:WARN Failure in post-close rollover action

I attached the source to figure-out the problem but have no success yet. Am I missing any configuration in my log4j.xml?

<appender name="errorAppender" class="org.apache.log4j.rolling.RollingFileAppender">
        <param name="File" value="C:/error.log"/>
        <param name="Append" value="true"/>
        <param name="BufferedIO" value="true"/>
        <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
            <param name="FileNamePattern" value="C:/error.%d{ddMMMyyyy HH:mm:ss}.log.gz" />
        <param name="ActiveFileName" value="C:/error.log"/>
    </rollingPolicy>

        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %C (line:%L) - %m%n"/>
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMax" value="error"/>
            <param name="LevelMin" value="error"/>
            <param name="AcceptOnMatch" value="true"/>
        </filter>
    </appender>

I am using log4j-1.2.17 and apache-log4j-extras-1.1. Has anybody seen this problem or have any clue about it?

回答1:

Problem with "log4j:WARN Failure in post-close rollover action" message is that in windows-based systems you can not create a file name with the ":" char, so the FileNamePattern specified should not contain any one of these: \, /, :, *, ?, ", <, >, |

Here it is a log4j.xml for my application that works fine using a rolling file appender. For testing purposes I made the rolling to create a new file every second:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="consola" class="org.apache.log4j.ConsoleAppender"> 
    <param name="target" value="System.out"/> 
    <layout class="org.apache.log4j.PatternLayout"> 
      <param name="conversionPattern" value="[%d{yyyyMMdd HH:mm:ss:mm,SSS}]%-5p [%t] [%c{1}-%M:%L] - %m%n"/>
    </layout> 
  </appender> 

  <appender name="desarr" class="org.apache.log4j.rolling.RollingFileAppender">
    <param name="Append" value="false"/>
    <rollingPolicy name="desarr" class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="fileNamePattern" value="C:/workspace/Probador/log/backups/importacion222.log_%d{mmss_mm}"/>
        <param name="activeFileName" value="C:/workspace/Probador/log/importacion222.log"/>
    </rollingPolicy> 
   <layout class="org.apache.log4j.PatternLayout"> 
        <param name="conversionPattern" value="[%d{yyyyMMdd HH:mm:ss:mm,SSS}]%-5p [%t] [%c{1}-%M] - %m%n"/>
   </layout> 
  </appender> 

  <root> 
    <priority value ="debug" /> 
    <appender-ref ref="consola" /> 
    <appender-ref ref="desarr"/>
  </root>

</log4j:configuration>

Special attention to:

<param name="fileNamePattern" value="C:/workspace/Probador/log/backups/importacion222.log_%d{mmss_mm}"/>

Try this before attempting to zip the file.



回答2:

I hit the same issue in log4j with WARN message - "log4j:WARN Failure in post-close rollover action" and the log file was not rolling over. It was root caused to insufficient permission issue on the directory into which log file was getting written. In this case, Java's File.renameTo() method was failing silently (just returns a boolean false). Took lot of time to figure out the issue :(



回答3:

I am using log4j-1.2.17 and apache-log4j-extras-1.1. Has anybody seen this problem and have any clue about it?

I have also observed this problem using log4j-1.2.16 and apache-log4j-extras-1.1. The exact same message.

I have tried various tweaks to no avail. The only time when rollingPolicy->FileNamePattern seems to be observed is when it is used without the appender->File parameter and rollingPolicy->ActiveFileName parameter. But even still I have not seen it rollover successfully nor gz or zip previous files.

I also get the same messages:

log4j: setFile called: somepath/somefile.log, true
log4j: setFile ended
log4j:WARN Failure in post-close rollover action

Very frustrating.



回答4:

I Also had the same problem,but in my case it was because of the fact that the 'fileNamePattern' path folder did not exist. Rectifying that worked for me and the rollover files were being created then.



回答5:

For me the solution was to create manually the directory for archived files.



回答6:

If you are using the org.apache.log4j.rolling.TimeBasedRollingPolicy rollingPolicy, then the directory must exist prior to log4j being able to rotate.

For example, the following rollover will only work if /var/log/blah/archive/YYYY/MM directory exists; create it in a nightly cronjob should do the trick. And, as mentioned previously, this will also occur when there is not enough permission to create the log file.

  <appender name="infoFile"
            class="org.apache.log4j.rolling.RollingFileAppender">
    <param name="threshold"
           value="INFO"/>
    <param name="append"
           value="true"/>
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
      <param name="ActiveFileName"
             value="/var/log/blah/file.log"/>
      <!-- IMPORTANT the archive folder must already exist, or log4j cannot
      put the rotated log there, and will keep using the old one -->
      <param name="FileNamePattern"
             value="/var/log/blah/archive/%d{yyyy}/%d{MM}/file.log.%d{yyyy-MM-dd}.gz"/>
    </rollingPolicy>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="%5p | %-40c{2} | %-4L | %d{yyyy-MM-dd}T%d{HH:mm:ss} | %m%n"/>
    </layout>
  </appender>


标签: log4j