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  package org.apache.log4j.builders.filter;
18  
19  import org.apache.log4j.bridge.FilterWrapper;
20  import org.apache.log4j.builders.AbstractBuilder;
21  import org.apache.log4j.builders.BooleanHolder;
22  import org.apache.log4j.builders.Holder;
23  import org.apache.log4j.config.PropertiesConfiguration;
24  import org.apache.log4j.spi.Filter;
25  import org.apache.log4j.xml.XmlConfiguration;
26  import org.apache.logging.log4j.Level;
27  import org.apache.logging.log4j.Logger;
28  import org.apache.logging.log4j.core.config.plugins.Plugin;
29  import org.apache.logging.log4j.core.filter.LevelRangeFilter;
30  import org.apache.logging.log4j.status.StatusLogger;
31  import org.w3c.dom.Element;
32  
33  
34  import java.util.Properties;
35  
36  import static org.apache.log4j.builders.BuilderManager.CATEGORY;
37  import static org.apache.log4j.xml.XmlConfiguration.*;
38  
39  /**
40   * Build a Level match failter.
41   */
42  @Plugin(name = "org.apache.log4j.varia.LevelRangeFilter", category = CATEGORY)
43  public class LevelRangeFilterBuilder extends AbstractBuilder implements FilterBuilder {
44  
45      private static final Logger LOGGER = StatusLogger.getLogger();
46      private static final String LEVEL_MAX = "LevelMax";
47      private static final String LEVEL_MIN = "LevelMin";
48      private static final String ACCEPT_ON_MATCH = "AcceptOnMatch";
49  
50      public LevelRangeFilterBuilder() {
51      }
52  
53      public LevelRangeFilterBuilder(String prefix, Properties props) {
54          super(prefix, props);
55      }
56  
57      @Override
58      public Filter parseFilter(Element filterElement, XmlConfiguration config) {
59          final Holder<String> levelMax = new Holder<>();
60          final Holder<String> levelMin = new Holder<>();
61          final Holder<Boolean> acceptOnMatch = new BooleanHolder();
62          forEachElement(filterElement.getElementsByTagName("param"), (currentElement) -> {
63              if (currentElement.getTagName().equals("param")) {
64                  switch (currentElement.getAttribute(NAME_ATTR)) {
65                      case LEVEL_MAX:
66                          levelMax.set(currentElement.getAttribute(VALUE_ATTR));
67                          break;
68                      case LEVEL_MIN:
69                          levelMax.set(currentElement.getAttribute(VALUE_ATTR));
70                          break;
71                      case ACCEPT_ON_MATCH:
72                          acceptOnMatch.set(Boolean.parseBoolean(currentElement.getAttribute(VALUE_ATTR)));
73                          break;
74                  }
75              }
76          });
77          return createFilter(levelMax.get(), levelMin.get(), acceptOnMatch.get());
78      }
79  
80      @Override
81      public Filter parseFilter(PropertiesConfiguration config) {
82          String levelMax = getProperty(LEVEL_MAX);
83          String levelMin = getProperty(LEVEL_MIN);
84          boolean acceptOnMatch = getBooleanProperty(ACCEPT_ON_MATCH);
85          return createFilter(levelMax, levelMin, acceptOnMatch);
86      }
87  
88      private Filter createFilter(String levelMax, String levelMin, boolean acceptOnMatch) {
89          Level max = Level.FATAL;
90          Level min = Level.TRACE;
91          if (levelMax != null) {
92              max = Level.toLevel(levelMax, Level.FATAL);
93          }
94          if (levelMin != null) {
95              min = Level.toLevel(levelMin, Level.DEBUG);
96          }
97          org.apache.logging.log4j.core.Filter.Result onMatch = acceptOnMatch
98                  ? org.apache.logging.log4j.core.Filter.Result.ACCEPT
99                  : org.apache.logging.log4j.core.Filter.Result.NEUTRAL;
100 
101         return new FilterWrapper(LevelRangeFilter.createFilter(min, max, onMatch,
102                 org.apache.logging.log4j.core.Filter.Result.DENY));
103     }
104 }