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  import org.apache.logging.log4j.util.PerformanceSensitive;
30  
31  /**
32   * This filter returns the onMatch result if the marker in the LogEvent is the same as or has the
33   * configured marker as a parent.
34   */
35  @Plugin(name = "MarkerFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true)
36  @PerformanceSensitive("allocation")
37  public final class MarkerFilter extends AbstractFilter {
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 Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
75              final Object p0) {
76          return filter(marker);
77      }
78  
79      @Override
80      public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
81              final Object p0, final Object p1) {
82          return filter(marker);
83      }
84  
85      @Override
86      public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
87              final Object p0, final Object p1, final Object p2) {
88          return filter(marker);
89      }
90  
91      @Override
92      public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
93              final Object p0, final Object p1, final Object p2, final Object p3) {
94          return filter(marker);
95      }
96  
97      @Override
98      public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
99              final Object p0, final Object p1, final Object p2, final Object p3,
100             final Object p4) {
101         return filter(marker);
102     }
103 
104     @Override
105     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
106             final Object p0, final Object p1, final Object p2, final Object p3,
107             final Object p4, final Object p5) {
108         return filter(marker);
109     }
110 
111     @Override
112     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
113             final Object p0, final Object p1, final Object p2, final Object p3,
114             final Object p4, final Object p5, final Object p6) {
115         return filter(marker);
116     }
117 
118     @Override
119     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
120             final Object p0, final Object p1, final Object p2, final Object p3,
121             final Object p4, final Object p5, final Object p6,
122             final Object p7) {
123         return filter(marker);
124     }
125 
126     @Override
127     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
128             final Object p0, final Object p1, final Object p2, final Object p3,
129             final Object p4, final Object p5, final Object p6,
130             final Object p7, final Object p8) {
131         return filter(marker);
132     }
133 
134     @Override
135     public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
136             final Object p0, final Object p1, final Object p2, final Object p3,
137             final Object p4, final Object p5, final Object p6,
138             final Object p7, final Object p8, final Object p9) {
139         return filter(marker);
140     }
141 
142     @Override
143     public String toString() {
144         return name;
145     }
146 
147     /**
148      * Create the MarkerFilter.
149      * @param marker The Marker name to match.
150      * @param match The action to take if a match occurs.
151      * @param mismatch The action to take if no match occurs.
152      * @return A MarkerFilter.
153      */
154     @PluginFactory
155     public static MarkerFilter createFilter(
156             @PluginAttribute("marker") final String marker,
157             @PluginAttribute("onMatch") final Result match,
158             @PluginAttribute("onMismatch") final Result mismatch) {
159 
160         if (marker == null) {
161             LOGGER.error("A marker must be provided for MarkerFilter");
162             return null;
163         }
164         return new MarkerFilter(marker, match, mismatch);
165     }
166 
167 }