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.AbstractLifeCycle;
22  import org.apache.logging.log4j.core.Filter;
23  import org.apache.logging.log4j.core.LogEvent;
24  import org.apache.logging.log4j.core.Logger;
25  import org.apache.logging.log4j.message.Message;
26  
27  /**
28   * Users should extend this class to implement filters. Filters can be either context wide or attached to
29   * an appender. A filter may choose to support being called only from the context or only from an appender in
30   * which case it will only implement the required method(s). The rest will default to return NEUTRAL.
31   *
32   */
33  public abstract class AbstractFilter extends AbstractLifeCycle implements Filter {
34      
35      /**
36       * The onMatch Result.
37       */
38      protected final Result onMatch;
39  
40      /**
41       * The onMismatch Result.
42       */
43      protected final Result onMismatch;
44  
45      /**
46       * The default constructor.
47       */
48      protected AbstractFilter() {
49          this(null, null);
50      }
51  
52      /**
53       * Constructor that allows the onMatch and onMismatch actions to be set.
54       * @param onMatch The result to return when a match occurs.
55       * @param onMismatch The result to return when a match dos not occur.
56       */
57      protected AbstractFilter(final Result onMatch, final Result onMismatch) {
58          this.onMatch = onMatch == null ? Result.NEUTRAL : onMatch;
59          this.onMismatch = onMismatch == null ? Result.DENY : onMismatch;
60      }
61  
62      /**
63       * Returns the Result to be returned when a match does not occur.
64       * @return the onMismatch Result.
65       */
66      @Override
67      public final Result getOnMismatch() {
68          return onMismatch;
69      }
70  
71      /**
72       * Returns the Result to be returned when a match occurs.
73       * @return the onMatch Result.
74       */
75      @Override
76      public final Result getOnMatch() {
77          return onMatch;
78      }
79  
80      @Override
81      public String toString() {
82          return this.getClass().getSimpleName();
83      }
84  
85      /**
86       * Appender Filter method. The default returns NEUTRAL.
87       * @param logger the Logger.
88       * @param level The logging Level.
89       * @param marker The Marker, if any.
90       * @param msg The message, if present.
91       * @param params An array of parameters or null.
92       * @return The Result of filtering.
93       */
94      @Override
95      public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
96                           final Object... params) {
97          return Result.NEUTRAL;
98      }
99  
100     /**
101      * Appender Filter method. The default returns NEUTRAL.
102      * @param logger the Logger.
103      * @param level The logging Level.
104      * @param marker The Marker, if any.
105      * @param msg The message, if present.
106      * @param t A throwable or null.
107      * @return The Result of filtering.
108      */
109     @Override
110     public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
111                          final Throwable t) {
112         return Result.NEUTRAL;
113     }
114 
115     /**
116      * Appender Filter method. The default returns NEUTRAL.
117      * @param logger the Logger.
118      * @param level The logging Level.
119      * @param marker The Marker, if any.
120      * @param msg The message, if present.
121      * @param t A throwable or null.
122      * @return The Result of filtering.
123      */
124     @Override
125     public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
126                          final Throwable t) {
127         return Result.NEUTRAL;
128     }
129 
130     /**
131      * Context Filter method. The default returns NEUTRAL.
132      * @param event The LogEvent.
133      * @return The Result of filtering.
134      */
135     @Override
136     public Result filter(final LogEvent event) {
137         return Result.NEUTRAL;
138     }
139 }