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