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