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.Lifecycle;
23  import org.apache.logging.log4j.core.LogEvent;
24  import org.apache.logging.log4j.core.appender.AppenderRuntimeException;
25  import org.apache.logging.log4j.core.filter.Filterable;
26  import org.apache.logging.log4j.core.filter.Filtering;
27  
28  /**
29   * Wraps appenders with details the appender implementation shouldn't need to know about.
30   */
31  public class AppenderControl extends Filterable {
32  
33      private ThreadLocal<AppenderControl> recursive = new ThreadLocal<AppenderControl>();
34  
35      private final Appender appender;
36  
37      private final Level level;
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(Appender appender, Level level, 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       * Return 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(LogEvent event) {
67          if (getFilter() != null) {
68              Filter.Result r = getFilter().filter(event);
69              if (r == Filter.Result.DENY) {
70                  return;
71              }
72          }
73          if (level != null) {
74              if (intLevel < event.getLevel().intLevel()) {
75                  return;
76              }
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 instanceof Lifecycle && !appender.isStarted()) {
86                  appender.getHandler().error("Attempted to append to non-started appender " + appender.getName());
87  
88                  if (!appender.isExceptionSuppressed()) {
89                      throw new AppenderRuntimeException(
90                          "Attempted to append to non-started appender " + appender.getName());
91                  }
92              }
93  
94              if (appender instanceof Filtering && ((Filtering) appender).isFiltered(event)) {
95                  return;
96              }
97  
98              try {
99                  appender.append(event);
100             } catch (RuntimeException ex) {
101                 appender.getHandler().error("An exception occurred processing Appender " + appender.getName(), ex);
102                 if (!appender.isExceptionSuppressed()) {
103                     throw ex;
104                 }
105             } catch (Exception ex) {
106                 appender.getHandler().error("An exception occurred processing Appender " + appender.getName(), ex);
107                 if (!appender.isExceptionSuppressed()) {
108                     throw new AppenderRuntimeException(ex);
109                 }
110             }
111         } finally {
112             recursive.set(null);
113         }
114     }
115 
116 }