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.TimeZone;
28
29
30
31
32 @Plugin(name = "TimeFilter", type = "Core", elementType = "filter", printObject = true)
33 public final class TimeFilter extends AbstractFilter {
34
35
36
37 private static final long HOUR_MS = 3600000;
38
39
40
41
42 private static final long MINUTE_MS = 60000;
43
44
45
46
47 private static final long SECOND_MS = 1000;
48
49
50
51
52 private final long start;
53
54
55
56 private final long end;
57
58
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.getMillis());
75
76
77
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
97
98
99
100
101
102
103
104 @PluginFactory
105 public static TimeFilter createFilter(@PluginAttr("start") final String start,
106 @PluginAttr("end") final String end,
107 @PluginAttr("timezone") final String tz,
108 @PluginAttr("onMatch") final String match,
109 @PluginAttr("onMismatch") final String mismatch) {
110 final 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 (final 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 (final ParseException ex) {
126 LOGGER.warn("Error parsing start value " + end, ex);
127 }
128 }
129 final TimeZone timezone = tz == null ? TimeZone.getDefault() : TimeZone.getTimeZone(tz);
130 final Result onMatch = Result.toResult(match, Result.NEUTRAL);
131 final Result onMismatch = Result.toResult(mismatch, Result.DENY);
132 return new TimeFilter(s, e, timezone, onMatch, onMismatch);
133 }
134
135 }