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.util.LinkedList;
21 import java.util.List;
22
23 import org.apache.log4j.Level;
24 import org.apache.log4j.helpers.UtilLoggingLevel;
25 import org.apache.log4j.spi.LoggingEvent;
26
27 /***
28 * A Rule class implementing inequality evaluation for Levels (log4j and
29 * util.logging) using the toInt method.
30 *
31 * @author Scott Deboy (sdeboy@apache.org)
32 */
33 public class LevelInequalityRule {
34 /***
35 * Level list.
36 */
37 private static List levelList;
38 /***
39 * List equivalents of java.util.logging levels.
40 */
41 private static List utilLoggingLevelList;
42
43
44 static {
45 populateLevels();
46 }
47
48 /***
49 * Create new instance.
50 */
51 private LevelInequalityRule() {
52 super();
53 }
54
55 /***
56 * Populate list of levels.
57 */
58 private static void populateLevels() {
59 levelList = new LinkedList();
60
61 levelList.add(Level.FATAL.toString());
62 levelList.add(Level.ERROR.toString());
63 levelList.add(Level.WARN.toString());
64 levelList.add(Level.INFO.toString());
65 levelList.add(Level.DEBUG.toString());
66 Level trace = Level.toLevel(5000, null);
67 if (trace != null) {
68 levelList.add(trace.toString());
69 }
70
71 utilLoggingLevelList = new LinkedList();
72
73 utilLoggingLevelList.add(UtilLoggingLevel.SEVERE.toString());
74 utilLoggingLevelList.add(UtilLoggingLevel.WARNING.toString());
75 utilLoggingLevelList.add(UtilLoggingLevel.INFO.toString());
76 utilLoggingLevelList.add(UtilLoggingLevel.CONFIG.toString());
77 utilLoggingLevelList.add(UtilLoggingLevel.FINE.toString());
78 utilLoggingLevelList.add(UtilLoggingLevel.FINER.toString());
79 utilLoggingLevelList.add(UtilLoggingLevel.FINEST.toString());
80
81 }
82
83 /***
84 * Create new rule.
85 * @param inequalitySymbol inequality symbol.
86 * @param value Symbolic name of comparison level.
87 * @return instance of AbstractRule.
88 */
89 public static Rule getRule(final String inequalitySymbol,
90 final String value) {
91
92 Level thisLevel;
93
94
95
96
97
98
99
100
101 if (levelList.contains(value.toUpperCase())) {
102 thisLevel = Level.toLevel(value.toUpperCase());
103 } else if (utilLoggingLevelList.contains(value.toUpperCase())) {
104 thisLevel = UtilLoggingLevel.toLevel(value.toUpperCase());
105 } else {
106 throw new IllegalArgumentException(
107 "Invalid level inequality rule - " + value
108 + " is not a supported level");
109 }
110
111 if ("<".equals(inequalitySymbol)) {
112 return new LessThanRule(thisLevel);
113 }
114 if (">".equals(inequalitySymbol)) {
115 return new GreaterThanRule(thisLevel);
116 }
117 if ("<=".equals(inequalitySymbol)) {
118 return new LessThanEqualsRule(thisLevel);
119 }
120 if (">=".equals(inequalitySymbol)) {
121 return new GreaterThanEqualsRule(thisLevel);
122 }
123
124 return null;
125 }
126
127 /***
128 * Rule returning true if event level less than specified level.
129 */
130 private static final class LessThanRule extends AbstractRule {
131 /***
132 * Comparison level.
133 */
134 private final int newLevelInt;
135
136 /***
137 * Create new instance.
138 * @param level comparison level.
139 */
140 public LessThanRule(final Level level) {
141 super();
142 newLevelInt = level.toInt();
143 }
144
145 /*** {@inheritDoc} */
146 public boolean evaluate(final LoggingEvent event) {
147 return (event.getLevel().toInt() < newLevelInt);
148 }
149 }
150
151 /***
152 * Rule returning true if event level greater than specified level.
153 */
154 private static final class GreaterThanRule extends AbstractRule {
155 /***
156 * Comparison level.
157 */
158 private final int newLevelInt;
159
160 /***
161 * Create new instance.
162 * @param level comparison level.
163 */
164 public GreaterThanRule(final Level level) {
165 super();
166 newLevelInt = level.toInt();
167 }
168
169 /*** {@inheritDoc} */
170 public boolean evaluate(final LoggingEvent event) {
171 return (event.getLevel().toInt() > newLevelInt);
172 }
173 }
174
175 /***
176 * Rule returning true if event level greater than
177 * or equal to specified level.
178 */
179 private static final class GreaterThanEqualsRule extends AbstractRule {
180 /***
181 * Comparison level.
182 */
183 private final int newLevelInt;
184
185 /***
186 * Create new instance.
187 * @param level comparison level.
188 */
189 public GreaterThanEqualsRule(final Level level) {
190 super();
191 newLevelInt = level.toInt();
192 }
193
194 /*** {@inheritDoc} */
195 public boolean evaluate(final LoggingEvent event) {
196 return event.getLevel().toInt() >= newLevelInt;
197 }
198 }
199
200 /***
201 * Rule returning true if event level less than or
202 * equal to specified level.
203 */
204
205 private static final class LessThanEqualsRule extends AbstractRule {
206 /***
207 * Comparison level.
208 */
209 private final int newLevelInt;
210
211 /***
212 * Create new instance.
213 * @param level comparison level.
214 */
215 public LessThanEqualsRule(final Level level) {
216 super();
217 newLevelInt = level.toInt();
218 }
219
220 /*** {@inheritDoc} */
221 public boolean evaluate(final LoggingEvent event) {
222 return (event.getLevel().toInt() <= newLevelInt);
223 }
224 }
225 }