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