Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TimeBasedRollingFileAppender sometimes skips removing old files during MINUTELY rollover period. #634

@SRyabinin

Description

@SRyabinin

Some log files created by TimeBasedRollingFileAppender periodically remain on disk until the application is restarted. This issue occurs during the MINUTELY rollover period. Messages are appended to the log infrequently, typically one message every 2 to 5 minutes.

log4cplus.appender.T=log4cplus::TimeBasedRollingFileAppender 
log4cplus.appender.T.CreateDirs=true
log4cplus.appender.T.FilenamePattern=log_%d{yyyy-MM-dd_HH-mm}.TXT
log4cplus.appender.T.MaxHistory=2
log4cplus.appender.T.CleanHistoryOnStart = true
log4cplus.appender.T.RollOnClose = false
log4cplus.appender.T.layout = log4cplus::PatternLayout
log4cplus.appender.T.layout.ConversionPattern = %m%n
  • version: 2.0.7
  • operating system, CPU, bitness: Windows 11, x64
  • compiler and its version: C++17, Visual Studio 19

Possible Root Cause:

Based on my understanding, the issue is likely caused by a rounding error in the TimeBasedRollingFileAppender::clean method.
According to my experiments, the following patch can fix the issue:

 src/fileappender.cxx | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/fileappender.cxx b/src/fileappender.cxx
index 7c759508..db6ae1ca 100644
--- a/src/fileappender.cxx
+++ b/src/fileappender.cxx
@@ -1449,7 +1449,7 @@ TimeBasedRollingFileAppender::clean(Time time)
         file_remove(filenameToRemove);
     }
 
-    lastHeartBeat = time;
+    lastHeartBeat = time - period / 2;
 }
 
 Time::duration

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions