8

To my best knowledge, RollingFileAppender in log4j2 will not roll over at the specified time (let's say - at the end of an hour), but at the first log event that arrives after the time threshold has been exceeded.

Is there a way to trigger an event, that on one hand will cause the file to roll over, and on another - will not append to the log (or will append something trivial, like an empty string)?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Dan M
  • 1,175
  • 12
  • 23

3 Answers3

5

No there isn't any (built-in) way to do this. There are no background threads monitoring rollover time.

You could create a log4j2 plugin that implements org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy (See the built-in TimeBasedTriggeringPolicy and SizeBasedTriggeringPolicy classes for sample code.)

If you configure your custom triggering policy, log4j2 will check for every log event whether it should trigger a rollover (so take care when implementing the isTriggeringEvent method to avoid impacting performance). Note that for your custom plugin to be picked up, you need to specify the package of your class in the packages attribute of the Configuration element of your log4j2.xml file.

Finally, if this works well for you and you think your solution may be useful to others too, consider contributing your custom triggering policy back to the log4j2 code base.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • Background thread is something that is easy to implement outside the log4j2 framework. Question is: is there any API call in the log4j2 that would **trigger** Roll Over validation/execution logic? In other words - if I develop the background thread myself, is there any API on the log4j2 side I could call to trigger Roll Over validation? – Dan M Apr 10 '14 at 16:33
  • I see, I thought you were looking for a built-in solution. I edited my answer. – Remko Popma Apr 12 '14 at 06:53
  • Would this technique apply to Logback, as well? – Randall Schulz Jul 22 '14 at 16:25
  • I would like to share the solution for log4j2 that triggers .log file roll-over. It is published at: [github](https://github.com/mushkevych/log4j2plugin). Please, refer to the README for description and instructions. – Dan M Nov 03 '14 at 04:53
2

Following Remko's idea, I wrote the following code, and it's working.

package com.stony;

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.rolling.*;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;

@Plugin(name = "ForceTriggerPolicy", category = "Core")
public class ForceTriggerPolicy implements TriggeringPolicy {
private static boolean isRolling;

@Override
public void initialize(RollingFileManager arg0) {
    setRolling(false);
}

@Override
public boolean isTriggeringEvent(LogEvent arg0) {
    return isRolling();
}

public static boolean isRolling() {
    return isRolling;
}

public static void setRolling(boolean _isRolling) {
    isRolling = _isRolling;
}

@PluginFactory
public static ForceTriggerPolicy createPolicy(){
    return new ForceTriggerPolicy();
}

}
Stony
  • 3,541
  • 3
  • 17
  • 23
2

If you have access to the Object RollingFileAppender you could do something like:

rollingFileAppender.getManager().rollover();

Here you can see the manager class:

https://github.com/apache/logging-log4j2/blob/d368e294d631e79119caa985656d0ec571bd24f5/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java