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.filter;
18  
19  import java.text.ParseException;
20  import java.text.SimpleDateFormat;
21  import java.util.Calendar;
22  import java.util.TimeZone;
23  
24  import org.apache.logging.log4j.core.LogEvent;
25  import org.apache.logging.log4j.core.config.plugins.Plugin;
26  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
27  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
28  
29  /**
30   * Filters events that fall within a specified time period in each day.
31   */
32  @Plugin(name = "TimeFilter", category = "Core", elementType = "filter", printObject = true)
33  public final class TimeFilter extends AbstractFilter {
34      /**
35       * Length of hour in milliseconds.
36       */
37      private static final long HOUR_MS = 3600000;
38  
39      /**
40       * Length of minute in milliseconds.
41       */
42      private static final long MINUTE_MS = 60000;
43  
44      /**
45       * Length of second in milliseconds.
46       */
47      private static final long SECOND_MS = 1000;
48  
49      /**
50       * Starting offset from midnight in milliseconds.
51       */
52      private final long start;
53      /**
54       * Ending offset from midnight in milliseconds.
55       */
56      private final long end;
57      /**
58       * Timezone.
59       */
60      private final TimeZone timezone;
61  
62  
63      private TimeFilter(final long start, final long end, final TimeZone tz, final Result onMatch,
64                         final Result onMismatch) {
65          super(onMatch, onMismatch);
66          this.start = start;
67          this.end = end;
68          timezone = tz;
69      }
70  
71      @Override
72      public Result filter(final LogEvent event) {
73          final Calendar calendar = Calendar.getInstance(timezone);
74          calendar.setTimeInMillis(event.getTimeMillis());
75          //
76          //   get apparent number of milliseconds since midnight
77          //      (ignores extra or missing hour on daylight time changes).
78          //
79          final long apparentOffset = calendar.get(Calendar.HOUR_OF_DAY) * HOUR_MS +
80              calendar.get(Calendar.MINUTE) * MINUTE_MS +
81              calendar.get(Calendar.SECOND) * SECOND_MS +
82              calendar.get(Calendar.MILLISECOND);
83          return apparentOffset >= start && apparentOffset < end ? onMatch : onMismatch;
84      }
85  
86      @Override
87      public String toString() {
88          final StringBuilder sb = new StringBuilder();
89          sb.append("start=").append(start);
90          sb.append(", end=").append(end);
91          sb.append(", timezone=").append(timezone.toString());
92          return sb.toString();
93      }
94  
95      /**
96       * Create a TimeFilter.
97       * @param start The start time.
98       * @param end The end time.
99       * @param tz timezone.
100      * @param match Action to perform if the time matches.
101      * @param mismatch Action to perform if the action does not match.
102      * @return A TimeFilter.
103      */
104     @PluginFactory
105     public static TimeFilter createFilter(
106             @PluginAttribute("start") final String start,
107             @PluginAttribute("end") final String end,
108             @PluginAttribute("timezone") final String tz,
109             @PluginAttribute("onMatch") final Result match,
110             @PluginAttribute("onMismatch") final Result mismatch) {
111         final SimpleDateFormat stf = new SimpleDateFormat("HH:mm:ss");
112         long s = 0;
113         if (start != null) {
114             stf.setTimeZone(TimeZone.getTimeZone("UTC"));
115             try {
116                 s = stf.parse(start).getTime();
117             } catch (final ParseException ex) {
118                 LOGGER.warn("Error parsing start value " + start, ex);
119             }
120         }
121         long e = Long.MAX_VALUE;
122         if (end != null) {
123             stf.setTimeZone(TimeZone.getTimeZone("UTC"));
124             try {
125                 e = stf.parse(end).getTime();
126             } catch (final ParseException ex) {
127                 LOGGER.warn("Error parsing start value " + end, ex);
128             }
129         }
130         final TimeZone timezone = tz == null ? TimeZone.getDefault() : TimeZone.getTimeZone(tz);
131         final Result onMatch = match == null ? Result.NEUTRAL : match;
132         final Result onMismatch = mismatch == null ? Result.DENY : mismatch;
133         return new TimeFilter(s, e, timezone, onMatch, onMismatch);
134     }
135 
136 }