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.logging.log4j.core.filter;
18  
19  import java.util.Locale;
20  
21  import org.apache.logging.log4j.Level;
22  import org.apache.logging.log4j.Marker;
23  import org.apache.logging.log4j.core.LogEvent;
24  import org.apache.logging.log4j.core.Logger;
25  import org.apache.logging.log4j.core.config.plugins.Plugin;
26  import org.apache.logging.log4j.core.config.plugins.PluginAttr;
27  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
28  import org.apache.logging.log4j.message.Message;
29  
30  /**
31   * This filter returns the onMatch result if the marker in the LogEvent is the same as or has the
32   * configured marker as a parent.
33   *
34   */
35  @Plugin(name = "MarkerFilter", type = "Core", elementType = "filter", printObject = true)
36  public final class MarkerFilter extends AbstractFilter {
37  
38      private final String name;
39  
40      private MarkerFilter(String name, Result onMatch, Result onMismatch) {
41          super(onMatch, onMismatch);
42          this.name = name;
43      }
44  
45      @Override
46      public Result filter(Logger logger, Level level, Marker marker, String msg, Object[] params) {
47          return filter(marker);
48      }
49  
50      @Override
51      public Result filter(Logger logger, Level level, Marker marker, Object msg, Throwable t) {
52          return filter(marker);
53      }
54  
55      @Override
56      public Result filter(Logger logger, Level level, Marker marker, Message msg, Throwable t) {
57          return filter(marker);
58      }
59  
60      @Override
61      public Result filter(LogEvent event) {
62          return filter(event.getMarker());
63      }
64  
65      private Result filter(Marker marker) {
66          return marker != null && marker.isInstanceOf(name) ? onMatch : onMismatch;
67      }
68  
69      @Override
70      public String toString() {
71          return name;
72      }
73  
74      /**
75       * Create the MarkerFilter.
76       * @param marker The Marker name to match.
77       * @param match The action to take if a match occurs.
78       * @param mismatch The action to take if no match occurs.
79       * @return A MarkerFilter.
80       */
81      @PluginFactory
82      public static MarkerFilter createFilter(@PluginAttr("marker") String marker,
83                                              @PluginAttr("onMatch") String match,
84                                              @PluginAttr("onMismatch") String mismatch) {
85  
86          if (marker == null) {
87              LOGGER.error("A marker must be provided for MarkerFilter");
88              return null;
89          }
90          Result onMatch = Result.toResult(match);
91          Result onMismatch = Result.toResult(mismatch);
92          return new MarkerFilter(marker, onMatch, onMismatch);
93      }
94  
95  }