1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.rule;
19
20 import java.io.IOException;
21 import java.text.DateFormat;
22 import java.text.ParseException;
23 import java.text.SimpleDateFormat;
24
25 import org.apache.log4j.spi.LoggingEvent;
26 import org.apache.log4j.spi.LoggingEventFieldResolver;
27
28 /***
29 * A Rule class implementing equality evaluation for timestamps.
30 *
31 * @author Scott Deboy (sdeboy@apache.org)
32 */
33 public class TimestampEqualsRule extends AbstractRule {
34 /***
35 * Serialization ID.
36 */
37 static final long serialVersionUID = 1639079557187790321L;
38 /***
39 * Resolver.
40 */
41 private static final LoggingEventFieldResolver RESOLVER =
42 LoggingEventFieldResolver.getInstance();
43 /***
44 * Date format.
45 */
46 private static final DateFormat DATE_FORMAT =
47 new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
48
49 /***
50 * time stamp.
51 */
52 private long timeStamp;
53
54 /***
55 * Create new instance.
56 * @param value string representation of date.
57 */
58 private TimestampEqualsRule(final String value) {
59 super();
60
61 try {
62 timeStamp = DATE_FORMAT.parse(value).getTime();
63 } catch (ParseException pe) {
64 throw new IllegalArgumentException("Could not parse date: " + value);
65 }
66 }
67
68 /***
69 * Create new instance.
70 * @param value string representation of date.
71 * @return new instance
72 */
73 public static Rule getRule(final String value) {
74 return new TimestampEqualsRule(value);
75 }
76
77 /*** {@inheritDoc} */
78 public boolean evaluate(final LoggingEvent event) {
79 long eventTimeStamp = Long.parseLong(
80 RESOLVER.getValue("TIMESTAMP", event).toString()) / 1000 * 1000;
81 return eventTimeStamp == timeStamp;
82 }
83
84 /***
85 * Deserialize the state of the object.
86 *
87 * @param in object input stream
88 *
89 * @throws IOException if IO error during deserialization
90 * @throws ClassNotFoundException if class not found during
91 * deserialization
92 */
93 private void readObject(final java.io.ObjectInputStream in)
94 throws IOException, ClassNotFoundException {
95 timeStamp = in.readLong();
96 }
97
98 /***
99 * Serialize the state of the object.
100 *
101 * @param out object output stream
102 *
103 * @throws IOException if IO error during serialization
104 */
105 private void writeObject(final java.io.ObjectOutputStream out)
106 throws IOException {
107 out.writeLong(timeStamp);
108 }
109 }