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.AppenderRuntimeException;
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 final ThreadLocal<AppenderControl> recursive = new ThreadLocal<AppenderControl>();
33  
34      private final Appender appender;
35  
36      private final Level level;
37      private final int intLevel;
38  
39      /**
40       * Constructor.
41       * @param appender The target Appender.
42       * @param level the Level to filter on.
43       * @param filter the Filter(s) to apply.
44       */
45      public AppenderControl(Appender appender, Level level, Filter filter) {
46          super(filter);
47          this.appender = appender;
48          this.level = level;
49          this.intLevel = level == null ? Level.ALL.intLevel() : level.intLevel();
50          startFilter();
51      }
52  
53      /**
54       * Returns the Appender.
55       * @return the Appender.
56       */
57      public Appender getAppender() {
58          return appender;
59      }
60  
61      /**
62       * Call the appender.
63       * @param event The event to process.
64       */
65      public void callAppender(LogEvent event) {
66          if (getFilter() != null) {
67              Filter.Result r = getFilter().filter(event);
68              if (r == Filter.Result.DENY) {
69                  return;
70              }
71          }
72          if (level != null) {
73              if (intLevel < event.getLevel().intLevel()) {
74                  return;
75              }
76          }
77          if (recursive.get() != null) {
78              appender.getHandler().error("Recursive call to appender " + appender.getName());
79              return;
80          }
81          try {
82              recursive.set(this);
83  
84              if (!appender.isStarted()) {
85                  appender.getHandler().error("Attempted to append to non-started appender " + appender.getName());
86  
87                  if (!appender.isExceptionSuppressed()) {
88                      throw new AppenderRuntimeException(
89                          "Attempted to append to non-started appender " + appender.getName());
90                  }
91              }
92  
93              if (appender instanceof Filterable && ((Filterable) appender).isFiltered(event)) {
94                  return;
95              }
96  
97              try {
98                  appender.append(event);
99              } catch (RuntimeException ex) {
100                 appender.getHandler().error("An exception occurred processing Appender " + appender.getName(), ex);
101                 if (!appender.isExceptionSuppressed()) {
102                     throw ex;
103                 }
104             } catch (Exception ex) {
105                 appender.getHandler().error("An exception occurred processing Appender " + appender.getName(), ex);
106                 if (!appender.isExceptionSuppressed()) {
107                     throw new AppenderRuntimeException(ex);
108                 }
109             }
110         } finally {
111             recursive.set(null);
112         }
113     }
114 
115 }