KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Date JavaDoc;
54
55 /**
56  * A log formatter that mimics the log output from WebLogic.
57  * This class was contributed by Vik Ganora <vganora@yahoo.com>
58  *
59  * @see com.protomatter.syslog.xml.WlSyslogTextFormatter_Helper XML configuration class
60  */

61 public class WlSyslogTextFormatter
62 extends SimpleSyslogTextFormatter
63 {
64   // intern some strings so they are shared all over the VM.
65
private static char[] cr = System.getProperty("line.separator").toCharArray();
66   private static char rt = '>';
67   private static char[] lf = " <".toCharArray();
68   private static char lab = '<';
69
70   private static char[] DEBUG = "D".toCharArray();
71   private static char[] INFO = "I".toCharArray();
72   private static char[] WARNING = "W".toCharArray();
73   private static char[] ERROR = "E".toCharArray();
74   private static char[] FATAL = "F".toCharArray();
75   private static char[] UNKNOWN_LEVEL = "?".toCharArray();
76
77   public WlSyslogTextFormatter()
78   {
79     super();
80     setDateFormat("EEE MMM dd HH:mm:ss zzz yyyy:");
81   }
82
83   public void formatLogEntry(StringBuffer JavaDoc b, SyslogMessage message)
84   {
85     b.append(formatDate(message.time));
86     b.append(lab);
87     b.append(getStringForLevel(message.level));
88     b.append(rt);
89
90     b.append(lf);
91     //b.append(trimFromLastPeriod(message.loggerClassname));
92
formatLoggerClassName(b, message);
93     b.append(rt);
94
95     if (getShowChannel())
96     {
97       b.append(lf);
98       if (message.channel.equals(Syslog.ALL_CHANNEL))
99         b.append("ALL_CHANNEL");
100       else if (message.channel.equals(Syslog.DEFAULT_CHANNEL))
101         b.append("DEFAULT_CHANNEL");
102       else
103         b.append(message.channel);
104       b.append(rt);
105     }
106
107     if (getShowHostName())
108     {
109       b.append(lf);
110       b.append(getHostname(message.host));
111       b.append(rt);
112     }
113
114     if (getShowThreadName())
115     {
116       b.append(lf);
117       b.append(message.threadName);
118       b.append(rt);
119     }
120
121     if (message.msg != null)
122     {
123       b.append(" ");
124       b.append(message.msg);
125     }
126     b.append(cr);
127     if (message.detail != null)
128     {
129       formatMessageDetail(b, message);
130       b.append(cr);
131     }
132   }
133
134   protected char[] getStringForLevel(int level)
135   {
136     switch (level)
137     {
138       case Syslog.DEBUG: return DEBUG;
139       case Syslog.INFO: return INFO;
140       case Syslog.WARNING: return WARNING;
141       case Syslog.ERROR: return ERROR;
142       case Syslog.FATAL: return FATAL;
143       default: return UNKNOWN_LEVEL;
144     }
145   }
146
147 }
148
Popular Tags