View Javadoc

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