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.util.HashSet;
22 import java.util.LinkedList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26
27 import org.apache.log4j.Level;
28 import org.apache.log4j.helpers.UtilLoggingLevel;
29 import org.apache.log4j.spi.LoggingEvent;
30 import org.apache.log4j.spi.LoggingEventFieldResolver;
31
32 /***
33 * A Rule class implementing not equals against two levels.
34 *
35 * @author Scott Deboy (sdeboy@apache.org)
36 */
37 public class NotLevelEqualsRule extends AbstractRule {
38 /***
39 * Serialization ID.
40 */
41 static final long serialVersionUID = -3638386582899583994L;
42
43 /***
44 * Level.
45 */
46 private transient Level level;
47
48 /***
49 * List of levels.
50 */
51 private static List levelList = new LinkedList();
52
53 static {
54 populateLevels();
55 }
56
57 /***
58 * Create new instance.
59 * @param level level.
60 */
61 private NotLevelEqualsRule(final Level level) {
62 super();
63 this.level = level;
64 }
65
66 /***
67 * Populate list of levels.
68 */
69 private static void populateLevels() {
70 levelList = new LinkedList();
71
72 levelList.add(Level.FATAL.toString());
73 levelList.add(Level.ERROR.toString());
74 levelList.add(Level.WARN.toString());
75 levelList.add(Level.INFO.toString());
76 levelList.add(Level.DEBUG.toString());
77 Level trace = Level.toLevel(5000, null);
78 if (trace != null) {
79 levelList.add(trace.toString());
80 }
81 }
82
83 /***
84 * Create new rule.
85 * @param value name of level.
86 * @return instance of NotLevelEqualsRule.
87 */
88 public static Rule getRule(final String value) {
89 Level thisLevel;
90 if (levelList.contains(value.toUpperCase())) {
91 thisLevel = Level.toLevel(value.toUpperCase());
92 } else {
93 thisLevel = UtilLoggingLevel.toLevel(value.toUpperCase());
94 }
95
96 return new NotLevelEqualsRule(thisLevel);
97 }
98
99 /***
100 * {@inheritDoc}
101 */
102 public boolean evaluate(final LoggingEvent event, Map matches) {
103
104
105 Level eventLevel = event.getLevel();
106 boolean result = level.toInt() != eventLevel.toInt();
107 if (result && matches != null) {
108 Set entries = (Set) matches.get(LoggingEventFieldResolver.LEVEL_FIELD);
109 if (entries == null) {
110 entries = new HashSet();
111 matches.put(LoggingEventFieldResolver.LEVEL_FIELD, entries);
112 }
113 entries.add(eventLevel);
114 }
115 return result;
116 }
117
118 /***
119 * Deserialize the state of the object.
120 *
121 * @param in object input stream.
122 *
123 * @throws IOException if error in reading stream for deserialization.
124 */
125 private void readObject(final java.io.ObjectInputStream in)
126 throws IOException {
127 populateLevels();
128 boolean isUtilLogging = in.readBoolean();
129 int levelInt = in.readInt();
130 if (isUtilLogging) {
131 level = UtilLoggingLevel.toLevel(levelInt);
132 } else {
133 level = Level.toLevel(levelInt);
134 }
135 }
136
137 /***
138 * Serialize the state of the object.
139 *
140 * @param out object output stream.
141 *
142 * @throws IOException if error in writing stream during serialization.
143 */
144 private void writeObject(final java.io.ObjectOutputStream out)
145 throws IOException {
146 out.writeBoolean(level instanceof UtilLoggingLevel);
147 out.writeInt(level.toInt());
148 }
149 }