KickJava   Java API By Example, From Geeks To Geeks.

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


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 mail subject formatter. This class is used by
63  * default by the <tt>MailLog</tt> logger.
64  *
65  * @see MailLog
66  * @see com.protomatter.syslog.xml.SimpleSyslogMailSubjectFormatter_Helper XML configuration class
67  */

68 public class SimpleSyslogMailSubjectFormatter
69 implements SyslogMailSubjectFormatter
70 {
71   private static String JavaDoc DEBUG = "DEBUG".intern();
72   private static String JavaDoc INFO = "INFO".intern();
73   private static String JavaDoc WARNING = "WARNING".intern();
74   private static String JavaDoc ERROR = "ERROR".intern();
75   private static String JavaDoc FATAL = "FATAL".intern();
76   private static String JavaDoc UNKNOWN_LEVEL = "????".intern();
77
78   private boolean showCaller = true;
79   private boolean showChannel = false;
80   private boolean showThreadName = false;
81   private boolean showHostName = false;
82
83   /**
84    * Default constructor.
85    */

86   public SimpleSyslogMailSubjectFormatter()
87   {
88     super();
89   }
90
91   /**
92    * Format the given log entry.
93    */

94   public String JavaDoc formatMessageSubject(SyslogMessage message)
95   {
96     StringBuffer JavaDoc b = new StringBuffer JavaDoc(64);
97     b.append(getStringForLevel(message.level));
98     b.append(": ");
99
100     if (showChannel)
101     {
102       b.append("[");
103       if (message.channel.equals(Syslog.ALL_CHANNEL))
104         b.append("ALL_CHANNEL");
105       else if (message.channel.equals(Syslog.DEFAULT_CHANNEL))
106         b.append("DEFAULT_CHANNEL");
107       else
108         b.append(message.channel);
109       b.append("] ");
110     }
111
112     if (showHostName)
113     {
114       b.append("[");
115       b.append(StringUtil.pad(getHostname(message.host), 10));
116       b.append("] ");
117     }
118
119     if (showThreadName)
120     {
121       b.append("[");
122       b.append(message.threadName);
123       b.append("] ");
124     }
125     if (showCaller)
126     {
127       b.append(trimFromLastPeriod(message.loggerClassname));
128     }
129
130     if (message.msg != null)
131     {
132       b.append(": ");
133       b.append(message.msg);
134     }
135     return b.toString();
136   }
137
138   protected String JavaDoc getStringForLevel(int level)
139   {
140     switch (level)
141     {
142       case Syslog.DEBUG: return DEBUG;
143       case Syslog.INFO: return INFO;
144       case Syslog.WARNING: return WARNING;
145       case Syslog.ERROR: return ERROR;
146       case Syslog.FATAL: return FATAL;
147       default: return UNKNOWN_LEVEL;
148     }
149   }
150
151   protected String JavaDoc getHostname(InetAddress host)
152   {
153     String JavaDoc ip = host.getHostAddress();
154     String JavaDoc name = host.getHostName();
155     if (ip.equals(name))
156       return ip;
157
158     int idx = name.indexOf(".");
159     if (idx == -1)
160       return name;
161     return name.substring(0, idx);
162   }
163
164   /**
165    * Set wether we should show the host name in the output.
166    */

167   public void setShowHostName(boolean showHostName)
168   {
169     this.showHostName = showHostName;
170   }
171
172   /**
173    * Get wether we should show the host name in the output.
174    */

175   public boolean getShowHostName()
176   {
177     return this.showHostName;
178   }
179
180   /**
181    * Set wether we should show the thread name in the output.
182    */

183   public void setShowThreadName(boolean showThreadName)
184   {
185     this.showThreadName = showThreadName;
186   }
187
188   /**
189    * Get wether we should show the thread name in the output.
190    */

191   public boolean getShowThreadName()
192   {
193     return this.showThreadName;
194   }
195
196   /**
197    * Set wether we should show the caller name in the output.
198    */

199   public void setShowCaller(boolean showCaller)
200   {
201     this.showCaller = showCaller;
202   }
203
204   /**
205    * Get wether we should show the caller name in the output.
206    */

207   public boolean getShowCaller()
208   {
209     return this.showCaller;
210   }
211
212   /**
213    * Set wether we should show the channel name in the output.
214    */

215   public void setShowChannel(boolean showChannel)
216   {
217     this.showChannel = showChannel;
218   }
219
220   /**
221    * Get wether we should show the channel name in the output.
222    */

223   public boolean getShowChannel()
224   {
225     return this.showChannel;
226   }
227
228   /**
229    * Given something like "foo.bar.Baz" this will return "Baz".
230    */

231   protected String JavaDoc trimFromLastPeriod(String JavaDoc s)
232   {
233     int pos = s.lastIndexOf('.');
234     if (pos >= 0)
235       return s.substring(pos+1);
236     else
237       return s;
238   }
239 }
240
Popular Tags