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