1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
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 }