View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core;
15  
16  import java.util.List;
17  
18  import ch.qos.logback.core.filter.Filter;
19  import ch.qos.logback.core.spi.ContextAwareBase;
20  import ch.qos.logback.core.spi.FilterAttachableImpl;
21  import ch.qos.logback.core.spi.FilterReply;
22  import ch.qos.logback.core.status.WarnStatus;
23  
24  /**
25   * Similar to AppenderBase except that derived appenders need to handle thread
26   * synchronization on their own.
27   * 
28   * @author Ceki Gülcü
29   * @author Ralph Goers
30   */
31  abstract public class UnsynchronizedAppenderBase<E> extends ContextAwareBase implements Appender<E> {
32  
33      protected boolean started = false;
34  
35      // using a ThreadLocal instead of a boolean add 75 nanoseconds per
36      // doAppend invocation. This is tolerable as doAppend takes at least a few
37      // microseconds
38      // on a real appender
39      /**
40       * The guard prevents an appender from repeatedly calling its own doAppend
41       * method.
42       */
43      private ThreadLocal<Boolean> guard = new ThreadLocal<Boolean>();
44  
45      /**
46       * Appenders are named.
47       */
48      protected String name;
49  
50      private FilterAttachableImpl<E> fai = new FilterAttachableImpl<E>();
51  
52      public String getName() {
53          return name;
54      }
55  
56      private int statusRepeatCount = 0;
57      private int exceptionCount = 0;
58  
59      static final int ALLOWED_REPEATS = 3;
60  
61      public void doAppend(E eventObject) {
62          // WARNING: The guard check MUST be the first statement in the
63          // doAppend() method.
64  
65          // prevent re-entry.
66          if (Boolean.TRUE.equals(guard.get())) {
67              return;
68          }
69  
70          try {
71              guard.set(Boolean.TRUE);
72  
73              if (!this.started) {
74                  if (statusRepeatCount++ < ALLOWED_REPEATS) {
75                      addStatus(new WarnStatus("Attempted to append to non started appender [" + name + "].", this));
76                  }
77                  return;
78              }
79  
80              if (getFilterChainDecision(eventObject) == FilterReply.DENY) {
81                  return;
82              }
83  
84              // ok, we now invoke derived class' implementation of append
85              this.append(eventObject);
86  
87          } catch (Exception e) {
88              if (exceptionCount++ < ALLOWED_REPEATS) {
89                  addError("Appender [" + name + "] failed to append.", e);
90              }
91          } finally {
92              guard.set(Boolean.FALSE);
93          }
94      }
95  
96      abstract protected void append(E eventObject);
97  
98      /**
99       * Set the name of this appender.
100      */
101     public void setName(String name) {
102         this.name = name;
103     }
104 
105     public void start() {
106         started = true;
107     }
108 
109     public void stop() {
110         started = false;
111     }
112 
113     public boolean isStarted() {
114         return started;
115     }
116 
117     public String toString() {
118         return this.getClass().getName() + "[" + name + "]";
119     }
120 
121     public void addFilter(Filter<E> newFilter) {
122         fai.addFilter(newFilter);
123     }
124 
125     public void clearAllFilters() {
126         fai.clearAllFilters();
127     }
128 
129     public List<Filter<E>> getCopyOfAttachedFiltersList() {
130         return fai.getCopyOfAttachedFiltersList();
131     }
132 
133     public FilterReply getFilterChainDecision(E event) {
134         return fai.getFilterChainDecision(event);
135     }
136 }