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.config;
18  
19  import java.io.Serializable;
20  
21  import org.apache.logging.log4j.Level;
22  import org.apache.logging.log4j.core.Appender;
23  import org.apache.logging.log4j.core.Filter;
24  import org.apache.logging.log4j.core.LogEvent;
25  import org.apache.logging.log4j.core.appender.AppenderRuntimeException;
26  import org.apache.logging.log4j.core.filter.AbstractFilterable;
27  import org.apache.logging.log4j.core.filter.Filterable;
28  
29  /**
30   * Wraps an {@link Appender} with details an appender implementation shouldn't need to know about.
31   * @param <T> The appender's Serializable type.
32   */
33  public class AppenderControl<T extends Serializable> extends AbstractFilterable {
34  
35      private final ThreadLocal<AppenderControl<T>> recursive = new ThreadLocal<AppenderControl<T>>();
36  
37      private final Appender<T> appender;
38  
39      private final Level level;
40  
41      private final int intLevel;
42  
43      /**
44       * Constructor.
45       * @param appender The target Appender.
46       * @param level the Level to filter on.
47       * @param filter the Filter(s) to apply.
48       */
49      public AppenderControl(final Appender<T> appender, final Level level, final Filter filter) {
50          super(filter);
51          this.appender = appender;
52          this.level = level;
53          this.intLevel = level == null ? Level.ALL.intLevel() : level.intLevel();
54          startFilter();
55      }
56  
57      /**
58       * Returns the Appender.
59       * @return the Appender.
60       */
61      public Appender<T> getAppender() {
62          return appender;
63      }
64  
65      /**
66       * Call the appender.
67       * @param event The event to process.
68       */
69      public void callAppender(final LogEvent event) {
70          if (getFilter() != null) {
71              final Filter.Result r = getFilter().filter(event);
72              if (r == Filter.Result.DENY) {
73                  return;
74              }
75          }
76          if (level != null) {
77              if (intLevel < event.getLevel().intLevel()) {
78                  return;
79              }
80          }
81          if (recursive.get() != null) {
82              appender.getHandler().error("Recursive call to appender " + appender.getName());
83              return;
84          }
85          try {
86              recursive.set(this);
87  
88              if (!appender.isStarted()) {
89                  appender.getHandler().error("Attempted to append to non-started appender " + appender.getName());
90  
91                  if (!appender.isExceptionSuppressed()) {
92                      throw new AppenderRuntimeException(
93                          "Attempted to append to non-started appender " + appender.getName());
94                  }
95              }
96  
97              if (appender instanceof Filterable && ((Filterable) appender).isFiltered(event)) {
98                  return;
99              }
100 
101             try {
102                 appender.append(event);
103             } catch (final RuntimeException ex) {
104                 appender.getHandler().error("An exception occurred processing Appender " + appender.getName(), ex);
105                 if (!appender.isExceptionSuppressed()) {
106                     throw ex;
107                 }
108             } catch (final Exception ex) {
109                 appender.getHandler().error("An exception occurred processing Appender " + appender.getName(), ex);
110                 if (!appender.isExceptionSuppressed()) {
111                     throw new AppenderRuntimeException(ex);
112                 }
113             }
114         } finally {
115             recursive.set(null);
116         }
117     }
118 
119 }