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  
18  package org.apache.log4j.filter;
19  
20  import org.apache.log4j.helpers.LogLog;
21  import org.apache.log4j.spi.Filter;
22  import org.apache.log4j.spi.LoggingEvent;
23  
24  import java.text.ParseException;
25  import java.text.SimpleDateFormat;
26  import java.util.Calendar;
27  import java.util.TimeZone;
28  
29  /***
30   * Filters events that fall within a specified time period
31   * in each day.
32   *
33  */
34  public final class TimeFilter extends Filter {
35  
36    private boolean acceptOnMatch;
37      /***
38       * Starting offset from midnight in milliseconds.
39       */
40    private long start;
41      /***
42       * Ending offset from midnight in milliseconds.
43       */
44    private long end;
45      /***
46       * Timezone.
47       */
48    private Calendar calendar;
49  
50  
51      /***
52       * Length of hour in milliseconds.
53       */
54    private static final long HOUR_MS = 3600000;
55  
56      /***
57       * Length of minute in milliseconds.
58       */
59    private static final long MINUTE_MS = 60000;
60  
61      /***
62       * Length of second in milliseconds.
63       */
64    private static final long SECOND_MS = 1000;
65  
66      /***
67       * Constructor.
68       */
69    public TimeFilter() {
70        acceptOnMatch = true;
71        start = 0;
72        end = Long.MAX_VALUE;
73        calendar = Calendar.getInstance();
74    }
75  
76      /***
77       * Set start (inclusive) of time span.
78       * @param s string representation of start time as HH:mm:ss.
79       */
80    public void setStart(final String s) {
81        SimpleDateFormat stf = new SimpleDateFormat("HH:mm:ss");
82        stf.setTimeZone(TimeZone.getTimeZone("UTC"));
83        try {
84           start = stf.parse(s).getTime();
85        } catch(ParseException ex) {
86            LogLog.warn("Error parsing start value " + s, ex);
87        }
88    }
89  
90      /***
91       * Set end (exclusive) of time span.
92       * @param s string representation of end time as HH:mm:ss.
93       */
94    public void setEnd(final String s) {
95          SimpleDateFormat stf = new SimpleDateFormat("HH:mm:ss");
96          stf.setTimeZone(TimeZone.getTimeZone("UTC"));
97          try {
98              end = stf.parse(s).getTime();
99          } catch(ParseException ex) {
100             LogLog.warn("Error parsing end value " + s, ex);
101         }
102     }
103 
104     /***
105      * Set timezone.
106      * @param s time zone.
107      */
108   public void setTimeZone(final String s) {
109       if (s == null) {
110           calendar = Calendar.getInstance();
111       } else {
112         calendar = Calendar.getInstance(TimeZone.getTimeZone(s));
113       }
114   }
115 
116     /***
117      * Sets whether an event within the timespan should be accepted or denied.
118      * @param acceptOnMatch true if matching event should be accepted.
119      */
120   public synchronized void setAcceptOnMatch(boolean acceptOnMatch) {
121     this.acceptOnMatch = acceptOnMatch;
122   }
123 
124     /***
125      * Gets whether an event within the timespan should be accepted or denied.
126      * @return true if matching event should be accepted.
127      */
128   public synchronized boolean getAcceptOnMatch() {
129     return acceptOnMatch;
130   }
131 
132   /*** {@inheritDoc} */
133   public int decide(final LoggingEvent event) {
134     calendar.setTimeInMillis(event.timeStamp);
135     //
136     //   get apparent number of milliseconds since midnight
137     //      (ignores extra or missing hour on daylight time changes).
138     //
139     long apparentOffset = calendar.get(Calendar.HOUR_OF_DAY) * HOUR_MS +
140         calendar.get(Calendar.MINUTE) * MINUTE_MS +
141         calendar.get(Calendar.SECOND) * SECOND_MS +
142         calendar.get(Calendar.MILLISECOND);
143     if (apparentOffset >= start && apparentOffset < end) {
144         if (acceptOnMatch) {
145             return Filter.ACCEPT;
146         } else {
147             return Filter.DENY;
148         }
149     }
150     return Filter.NEUTRAL;
151   }
152 }
153