log4j2: how-to force rolling file appender to roll over?

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

EDIT: 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.


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();
}

}