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 org.apache.logging.log4j.Level;
20  import org.apache.logging.log4j.Marker;
21  import org.apache.logging.log4j.core.Filter;
22  import org.apache.logging.log4j.core.LogEvent;
23  import org.apache.logging.log4j.core.Logger;
24  import org.apache.logging.log4j.core.config.Node;
25  import org.apache.logging.log4j.core.config.plugins.Plugin;
26  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
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  @Plugin(name = "MarkerFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true)
35  public final class MarkerFilter extends AbstractFilter {
36  
37      private static final long serialVersionUID = 1L;
38  
39      private final String name;
40  
41      private MarkerFilter(final String name, final Result onMatch, final Result onMismatch) {
42          super(onMatch, onMismatch);
43          this.name = name;
44      }
45  
46      @Override
47      public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
48                           final Object... params) {
49          return filter(marker);
50      }
51  
52      @Override
53      public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
54                           final Throwable t) {
55          return filter(marker);
56      }
57  
58      @Override
59      public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
60                           final Throwable t) {
61          return filter(marker);
62      }
63  
64      @Override
65      public Result filter(final LogEvent event) {
66          return filter(event.getMarker());
67      }
68  
69      private Result filter(final Marker marker) {
70          return marker != null && marker.isInstanceOf(name) ? onMatch : onMismatch;
71      }
72  
73      @Override
74      public String toString() {
75          return name;
76      }
77  
78      /**
79       * Create the MarkerFilter.
80       * @param marker The Marker name to match.
81       * @param match The action to take if a match occurs.
82       * @param mismatch The action to take if no match occurs.
83       * @return A MarkerFilter.
84       */
85      @PluginFactory
86      public static MarkerFilter createFilter(
87              @PluginAttribute("marker") final String marker,
88              @PluginAttribute("onMatch") final Result match,
89              @PluginAttribute("onMismatch") final Result mismatch) {
90  
91          if (marker == null) {
92              LOGGER.error("A marker must be provided for MarkerFilter");
93              return null;
94          }
95          return new MarkerFilter(marker, match, mismatch);
96      }
97  
98  }