[ 
https://issues.apache.org/jira/browse/LOG4J2-1250?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15095608#comment-15095608
 ] 

Steven Swor commented on LOG4J2-1250:
-------------------------------------

Looks like a bug.

Consider the initialization code for TimeBasedTriggeringPolicy and 
CronTriggeringPolicy:

{code:title=TimeBasedTriggeringPolicy.java}
    /**
     * Initializes the policy.
     * @param aManager The RollingFileManager.
     */
    @Override
    public void initialize(final RollingFileManager aManager) {
        this.manager = aManager;
        
        // LOG4J2-531: call getNextTime twice to force initialization of both 
prevFileTime and nextFileTime
        aManager.getPatternProcessor().getNextTime(aManager.getFileTime(), 
interval, modulate);
        
        nextRolloverMillis = 
aManager.getPatternProcessor().getNextTime(aManager.getFileTime(), interval, 
modulate);
    }
{code}

{code:title=CronTriggeringPolicy.java}
    /**
     * Initializes the policy.
     * @param aManager The RollingFileManager.
     */
    @Override
    public void initialize(final RollingFileManager aManager) {
        this.manager = aManager;
        if (checkOnStartup) {
            Date nextDate = cronExpression.getNextValidTimeAfter(new 
Date(this.manager.getFileTime()));
            if (nextDate.getTime() < System.currentTimeMillis()) {
                manager.rollover();
            }
        }
        configuration.getScheduler().scheduleWithCron(cronExpression, new 
CronTrigger());
    }
{code}

And also PatternProcessor.formatFileName():
{code:title=PatternProcessor.java}
    /**
     * Formats file name.
     * @param subst The StrSubstitutor.
     * @param buf string buffer to which formatted file name is appended, may 
not be null.
     * @param obj object to be evaluated in formatting, may not be null.
     */
    public final void formatFileName(final StrSubstitutor subst, final 
StringBuilder buf, final Object obj) {
        // LOG4J2-628: we deliberately use System time, not the log4j.Clock time
        // for creating the file name of rolled-over files. 
        final long time = prevFileTime == 0 ? System.currentTimeMillis() : 
prevFileTime;
        formatFileName(buf, new Date(time), obj);
        final LogEvent event = new 
Log4jLogEvent.Builder().setTimeMillis(time).build();
        final String fileName = subst.replace(event, buf);
        buf.setLength(0);
        buf.append(fileName);
    }
{code}

What I think is happening is that the pattern processor's internal file 
timestamps are still 0 when the rollover happens, so it defaults to using the 
current system time instead of the previous day. I think it might be fixed by 
calling {{PatternProcessor.getNextTime()}} twice when initializing the 
CronTriggeringPolicy.

[~ralphgoers] - does this sound reasonable to you?

> [2.5] CronTriggeringPolicy renaming behavior when midnight.
> -----------------------------------------------------------
>
>                 Key: LOG4J2-1250
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1250
>             Project: Log4j 2
>          Issue Type: Question
>          Components: Appenders
>    Affects Versions: 2.5
>            Reporter: lee yongjoong
>
> Hi.
> I tried CrontriggeringPolicy(added 2.5) for save daily log files at midnight.
> My goal is, for example.
> if today is 2015/01/01, "http.log" <- active file.
> and when midnight, "http.log" will be renamed "http_20150101.log", and create 
> new "http.log".
> But my problem is when midnight, "http.log" renamed "http_20150102.log"...
> I want to make that file's name "http_20150101.log" because that file has 
> 2015/01/01's log,
> Here's my configure xml.
> {code:xml}
> <Routing name="routing_file">
>   <Routes pattern="pattern1">
>     <Route key="http">
>       <RollingFile name="rolling_http" filename="${path}/http.log" 
> filepattern="${path}/http_%d{yyyyMMdd}.log">
>         <PatternLayout pattern="http"/>
>         <CronTriggeringPolicy schedule="0 0 0 * * ?"/>
>       </RollingFile>
>     </Route>
>   </Routes>
> </Routing>
> {code}
> Please help me.
> Thanks for your help, I'm very appreciated it :D



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to