KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > SyslogHTMLMailFormatter


1 package com.protomatter.syslog;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.io.PrintWriter JavaDoc;
54 import java.util.*;
55 import java.text.*;
56 import java.net.*;
57 import java.io.*;
58
59 import com.protomatter.util.*;
60
61 /**
62  * A simple HTML log entry formatter for email.
63  *
64  * @see com.protomatter.syslog.xml.SyslogHTMLMailFormatter_Helper XML configuration class
65  */

66 public class SyslogHTMLMailFormatter
67 extends SimpleSyslogTextFormatter
68 {
69   private static char[] DEBUG = "DEBUG".toCharArray();
70   private static char[] INFO = "INFO".toCharArray();
71   private static char[] WARNING = "WARNING".toCharArray();
72   private static char[] ERROR = "ERROR".toCharArray();
73   private static char[] FATAL = "FATAL".toCharArray();
74   private static char[] UNKNOWN_LEVEL = "????".toCharArray();
75
76   /**
77    * Default constructor.
78    */

79   public SyslogHTMLMailFormatter()
80   {
81     super();
82     setDateFormat("MM/dd/yyyy HH:mm:ss");
83   }
84
85   /**
86    * Format the given log entry.
87    *
88    * A sample of the text created by this formatter is:<P>
89    *
90    * <TABLE BORDER=1 CELLSPACING=0 CELLPADDING=5>
91    * <TR><TD>
92    *
93    * <TABLE BORDER=0 CELLPADDING=2 CELLSPACING=0>
94    * <TR><TD>Date:</TD><TD><TT>10/31/2000 23:31:25</TT></TD></TR>
95    * <TR><TD>Severity:</TD><TD><TT><B>ERROR</B></TT></TD></TR>
96    * <TR><TD>Channel:</TD><TD><TT>DEFAULT_CHANNEL</TT></TD></TR>
97    * <TR><TD>Hostname:</TD><TD><TT>sassyrobot</TT></TD></TR>
98    * <TR><TD>Thread:</TD><TD><TT>main</TT></TD></TR>
99    * <TR><TD>Logger:</TD><TD><TT>LittleSyslogTest</TT></TD></TR>
100    * </TABLE>
101    * <DL>
102    * <DT>Message: <TT>java.lang.Exception: Happy halloween!</TT></DT>
103    *
104    * <DD><PRE>
105    * java.lang.Exception: Happy halloween!
106    * at LittleSyslogTest.go(LittleSyslogTest.java:31)
107    * at LittleSyslogTest.main(LittleSyslogTest.java:13)
108    * </PRE></DD>
109    * </DL>
110    *
111    * </TD></TR>
112    * </TABLE>
113    */

114   public String JavaDoc formatLogEntry(SyslogMessage message)
115   {
116     StringBuffer JavaDoc b = new StringBuffer JavaDoc(256);
117
118     b.append("<TABLE BORDER=0 CELLPADDING=2 CELLSPACING=0>\n");
119
120     b.append("<TR><TD>");
121     b.append(Syslog.getResourceString(MessageConstants.HTML_DATE_MESSAGE));
122     b.append("</TD><TD><TT>");
123     b.append(formatDate(message.time));
124     b.append("</TT></TD></TR>\n");
125
126     b.append("<TR><TD>");
127     b.append(Syslog.getResourceString(MessageConstants.HTML_SEVERITY_MESSAGE));
128     b.append("</TD><TD><TT><B>");
129     b.append(getStringForLevel(message.level));
130     b.append("</B></TT></TD></TR>\n");
131
132     if (getShowChannel())
133     {
134       b.append("<TR><TD>");
135       b.append(Syslog.getResourceString(MessageConstants.HTML_CHANNEL_MESSAGE));
136       b.append("</TD><TD><TT>");
137       if (message.channel.equals(Syslog.ALL_CHANNEL))
138         b.append("ALL_CHANNEL");
139       else if (message.channel.equals(Syslog.DEFAULT_CHANNEL))
140         b.append("DEFAULT_CHANNEL");
141       else
142         b.append(message.channel);
143       b.append("</TT></TD></TR>\n");
144     }
145
146     if (getShowHostName())
147     {
148       b.append("<TR><TD>");
149       b.append(Syslog.getResourceString(MessageConstants.HTML_HOSTNAME_MESSAGE));
150       b.append("</TD><TD><TT>");
151       b.append(getHostname(message.host));
152       b.append("</TT></TD></TR>\n");
153     }
154
155     if (getShowThreadName())
156     {
157       b.append("<TR><TD>");
158       b.append(Syslog.getResourceString(MessageConstants.HTML_THREAD_MESSAGE));
159       b.append("</TD><TD><TT>");
160       b.append(message.threadName);
161       b.append("</TT></TD></TR>\n");
162     }
163     b.append("<TR><TD>");
164     b.append(Syslog.getResourceString(MessageConstants.HTML_LOGGER_MESSAGE));
165     b.append("</TD><TD><TT>");
166     formatLoggerClassName(b, message);
167     //b.append(message.loggerClassname);
168
b.append("</TT></TD></TR>\n");
169     b.append("</TABLE>\n");
170
171     b.append("<DL>\n");
172     if (message.msg != null)
173     {
174       b.append("<DT>");
175       b.append(Syslog.getResourceString(MessageConstants.HTML_MESSAGE_MESSAGE));
176       b.append("<TT> ");
177       b.append(message.msg);
178       b.append("</TT></DT>\n");
179     }
180     if (message.detail != null)
181     {
182       b.append("<DD><PRE>\n");
183       formatMessageDetail(b, message);
184       b.append("</PRE></DD>\n");
185     }
186     b.append("</DL>\n");
187     return b.toString();
188   }
189
190   protected char[] getStringForLevel(int level)
191   {
192     switch (level)
193     {
194       case Syslog.DEBUG: return DEBUG;
195       case Syslog.INFO: return INFO;
196       case Syslog.WARNING: return WARNING;
197       case Syslog.ERROR: return ERROR;
198       case Syslog.FATAL: return FATAL;
199       default: return UNKNOWN_LEVEL;
200     }
201   }
202 }
203
Popular Tags