Log4j2 Rolled Files that are deleted are still ope

2019-05-23 11:19发布

问题:

I recently migrated from log4j to log4j2 on my tomcat6 server. The problem is that my rolling files that are deleted are remaining open forever and hence the disk space is not being freed. i used this command "sudo /usr/sbin/lsof | grep deleted" to check on the open deleted files. I don't seem to understand how to close those files without restarting the tomcat server. Some of these logs are being deleted by cron jobs. while a lot of them are being deleted via logrotate.

...

 <Appenders>
    <RollingRandomAccessFile name="requestAppender" fileName="${sys:catalina.home}/webapps/miscLogs/request.log"
                filePattern="${sys:catalina.home}/webapps/miscLogs/request.log.%i">
            <PatternLayout>
                <Pattern>
                    "%d{dd MMM yyyy HH:mm:ss } %-5p %c %x -%m%n"
                </Pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="3400MB"/>
            </Policies>
            <DefaultRolloverStrategy max="3"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<logger additivity="false" name="requestLogger" level="debug">
            <AppenderRef ref="requestAppender"/>
        </logger>
</Loggers>

...

The problem is with the files that have already been rolled. In this case the problem is with request.log.3. It's being deleted by a cron (no logrotate) but it's still not giving up disk space and is held open by the tomcat server on which the whole program is running.

the output of lsof is below..

java 6393 root 160u REG 8,1 3565160049 5525509 /opt/tomcat/webapps/miscLogs/request.log.1 (deleted)

here the java process is the tomcat 6 server which is still holding the file open.

回答1:

There is a known problem with RandomAccessFileAppender and logrotate: http://issues.apache.org/jira/browse/LOG4J2-354; the Log4j team recommends using RollingRandomAccessFileAppender instead.