View Javadoc

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