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