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 org.apache.logging.log4j.core.LogEvent;
20  import org.apache.logging.log4j.core.config.plugins.Plugin;
21  import org.apache.logging.log4j.core.config.plugins.PluginAttr;
22  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
23  
24  import java.text.ParseException;
25  import java.text.SimpleDateFormat;
26  import java.util.Calendar;
27  import java.util.Locale;
28  import java.util.TimeZone;
29  
30  /**
31   * Filters events that fall within a specified time period in each day.
32   */
33  @Plugin(name = "TimeFilter", type = "Core", elementType = "filter", printObject = true)
34  public final class TimeFilter extends AbstractFilter {
35      /**
36       * Length of hour in milliseconds.
37       */
38      private static final long HOUR_MS = 3600000;
39  
40      /**
41       * Length of minute in milliseconds.
42       */
43      private static final long MINUTE_MS = 60000;
44  
45      /**
46       * Length of second in milliseconds.
47       */
48      private static final long SECOND_MS = 1000;
49  
50      /**
51       * Starting offset from midnight in milliseconds.
52       */
53      private final long start;
54      /**
55       * Ending offset from midnight in milliseconds.
56       */
57      private final long end;
58      /**
59       * Timezone.
60       */
61      private final TimeZone timezone;
62  
63  
64      private TimeFilter(long start, long end, TimeZone tz, Result onMatch, 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(LogEvent event) {
73          Calendar calendar = Calendar.getInstance(timezone);
74          calendar.setTimeInMillis(event.getMillis());
75          //
76          //   get apparent number of milliseconds since midnight
77          //      (ignores extra or missing hour on daylight time changes).
78          //
79          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          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(@PluginAttr("start") String start,
106                                           @PluginAttr("end") String end,
107                                           @PluginAttr("timezone") String tz,
108                                           @PluginAttr("onMatch") String match,
109                                           @PluginAttr("onMismatch") String mismatch) {
110         SimpleDateFormat stf = new SimpleDateFormat("HH:mm:ss");
111         long s = 0;
112         if (start != null) {
113             stf.setTimeZone(TimeZone.getTimeZone("UTC"));
114             try {
115                 s = stf.parse(start).getTime();
116             } catch (ParseException ex) {
117                 LOGGER.warn("Error parsing start value " + start, ex);
118             }
119         }
120         long e = Long.MAX_VALUE;
121         if (end != null) {
122             stf.setTimeZone(TimeZone.getTimeZone("UTC"));
123             try {
124                 e = stf.parse(end).getTime();
125             } catch (ParseException ex) {
126                 LOGGER.warn("Error parsing start value " + end, ex);
127             }
128         }
129         TimeZone timezone = (tz == null) ? TimeZone.getDefault() : TimeZone.getTimeZone(tz);
130         Result onMatch = Result.toResult(match, Result.NEUTRAL);
131         Result onMismatch = Result.toResult(mismatch, Result.DENY);
132         return new TimeFilter(s, e, timezone, onMatch, onMismatch);
133     }
134 
135 }