How to force log4j2 rolling file appender to roll over?
Asked Answered
R

3

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)?

Reasoning answered 9/4, 2014 at 22:22 Comment(0)
R
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.

Rici answered 10/4, 2014 at 5:2 Comment(4)
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?Reasoning
I see, I thought you were looking for a built-in solution. I edited my answer.Rici
Would this technique apply to Logback, as well?Paneling
I would like to share the solution for log4j2 that triggers .log file roll-over. It is published at: github. Please, refer to the README for description and instructions.Reasoning
K
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();
}

}
Klehm answered 11/12, 2018 at 6:2 Comment(0)
A
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

Aishaaisle answered 9/10, 2020 at 15:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.