View Javadoc

1   /*
2   Copyright (c) 2005, CodeSmarts
3    All rights reserved.
4   
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are
7   met:
8   	* 	Redistributions of source code must retain the above copyright
9   notice, this list of conditions and the following disclaimer.
10  	* 	Redistributions in binary form must reproduce the above
11  copyright notice, this list of conditions and the following disclaimer
12  in the documentation and/or other materials provided with the
13  distribution.
14  	* 	Neither the name of the CodeSmarts nor the names of its
15  contributors may be used to endorse or promote products derived from
16  this software without specific prior written permission.
17  
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
19  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30  
31  package net.codesmarts.log4j;
32  
33  import java.util.Iterator;
34  import java.util.List;
35  import org.apache.log4j.Layout;
36  import org.apache.log4j.spi.LoggingEvent;
37  
38  /***
39   * Bug Report appender that sends BugReports via email
40   * @author Fred McCann
41   */
42  public class EmailBugReportAppender extends AbstractEmailBugReportAppender {
43      
44      /***
45       * Layout for the html portion of the message
46       */
47      private Layout htmlLayout = new org.apache.log4j.HTMLLayout();
48  
49      /***
50       * @see net.codesmarts.log4j.AbstractEmailBugReportAppender#getMessage(net.codesmarts.log4j.BugReport)
51       */
52      public String getMessage(BugReport report) {
53          return getMessage(report, getLayout());
54      }
55      
56      /***
57       * @see net.codesmarts.log4j.AbstractEmailBugReportAppender#getHTMLMessage(net.codesmarts.log4j.BugReport)
58       */
59      public String getHTMLMessage(BugReport report) {
60          return getMessage(report, htmlLayout);
61      }
62      
63      /***
64       * Formate report with specified layout
65       * @param report the bug report
66       * @param layout
67       */
68      private String getMessage(BugReport report, Layout layout) {
69          StringBuffer message = new StringBuffer();
70          
71          List events = report.getEvents();
72          LoggingEvent event = null;
73          
74          Iterator i = events.iterator();
75          
76          // Write formatted events to bug report description
77          while (i.hasNext()) {
78              event = (LoggingEvent)i.next();
79              message.append(getLayout().format(event));
80          }
81          
82          return message.toString();
83      }
84      
85      /***
86       * @see net.codesmarts.log4j.AbstractEmailBugReportAppender#getSubject(net.codesmarts.log4j.BugReport)
87       */
88      public String getSubject(BugReport report) {
89          String subject = null;
90  
91          List events = report.getEvents();
92          LoggingEvent event = null;
93          
94          Iterator i = events.iterator();
95  
96          while (i.hasNext() && subject==null) {
97              event = (LoggingEvent)i.next();
98              if (event.getLevel().isGreaterOrEqual(this.getThresholdPriority()))
99                  subject = event.getMessage().toString();
100         }
101         
102         return subject;
103     }
104     
105     /***
106      * @param htmlLayout The htmlLayout to set.
107      */
108     public void setHtmlLayout(Layout htmlLayout) {
109         this.htmlLayout = htmlLayout;
110     }
111 }