KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
54 import java.util.HashMap JavaDoc;
55 import java.text.MessageFormat JavaDoc;
56 import com.protomatter.util.*;
57
58 /**
59  * A utility class for writing log messages to channels.
60  * Example usage of this class is as follows:<P>
61  *
62  * <UL><TABLE BORDER=1 CELLPADDING=4 CELLSPACING=0 WIDTH="90%">
63  * <TR><TD><PRE>
64  *
65  * // get the channel object
66  * Channel myChannel = Channel.getChannel("MY CHANNEL");
67  *
68  * myChannel.info(this, "Hello there");
69  *
70  * // get the default chanel object
71  * Channel default = Channel.getDefaultChannel();
72  *
73  * default.info(this, "Hello there");
74  *
75  *
76  * // You can also write to multiple channels
77  * String channels[] = new String[] { "first-channel", "second-channel" };
78  * Channel lotsaChannels = Channel.getChannel(channels);
79  *
80  * lotsChannels.info(this, "Hello there");
81  *
82  *
83  * // And finally, you can delegate the list of channels to
84  * // an object that implements the SyslogChannelAware interface
85  * SyslogChannelAware channelAware = new MyChannelAwareObject();
86  * Channel delegatedChannel = Channel.getChannel(channelAware);
87  *
88  * delegatedChannel.info(this, "Hello there");
89  *
90  * </PRE></TD></TR></TABLE></UL><P>
91  *
92  * Basically, rather than using the <TT>infoToChannel(...)</TT>
93  * method and others on <TT>Syslog</TT>, you can simply get a
94  * handle to a channel and call methods on it. Some people
95  * prefer this interface to the regular <tt>Syslog</tt> interface.
96  */

97 public final class Channel
98 {
99     /**
100      * A static utility field, which is the "all" channel.
101      */

102     public static final Channel ALL = Channel.getAllChannel();
103
104     /**
105      * A static utility field, which is the "default" channel.
106      */

107     public static final Channel DEFAULT = Channel.getDefaultChannel();
108
109     // one or the other of these two is null
110
private String JavaDoc channels[] = null;
111     private SyslogChannelAware channelAware = null;
112
113     private Channel(String JavaDoc channels[])
114     {
115         this.channels = channels;
116     }
117
118     private Channel(SyslogChannelAware channelAware)
119     {
120         this.channelAware = channelAware;
121     }
122
123     private final Object JavaDoc getChannelForLogging()
124     {
125         return (channels != null) ? channels : channelAware.getSyslogChannel();
126     }
127
128     /**
129      * Get the list of channels that this <tt>Channel</tt> object writes to.
130      * This method will either return the channel name(s) passed
131      * into the <tt>getChannel()</tt> method that created this
132      * object, or it will ask the <tt>SyslogChannelAware</tt> object
133      * that this <tt>Channel</tt> is associated with what channels
134      * it thinks we should write to.
135      */

136     public String JavaDoc[] getChannelNames()
137     {
138         return Syslog.convertChannelObject(getChannelForLogging());
139     }
140
141     /**
142      * Get a Channel object for the given channel name.
143      */

144     public static Channel getChannel(String JavaDoc channelName)
145     {
146         return getChannel(new String JavaDoc[] { channelName });
147     }
148
149     /**
150      * Get a Channel object with the name of the given class.
151      */

152     public static Channel getChannel(Class JavaDoc channelClass)
153     {
154         return getChannel(new String JavaDoc[] { channelClass.getName() });
155     }
156
157     /**
158      * Get a Channel whose name is the package
159      * that the given class is in. For instace,
160      * if you pass in <tt>com.yourcompany.foo.SomeClass</tt>
161      * the channel name will be <tt>com.yourcompany.foo</tt>.
162      */

163     public static Channel forPackage(Class JavaDoc someClass)
164     {
165         String JavaDoc packageName = someClass.getName();
166         int pos = packageName.lastIndexOf('.');
167         return getChannel(new String JavaDoc[] { packageName.substring(0, pos) });
168     }
169
170     /**
171      * Get a Channel object for the given set of channels. Each
172      * logging call will send messages to each of these channels.
173      */

174     public static Channel getChannel(String JavaDoc channels[])
175     {
176         return new Channel(channels);
177     }
178
179     /**
180      * Get a Channel object which will delegate to the given
181      * channel aware object. Each logging call will call
182      * the <tt>getSyslogChannel()</tt> method on the given
183      * <tt>SyslogChannelAware</tt> object to get the channel
184      * name (or list of channel names) to send messages to.
185      *
186      * @see SyslogChannelAware
187      */

188     public static Channel getChannel(SyslogChannelAware channelAware)
189     {
190         return new Channel(channelAware);
191     }
192
193     /**
194      * Get a Channel object for the "all" channel.
195      */

196     public static Channel getAllChannel()
197     {
198         return getChannel(Syslog.ALL_CHANNEL);
199     }
200
201     /**
202      * Get a Channel object for the default channel.
203      */

204     public static Channel getDefaultChannel()
205     {
206         return getChannel(Syslog.DEFAULT_CHANNEL);
207     }
208
209     /**
210      * Log a debug message.
211      */

212     public void debug(Object JavaDoc caller, Object JavaDoc message)
213     {
214         Syslog.log(Syslog.getLocalHostName(), caller, getChannelForLogging(), message, null, Syslog.DEBUG,
215             Thread.currentThread(), Thread.currentThread().getName(),
216             System.currentTimeMillis(), 1);
217     }
218
219     /**
220      * Log a debug message.
221      */

222     public void debug(Object JavaDoc caller, Object JavaDoc message, Object JavaDoc detail)
223     {
224         Syslog.log(Syslog.getLocalHostName(), caller, getChannelForLogging(), message, detail, Syslog.DEBUG,
225             Thread.currentThread(), Thread.currentThread().getName(),
226             System.currentTimeMillis(), 1);
227     }
228
229     /**
230      * Log an info message.
231      */

232     public void info(Object JavaDoc caller, Object JavaDoc message)
233     {
234         Syslog.log(Syslog.getLocalHostName(), caller, getChannelForLogging(), message, null, Syslog.INFO,
235             Thread.currentThread(), Thread.currentThread().getName(),
236             System.currentTimeMillis(), 1);
237     }
238
239     /**
240      * Log an info message.
241      */

242     public void info(Object JavaDoc caller, Object JavaDoc message, Object JavaDoc detail)
243     {
244         Syslog.log(Syslog.getLocalHostName(), caller, getChannelForLogging(), message, detail, Syslog.INFO,
245             Thread.currentThread(), Thread.currentThread().getName(),
246             System.currentTimeMillis(), 1);
247     }
248
249     /**
250      * Log a warning message.
251      */

252     public void warning(Object JavaDoc caller, Object JavaDoc message)
253     {
254         Syslog.log(Syslog.getLocalHostName(), caller, getChannelForLogging(), message, null, Syslog.WARNING,
255             Thread.currentThread(), Thread.currentThread().getName(),
256             System.currentTimeMillis(), 1);
257     }
258
259     /**
260      * Log a warning message.
261      */

262     public void warning(Object JavaDoc caller, Object JavaDoc message, Object JavaDoc detail)
263     {
264         Syslog.log(Syslog.getLocalHostName(), caller, getChannelForLogging(), message, detail, Syslog.WARNING,
265             Thread.currentThread(), Thread.currentThread().getName(),
266             System.currentTimeMillis(), 1);
267     }
268
269     /**
270      * Log an error message.
271      */

272     public void error(Object JavaDoc caller, Object JavaDoc message)
273     {
274         Syslog.log(Syslog.getLocalHostName(), caller, getChannelForLogging(), message, null, Syslog.ERROR,
275             Thread.currentThread(), Thread.currentThread().getName(),
276             System.currentTimeMillis(), 1);
277     }
278
279     /**
280      * Log an error message.
281      */

282     public void error(Object JavaDoc caller, Object JavaDoc message, Object JavaDoc detail)
283     {
284         Syslog.log(Syslog.getLocalHostName(), caller, getChannelForLogging(), message, detail, Syslog.ERROR,
285             Thread.currentThread(), Thread.currentThread().getName(),
286             System.currentTimeMillis(), 1);
287     }
288
289     /**
290      * Log a fatal message.
291      */

292     public void fatal(Object JavaDoc caller, Object JavaDoc message)
293     {
294         Syslog.log(Syslog.getLocalHostName(), caller, getChannelForLogging(), message, null, Syslog.FATAL,
295             Thread.currentThread(), Thread.currentThread().getName(),
296             System.currentTimeMillis(), 1);
297     }
298
299     /**
300      * Log a fatal message.
301      */

302     public void fatal(Object JavaDoc caller, Object JavaDoc message, Object JavaDoc detail)
303     {
304         Syslog.log(Syslog.getLocalHostName(), caller, getChannelForLogging(), message, detail, Syslog.FATAL,
305             Thread.currentThread(), Thread.currentThread().getName(),
306             System.currentTimeMillis(), 1);
307     }
308
309     /**
310      * Log a message about the given exception. The
311      * <TT>toString()</TT> method on the exception is used as
312      * the log message. A stack trace from the exception is used
313      * as the message detail, and the message is logged at
314      * the <TT>ERROR</TT> level.
315      */

316     public void log(Object JavaDoc caller, Throwable JavaDoc exception)
317     {
318         Syslog.log(Syslog.getLocalHostName(), caller, getChannelForLogging(), exception.toString(), exception, Syslog.ERROR,
319             Thread.currentThread(), Thread.currentThread().getName(),
320             System.currentTimeMillis(), 1);
321     }
322
323     /**
324      * Log a message.
325      */

326     public void log(Object JavaDoc caller, Object JavaDoc message, Object JavaDoc detail, int level)
327     {
328         Syslog.log(Syslog.getLocalHostName(), caller, getChannelForLogging(), message, detail, level,
329             Thread.currentThread(), Thread.currentThread().getName(),
330             System.currentTimeMillis(), 1);
331     }
332
333     /**
334      * Log a breadcrumb at the debug level. The breadcrumb
335      * includes information from a <tt><a HREF="../util/StackTraceInfo.html">StackTraceInfo</a></tt>
336      * object.
337      */

338     public void crumb(Object JavaDoc caller)
339     {
340         Syslog.log(Syslog.getLocalHostName(), caller, getChannelForLogging(),
341             MessageFormat.format(Syslog.getResourceString(MessageConstants.CRUMB_MESSAGE),
342             new Object JavaDoc[] { Syslog.whereAmI(1) }),
343             null, Syslog.DEBUG, Thread.currentThread(), Thread.currentThread().getName(),
344             System.currentTimeMillis(), 1);
345     }
346
347     /**
348      * Log a breadcrumb at the debug level. The breadcrumb
349      * includes information from a <tt><a HREF="../util/StackTraceInfo.html">StackTraceInfo</a></tt>
350      * object.
351      */

352     public void crumb()
353     {
354         StackTraceInfo trace = StackTraceUtil.whereAmI(1);
355         Syslog.log(Syslog.getLocalHostName(), trace.className, getChannelForLogging(),
356             MessageFormat.format(Syslog.getResourceString(MessageConstants.CRUMB_MESSAGE),
357             new Object JavaDoc[] { trace }),
358             null, Syslog.DEBUG, Thread.currentThread(), Thread.currentThread().getName(),
359             System.currentTimeMillis(), 1);
360     }
361
362 }
363
Popular Tags