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 final String name;
38  
39      private MarkerFilter(final String name, final Result onMatch, final Result onMismatch) {
40          super(onMatch, onMismatch);
41          this.name = name;
42      }
43  
44      @Override
45      public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
46                           final Object... params) {
47          return filter(marker);
48      }
49  
50      @Override
51      public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
52                           final Throwable t) {
53          return filter(marker);
54      }
55  
56      @Override
57      public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
58                           final Throwable t) {
59          return filter(marker);
60      }
61  
62      @Override
63      public Result filter(final LogEvent event) {
64          return filter(event.getMarker());
65      }
66  
67      private Result filter(final Marker marker) {
68          return marker != null && marker.isInstanceOf(name) ? onMatch : onMismatch;
69      }
70  
71      @Override
72      public String toString() {
73          return name;
74      }
75  
76      /**
77       * Create the MarkerFilter.
78       * @param marker The Marker name to match.
79       * @param match The action to take if a match occurs.
80       * @param mismatch The action to take if no match occurs.
81       * @return A MarkerFilter.
82       */
83      @PluginFactory
84      public static MarkerFilter createFilter(
85              @PluginAttribute("marker") final String marker,
86              @PluginAttribute("onMatch") final Result match,
87              @PluginAttribute("onMismatch") final Result mismatch) {
88  
89          if (marker == null) {
90              LOGGER.error("A marker must be provided for MarkerFilter");
91              return null;
92          }
93          return new MarkerFilter(marker, match, mismatch);
94      }
95  
96  }