View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.appender.rolling;
18  
19  import java.lang.reflect.Method;
20  
21  import org.apache.logging.log4j.core.Core;
22  import org.apache.logging.log4j.core.LogEvent;
23  import org.apache.logging.log4j.core.config.plugins.Plugin;
24  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
25  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
26  import org.apache.logging.log4j.core.util.Loader;
27  import org.apache.logging.log4j.status.StatusLogger;
28  
29  /**
30   * Triggers a rollover on every restart, but only if the file size is greater than zero.
31   */
32  @Plugin(name = "OnStartupTriggeringPolicy", category = Core.CATEGORY_NAME, printObject = true)
33  public class OnStartupTriggeringPolicy extends AbstractTriggeringPolicy {
34  
35      private static final long JVM_START_TIME = initStartTime();
36  
37      private final long minSize;
38  
39      private OnStartupTriggeringPolicy(final long minSize) {
40          this.minSize = minSize;
41      }
42  
43      /**
44       * Returns the result of {@code ManagementFactory.getRuntimeMXBean().getStartTime()},
45       * or the current system time if JMX is not available.
46       */
47      private static long initStartTime() {
48          // LOG4J2-379:
49          // We'd like to call ManagementFactory.getRuntimeMXBean().getStartTime(),
50          // but Google App Engine throws a java.lang.NoClassDefFoundError
51          // "java.lang.management.ManagementFactory is a restricted class".
52          // The reflection is necessary because without it, Google App Engine
53          // will refuse to initialize this class.
54          try {
55              final Class<?> factoryClass = Loader.loadSystemClass("java.lang.management.ManagementFactory");
56              final Method getRuntimeMXBean = factoryClass.getMethod("getRuntimeMXBean");
57              final Object runtimeMXBean = getRuntimeMXBean.invoke(null);
58  
59              final Class<?> runtimeMXBeanClass = Loader.loadSystemClass("java.lang.management.RuntimeMXBean");
60              final Method getStartTime = runtimeMXBeanClass.getMethod("getStartTime");
61              final Long result = (Long) getStartTime.invoke(runtimeMXBean);
62  
63              return result;
64          } catch (final Throwable t) {
65              StatusLogger.getLogger().error("Unable to call ManagementFactory.getRuntimeMXBean().getStartTime(), "
66                      + "using system time for OnStartupTriggeringPolicy", t);
67              // We have little option but to declare "now" as the beginning of time.
68              return System.currentTimeMillis();
69          }
70      }
71  
72      /**
73       * Provide the RollingFileManager to the policy.
74       * @param manager The RollingFileManager.
75       */
76      @Override
77      public void initialize(final RollingFileManager manager) {
78          if (manager.getFileTime() < JVM_START_TIME && manager.getFileSize() >= minSize) {
79              StatusLogger.getLogger().debug("Initiating rollover at startup");
80              if (minSize == 0) {
81                  manager.setRenameEmptyFiles(true);
82              }
83              manager.skipFooter(true);
84              manager.rollover();
85              manager.skipFooter(false);
86          }
87      }
88  
89      /**
90       * Determine if a rollover should be triggered.
91       * @param event   A reference to the current event.
92       * @return true if the target file's timestamp is older than the JVM start time.
93       */
94      @Override
95      public boolean isTriggeringEvent(final LogEvent event) {
96          return false;
97      }
98  
99      @Override
100     public String toString() {
101         return "OnStartupTriggeringPolicy";
102     }
103 
104     @PluginFactory
105     public static OnStartupTriggeringPolicy createPolicy(
106             @PluginAttribute(value = "minSize", defaultLong = 1) final long minSize) {
107         return new OnStartupTriggeringPolicy(minSize);
108     }
109 }