How to use file creation date in old log file name when rolled over log4j

I want to have the creation date of the old log file in its filename, after a rollover. Currently the date in the log file name is not equal to the file’s creation date. As an example: if a log file was created yesterday and updated until today and is rolled over now, yesterday’s date should be visible in old log file name.

log4j version is 2.14.1

log4j2.xml

    <Appenders>
        <RollingFile name="RollingFile"
                     fileName="${sys:dir}/${sys:file}.log"
                     immediateFlush="true"
                     filePattern="${sys:dir}/${sys:file}.log.%d{yyyy_MM_dd.HH_mm_ss}.%i"
                     ignoreExceptions="false">
            <PatternLayout pattern="%d{yyyyMMdd-HHmmss.SSS} |%-5p| [%t] [%c] - %-100m %n"/>
            <Policies>
                <OnStartupTriggeringPolicy/>
            </Policies>
        </RollingFile> 
    </Appenders>

Log switch after the startup is done as follows inside the code

((RollingFileAppender) appender).getManager().rollover();

This is similar to following bug but not fixed for OnStartupTriggeringPolicy
File creationTime issue for RollingFileAppender, combined time/size policy

Answer

When using OnStartupTriggeringPolicy, previousfiletime variable not updated with the creation of new file during a rollover. this is not a problem to the behaviour of the OnStartupTriggering strategy but current time printed on backed up log file during a manual rollover. This behaviour should be fixed in log4j but can do a workaround in application level to fix this issue as follows until the log4j issue is fixed.

  RollingFileManager rollingFileManager = ((RollingFileAppender) appender).getManager();                
  long currentLogCreationTime = rollingFileManager.getFileTime();
  rollingFileManager.getPatternProcessor().setPrevFileTime(currentLogCreationTime);
  rollingFileManager.rollover();