View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.rule;
19  
20  import java.util.HashSet;
21  import java.util.LinkedList;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import org.apache.log4j.Level;
27  import org.apache.log4j.helpers.UtilLoggingLevel;
28  import org.apache.log4j.spi.LoggingEvent;
29  import org.apache.log4j.spi.LoggingEventFieldResolver;
30  
31  /***
32   * A Rule class implementing inequality evaluation for Levels (log4j and
33   * util.logging) using the toInt method.
34   *
35   * @author Scott Deboy (sdeboy@apache.org)
36   */
37  public class LevelInequalityRule {
38      /***
39       * Level list.
40       */
41      private static List levelList;
42      /***
43       * List equivalents of java.util.logging levels.
44       */
45      private static List utilLoggingLevelList;
46  
47  
48      static {
49          populateLevels();
50      }
51  
52      /***
53       * Create new instance.
54       */
55      private LevelInequalityRule() {
56          super();
57      }
58  
59      /***
60       * Populate list of levels.
61       */
62      private static void populateLevels() {
63          levelList = new LinkedList();
64  
65          levelList.add(Level.FATAL.toString());
66          levelList.add(Level.ERROR.toString());
67          levelList.add(Level.WARN.toString());
68          levelList.add(Level.INFO.toString());
69          levelList.add(Level.DEBUG.toString());
70  		Level trace = Level.toLevel(5000, null);
71  		if (trace != null) {
72  			levelList.add(trace.toString());
73  		}
74  
75          utilLoggingLevelList = new LinkedList();
76  
77          utilLoggingLevelList.add(UtilLoggingLevel.SEVERE.toString());
78          utilLoggingLevelList.add(UtilLoggingLevel.WARNING.toString());
79          utilLoggingLevelList.add(UtilLoggingLevel.INFO.toString());
80          utilLoggingLevelList.add(UtilLoggingLevel.CONFIG.toString());
81          utilLoggingLevelList.add(UtilLoggingLevel.FINE.toString());
82          utilLoggingLevelList.add(UtilLoggingLevel.FINER.toString());
83          utilLoggingLevelList.add(UtilLoggingLevel.FINEST.toString());
84  
85      }
86  
87      /***
88       * Create new rule.
89       * @param inequalitySymbol inequality symbol.
90       * @param value Symbolic name of comparison level.
91       * @return instance of AbstractRule.
92       */
93      public static Rule getRule(final String inequalitySymbol,
94                                 final String value) {
95  
96          Level thisLevel;
97  
98          //if valid util.logging levels are used against events
99          // with log4j levels, the
100         //DEBUG level is used and an illegalargumentexception won't be generated
101 
102         //an illegalargumentexception is only generated
103         // if the user types a level name
104         //that doesn't exist as either a log4j or util.logging level name
105         if (levelList.contains(value.toUpperCase())) {
106             thisLevel = Level.toLevel(value.toUpperCase());
107         } else if (utilLoggingLevelList.contains(value.toUpperCase())) {
108             thisLevel = UtilLoggingLevel.toLevel(value.toUpperCase());
109         } else {
110             throw new IllegalArgumentException(
111                     "Invalid level inequality rule - " + value
112                             + " is not a supported level");
113         }
114 
115         if ("<".equals(inequalitySymbol)) {
116             return new LessThanRule(thisLevel);
117         }
118         if (">".equals(inequalitySymbol)) {
119             return new GreaterThanRule(thisLevel);
120         }
121         if ("<=".equals(inequalitySymbol)) {
122             return new LessThanEqualsRule(thisLevel);
123         }
124         if (">=".equals(inequalitySymbol)) {
125             return new GreaterThanEqualsRule(thisLevel);
126         }
127 
128         return null;
129     }
130 
131     /***
132      * Rule returning true if event level less than specified level.
133      */
134     private static final class LessThanRule extends AbstractRule {
135         /***
136          * Comparison level.
137          */
138         private final int newLevelInt;
139 
140         /***
141          * Create new instance.
142          * @param level comparison level.
143          */
144         public LessThanRule(final Level level) {
145             super();
146             newLevelInt = level.toInt();
147         }
148 
149         /*** {@inheritDoc} */
150         public boolean evaluate(final LoggingEvent event, Map matches) {
151             Level eventLevel = event.getLevel();
152             boolean result = (eventLevel.toInt() < newLevelInt);
153             if (result && matches != null) {
154                 Set entries = (Set) matches.get(LoggingEventFieldResolver.LEVEL_FIELD);
155                 if (entries == null) {
156                     entries = new HashSet();
157                     matches.put(LoggingEventFieldResolver.LEVEL_FIELD, entries);
158                 }
159                 entries.add(eventLevel);
160             }
161             return result;
162         }
163     }
164 
165     /***
166      * Rule returning true if event level greater than specified level.
167      */
168     private static final class GreaterThanRule extends AbstractRule {
169         /***
170          * Comparison level.
171          */
172         private final int newLevelInt;
173 
174         /***
175          * Create new instance.
176          * @param level comparison level.
177          */
178         public GreaterThanRule(final Level level) {
179             super();
180             newLevelInt = level.toInt();
181         }
182 
183         /*** {@inheritDoc} */
184         public boolean evaluate(final LoggingEvent event, Map matches) {
185             Level eventLevel = event.getLevel();
186             boolean result = (eventLevel.toInt() > newLevelInt);
187             if (result && matches != null) {
188                 Set entries = (Set) matches.get(LoggingEventFieldResolver.LEVEL_FIELD);
189                 if (entries == null) {
190                     entries = new HashSet();
191                     matches.put(LoggingEventFieldResolver.LEVEL_FIELD, entries);
192                 }
193                 entries.add(eventLevel);
194             }
195             return result;
196         }
197     }
198 
199     /***
200      * Rule returning true if event level greater than
201      * or equal to specified level.
202      */
203     private static final class GreaterThanEqualsRule extends AbstractRule {
204         /***
205          * Comparison level.
206          */
207         private final int newLevelInt;
208 
209         /***
210          * Create new instance.
211          * @param level comparison level.
212          */
213         public GreaterThanEqualsRule(final Level level) {
214             super();
215             newLevelInt = level.toInt();
216         }
217 
218         /*** {@inheritDoc} */
219         public boolean evaluate(final LoggingEvent event, Map matches) {
220             Level eventLevel = event.getLevel();
221             boolean result = eventLevel.toInt() >= newLevelInt;
222             if (result && matches != null) {
223                 Set entries = (Set) matches.get(LoggingEventFieldResolver.LEVEL_FIELD);
224                 if (entries == null) {
225                     entries = new HashSet();
226                     matches.put(LoggingEventFieldResolver.LEVEL_FIELD, entries);
227                 }
228                 entries.add(eventLevel);
229             }
230             return result;
231         }
232     }
233 
234     /***
235      * Rule returning true if event level less than or
236      * equal to specified level.
237      */
238 
239     private static final class LessThanEqualsRule extends AbstractRule {
240         /***
241          * Comparison level.
242          */
243         private final int newLevelInt;
244 
245         /***
246          * Create new instance.
247          * @param level comparison level.
248          */
249         public LessThanEqualsRule(final Level level) {
250             super();
251             newLevelInt = level.toInt();
252         }
253 
254         /*** {@inheritDoc} */
255         public boolean evaluate(final LoggingEvent event, Map matches) {
256             Level eventLevel = event.getLevel();
257             boolean result = eventLevel.toInt() <= newLevelInt;
258             if (result && matches != null) {
259                 Set entries = (Set) matches.get(LoggingEventFieldResolver.LEVEL_FIELD);
260                 if (entries == null) {
261                     entries = new HashSet();
262                     matches.put(LoggingEventFieldResolver.LEVEL_FIELD, entries);
263                 }
264                 entries.add(eventLevel);
265             }
266 
267             return result;
268         }
269     }
270 }