View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2009, 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.classic.net;
15  
16  import ch.qos.logback.classic.PatternLayout;
17  import ch.qos.logback.classic.boolex.OnErrorEvaluator;
18  import ch.qos.logback.classic.spi.ILoggingEvent;
19  import ch.qos.logback.core.Layout;
20  import ch.qos.logback.core.boolex.EventEvaluator;
21  import ch.qos.logback.core.helpers.CyclicBuffer;
22  import ch.qos.logback.core.net.SMTPAppenderBase;
23  
24  /**
25   * Send an e-mail when a specific logging event occurs, typically on errors or
26   * fatal errors.
27   * 
28   * For more information about this appender, please refer to the online manual at
29   * http://logback.qos.ch/manual/appenders.html#SMTPAppender
30   * 
31   * @author Ceki Gülcü
32   * @author Sébastien Pennec
33   * 
34   */
35  public class SMTPAppender extends SMTPAppenderBase<ILoggingEvent> {
36  
37    // value "%logger{20} - %m" is referenced in the docs!
38    static final String DEFAULT_SUBJECT_PATTERN = "%logger{20} - %m";
39    
40    private int bufferSize = 512;
41    protected CyclicBuffer<ILoggingEvent> cb = new CyclicBuffer<ILoggingEvent>(bufferSize);
42  
43    /**
44     * The default constructor will instantiate the appender with a
45     * {@link EventEvaluator} that will trigger on events with level
46     * ERROR or higher.
47     */
48    public SMTPAppender() {
49  
50    }
51    
52    public void start() {    
53      if (eventEvaluator == null) {
54        OnErrorEvaluator onError = new OnErrorEvaluator();
55        onError.setContext(getContext());
56        onError.setName("onError");
57        onError.start();
58        this.eventEvaluator = onError;      
59      }
60      super.start();
61    }
62  
63    /**
64     * Use the parameter as the {@link
65     * EventEvaluator} for this SMTPAppender.
66     */
67    public SMTPAppender(EventEvaluator<ILoggingEvent> eventEvaluator) {
68      this.eventEvaluator = eventEvaluator;
69    }
70  
71    /**
72     * Perform SMTPAppender specific appending actions, mainly adding the event to
73     * a cyclic buffer.
74     */
75    protected void subAppend(ILoggingEvent event) {
76      event.prepareForDeferredProcessing();
77      cb.add(event);
78      // addInfo("Added event to the cyclic buffer: " + event.getMessage());
79    }
80  
81    @Override
82    protected void fillBuffer(StringBuffer sbuf) {
83      int len = cb.length();
84      for (int i = 0; i < len; i++) {
85        // sbuf.append(MimeUtility.encodeText(layout.format(cb.get())));
86        ILoggingEvent event = cb.get();
87        sbuf.append(layout.doLayout(event));
88      }
89    }
90  
91    /**
92     * The <b>BufferSize</b> option takes a positive integer representing the
93     * maximum number of logging events to collect in a cyclic buffer. When the
94     * <code>BufferSize</code> is reached, oldest events are deleted as new
95     * events are added to the buffer. By default the size of the cyclic buffer is
96     * 512 events.
97     */
98    public void setBufferSize(int bufferSize) {
99      this.bufferSize = bufferSize;
100     cb.resize(bufferSize);
101   }
102 
103   /**
104    * Returns value of the <b>BufferSize</b> option.
105    */
106   public int getBufferSize() {
107     return bufferSize;
108   }
109 
110   @Override
111   protected Layout<ILoggingEvent> makeSubjectLayout(String subjectStr) {
112     if(subjectStr == null) {
113       subjectStr = DEFAULT_SUBJECT_PATTERN;
114     }
115     PatternLayout pl = new PatternLayout();
116     pl.setContext(getContext());
117     pl.setPattern(subjectStr);
118     // we don't want a ThrowableInformationConverter appended
119     // to the end of the converter chain
120     // This fixes issue LBCLASSIC-67
121     pl.setPostCompileProcessor(null);
122     pl.start();
123     return pl;
124   }
125 }