001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.appender.rolling;
018
019import java.lang.reflect.Method;
020
021import org.apache.logging.log4j.core.LogEvent;
022import org.apache.logging.log4j.core.config.plugins.Plugin;
023import org.apache.logging.log4j.core.config.plugins.PluginFactory;
024import org.apache.logging.log4j.status.StatusLogger;
025
026/**
027 * Trigger a rollover on every restart. The target file's timestamp is compared with the JVM start time
028 * and if it is older isTriggeringEvent will return true. After isTriggeringEvent has been called it will
029 * always return false.
030 */
031@Plugin(name = "OnStartupTriggeringPolicy", category = "Core", printObject = true)
032public class OnStartupTriggeringPolicy implements TriggeringPolicy {
033    private static long JVM_START_TIME = initStartTime();
034
035    private boolean evaluated = false;
036    private RollingFileManager manager;
037
038    /**
039     * Provide the RollingFileManager to the policy.
040     * @param manager The RollingFileManager.
041     */
042    @Override
043    public void initialize(final RollingFileManager manager) {
044        this.manager = manager;
045        if (JVM_START_TIME == 0) {
046            evaluated = true;
047        }
048    }
049
050    /**
051     * Returns the result of {@code ManagementFactory.getRuntimeMXBean().getStartTime()},
052     * or the current system time if JMX is not available.
053     */
054    private static long initStartTime() {
055        // LOG4J2-379:
056        // We'd like to call ManagementFactory.getRuntimeMXBean().getStartTime(),
057        // but Google App Engine throws a java.lang.NoClassDefFoundError
058        // "java.lang.management.ManagementFactory is a restricted class".
059        // The reflection is necessary because without it, Google App Engine
060        // will refuse to initialize this class.
061        try {
062            Class<?> factoryClass = Class.forName("java.lang.management.ManagementFactory");
063            Method getRuntimeMXBean = factoryClass.getMethod("getRuntimeMXBean", new Class[0]);
064            Object runtimeMXBean = getRuntimeMXBean.invoke(null, new Object[0]);
065            
066            Class<?> runtimeMXBeanClass = Class.forName("java.lang.management.RuntimeMXBean");
067            Method getStartTime = runtimeMXBeanClass.getMethod("getStartTime", new Class[0]);
068            Long result = (Long) getStartTime.invoke(runtimeMXBean, new Object[0]);
069            
070            return result.longValue();
071        } catch (Throwable t) {
072            StatusLogger.getLogger().error("Unable to call ManagementFactory.getRuntimeMXBean().getStartTime(), " //
073                    + "using system time for OnStartupTriggeringPolicy", t);
074            // We have little option but to declare "now" as the beginning of time.
075            return System.currentTimeMillis();
076        }
077    }
078
079    /**
080     * Determine if a rollover should be triggered.
081     * @param event   A reference to the current event.
082     * @return true if the target file's timestamp is older than the JVM start time.
083     */
084    @Override
085    public boolean isTriggeringEvent(final LogEvent event) {
086        if (evaluated) {
087            return false;
088        }
089        evaluated = true;
090        return manager.getFileTime() < JVM_START_TIME;
091    }
092
093    @Override
094    public String toString() {
095        return "OnStartupTriggeringPolicy";
096    }
097
098    @PluginFactory
099    public static OnStartupTriggeringPolicy createPolicy() {
100        return new OnStartupTriggeringPolicy();
101    }
102}