/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache license, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the license for the specific language governing permissions and
 * limitations under the license.
 */
package org.apache.logging.log4j.core.appender.rolling;

import org.apache.logging.log4j.core.Core;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.util.Integers;

/**
 * Rolls a file over based on time.
 */
@Plugin(name = "TimeBasedTriggeringPolicy", category = Core.CATEGORY_NAME, printObject = true)
public final class TimeBasedTriggeringPolicy extends AbstractTriggeringPolicy {

    private long nextRolloverMillis;
    private final int interval;
    private final boolean modulate;

    private RollingFileManager manager;

    private TimeBasedTriggeringPolicy(final int interval, final boolean modulate) {
        this.interval = interval;
        this.modulate = modulate;
    }

    public int getInterval() {
        return interval;
    }

    public long getNextRolloverMillis() {
        return nextRolloverMillis;
    }

    /**
     * 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);
    }

    /**
     * Determines whether a rollover should occur.
     * @param event   A reference to the currently event.
     * @return true if a rollover should occur.
     */
    @Override
    public boolean isTriggeringEvent(final LogEvent event) {
        if (manager.getFileSize() == 0) {
            return false;
        }
        final long nowMillis = event.getTimeMillis();
        if (nowMillis >= nextRolloverMillis) {
            nextRolloverMillis = manager.getPatternProcessor().getNextTime(nowMillis, interval, modulate);
            return true;
        }
        return false;
    }

    /**
     * Creates a TimeBasedTriggeringPolicy.
     * @param interval The interval between rollovers.
     * @param modulate If true the time will be rounded to occur on a boundary aligned with the increment.
     * @return a TimeBasedTriggeringPolicy.
     */
    @PluginFactory
    public static TimeBasedTriggeringPolicy createPolicy(
            @PluginAttribute("interval") final String interval,
            @PluginAttribute("modulate") final String modulate) {
        final int increment = Integers.parseInt(interval, 1);
        final boolean mod = Boolean.parseBoolean(modulate);
        return new TimeBasedTriggeringPolicy(increment, mod);
    }

    @Override
    public String toString() {
        return "TimeBasedTriggeringPolicy(nextRolloverMillis=" + nextRolloverMillis + ", interval=" + interval
                + ", modulate=" + modulate + ")";
    }

}

@@ -851,16 +851,102 @@
 lling;%0A%0A
+import java.util.concurrent.ThreadLocalRandom;%0Aimport java.util.concurrent.TimeUnit;%0A%0A
 import o
@@ -1600,16 +1600,61 @@
 odulate;
+%0A    private final long maxRandomDelayMillis;
 %0A%0A    pr
@@ -1760,32 +1760,65 @@
 boolean modulate
+, final long maxRandomDelayMillis
 ) %7B%0A        this
@@ -1869,24 +1869,82 @@
 = modulate;%0A
+        this.maxRandomDelayMillis = maxRandomDelayMillis;%0A
     %7D%0A%0A    p
@@ -2512,24 +2512,18 @@
        %0A
-        
+%09%09
 nextRoll
@@ -2534,16 +2534,88 @@
 Millis =
+ ThreadLocalRandom.current().nextLong(0, 1 + maxRandomDelayMillis)%0A%09%09%09%09+
  aManage
@@ -3129,28 +3129,19 @@
 llis) %7B%0A
-            
+%09%09%09
 nextRoll
@@ -3152,16 +3152,89 @@
 Millis =
+ ThreadLocalRandom.current().nextLong(0, 1 + maxRandomDelayMillis)%0A%09%09%09%09%09+
  manager
@@ -3574,24 +3574,162 @@
  increment.%0A
+     * @param maxRandomDelay If non-zero the rolling will be delayed by a random amount of time, up to the specified value (in seconds)  %0A
      * @retu
@@ -3971,19 +3971,17 @@
 modulate
-) %7B
+,
 %0A       
@@ -3977,24 +3977,96 @@
 te,%0A        
+    @PluginAttribute(%22maxRandomDelay%22) final int maxRandomDelay) %7B%0A    %09
 final int in
@@ -4167,32 +4167,105 @@
 ulate);%0A        
+long maxRandomDelayMillis = TimeUnit.SECONDS.toMillis(maxRandomDelay);%0A%09%09
 return new TimeB
@@ -4299,16 +4299,38 @@
 ent, mod
+, maxRandomDelayMillis
 );%0A    %7D

Add an optional random delay in TimeBasedTriggeringPolicy