Wei Kleeff
Wei Kleeff

Reputation: 308

log4j 2 RollingFile some times failed to gz log file

As title mentions, RollingFile appender sometimes failed to gz log file, sometimes I get xxx.txt sometimes get xxx.gz in the archived folder, the config is as following:

    <RollingFile name="dev1Log" fileName="E:/test/logs/dev1Log.txt" 
                 filePattern="E:/test/logs/archived/$${date:yyyy-MM}/dev1Log.txt-%d{yyyy-MM-dd}-%i.txt.gz">
        <PatternLayout pattern="%-5p:[%c.class(%c{1}.java:%L)] %m%n"/>            
        <Policies>
            <TimeBasedTriggeringPolicy />
            <SizeBasedTriggeringPolicy size="1 M"/>
        </Policies>
        <DefaultRolloverStrategy max="100"/>
    </RollingFile>   

Error log as following:

    2017-09-09 16:26:57,582 ContainerBackgroundProcessor[StandardEngine[Catalina]] T
RACE DefaultRolloverStrategy.purge() took 182.0 milliseconds
2017-09-09 16:26:57,582 ContainerBackgroundProcessor[StandardEngine[Catalina]] D
EBUG RollingFileManager executing synchronous FileRenameAction[E:\andro_web\log4
j\mlWebLogs\xxxdev1Log.txt to E:\andro_web\log4j\mlWebLogs\archived\2017-09\xxxd
ev1Log.txt-2017-09-09-100.txt, renameEmptyFiles=false]
2017-09-09 16:26:57,582 ContainerBackgroundProcessor[StandardEngine[Catalina]] E
RROR Unable to move file E:\andro_web\log4j\mlWebLogs\xxxdev1Log.txt to E:\andro
_web\log4j\mlWebLogs\archived\2017-09\xxxdev1Log.txt-2017-09-09-100.txt: java.ni
o.file.FileSystemException E:\andro_web\log4j\mlWebLogs\xxxdev1Log.txt -> E:\and
ro_web\log4j\mlWebLogs\archived\2017-09\xxxdev1Log.txt-2017-09-09-100.txt: 程序
無法存取檔案,因為檔案正由另一個程序使用。(Chinese means: The program can not access the file because the file is being used by another program.)

Upvotes: 1

Views: 1111

Answers (1)

Remko Popma
Remko Popma

Reputation: 36754

Please set <Configuration status="trace"> at the top of your configuration. That will print internal Log4j2 debugging logs on the console. For troubleshooting this problem, please post the Log4j2 internal status messages printed on the console during rollover.


If the same Log4j2 configuration is used by multiple processes on Windows, then rollover may fail. This is not a Log4j2 behavior but depends on the operating system. Windows will not allow you to delete a file that is in use by another process.

A common cause is that the same Log4j2 configuration (logging to the same log files) is shared between multiple applications. This should be avoided.

Web applications running in the same web container may share a logging configuration but need to take some care. See the Log4j2 user manual section on web applications for details.

Upvotes: 2

Related Questions