1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
32
33 @Plugin(name = "TimeFilter", type = "Core", elementType = "filter", printObject = true)
34 public final class TimeFilter extends AbstractFilter {
35
36
37
38 private static final long HOUR_MS = 3600000;
39
40
41
42
43 private static final long MINUTE_MS = 60000;
44
45
46
47
48 private static final long SECOND_MS = 1000;
49
50
51
52
53 private final long start;
54
55
56
57 private final long end;
58
59
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
77
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
97
98
99
100
101
102
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 }