membersound
membersound

Reputation: 86627

How to force RollingFile durign runtime with CronTriggeringPolicy?

According to this link, log4j2 can force a logfile rollover from code, since version 2.5.

https://issues.apache.org/jira/browse/LOG4J2-89

The question is: how can I force this? The RollingFileManager has a public synchronized void rollover() method, that is invoked by CronTriggeringPolicy.

But how could I create that policy during runtime?

Upvotes: 1

Views: 834

Answers (1)

rgoers
rgoers

Reputation: 9141

You can configure the RollingFileManager with the CronTriggeringPolicy - or any other policies you want.

To force a rollover programmatically, assuming you have configured a RollingFile Appender with the name "RollingFile" you would do:

import org.apache.logging.log4j.core.LoggerContext;

LoggerContext lc = (LoggerContext)LogManager.getContext(false);
Appender app = lc.getAppender("RollingFile");
if (app instanceof RollingFileAppender) {
    ((RollingFileAppender)app).getManager().rollover();
}

The CronTriggeringPolicy isn't really designed to have the schedule updated dynamically.

Upvotes: 3

Related Questions