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 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
31
32 @Plugin(name = "TimeFilter", category = "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(
106 @PluginAttribute("start") final String start,
107 @PluginAttribute("end") final String end,
108 @PluginAttribute("timezone") final String tz,
109 @PluginAttribute("onMatch") final String match,
110 @PluginAttribute("onMismatch") final String 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 = Result.toResult(match, Result.NEUTRAL);
132 final Result onMismatch = Result.toResult(mismatch, Result.DENY);
133 return new TimeFilter(s, e, timezone, onMatch, onMismatch);
134 }
135
136 }