001: package com.protomatter.syslog;
002:
003: /**
004: * {{{ The Protomatter Software License, Version 1.0
005: * derived from The Apache Software License, Version 1.1
006: *
007: * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution,
022: * if any, must include the following acknowledgment:
023: * "This product includes software developed for the
024: * Protomatter Software Project
025: * (http://protomatter.sourceforge.net/)."
026: * Alternately, this acknowledgment may appear in the software itself,
027: * if and wherever such third-party acknowledgments normally appear.
028: *
029: * 4. The names "Protomatter" and "Protomatter Software Project" must
030: * not be used to endorse or promote products derived from this
031: * software without prior written permission. For written
032: * permission, please contact support@protomatter.com.
033: *
034: * 5. Products derived from this software may not be called "Protomatter",
035: * nor may "Protomatter" appear in their name, without prior written
036: * permission of the Protomatter Software Project
037: * (support@protomatter.com).
038: *
039: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
040: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
041: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
042: * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
043: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
044: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
045: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
046: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
047: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
048: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
049: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
050: * SUCH DAMAGE. }}}
051: */
052:
053: import java.util.Map;
054: import java.util.HashMap;
055: import java.text.MessageFormat;
056: import com.protomatter.util.*;
057:
058: /**
059: * A utility class for writing log messages to channels.
060: * Example usage of this class is as follows:<P>
061: *
062: * <UL><TABLE BORDER=1 CELLPADDING=4 CELLSPACING=0 WIDTH="90%">
063: * <TR><TD><PRE>
064: *
065: * // get the channel object
066: * Channel myChannel = Channel.getChannel("MY CHANNEL");
067: *
068: * myChannel.info(this, "Hello there");
069: *
070: * // get the default chanel object
071: * Channel default = Channel.getDefaultChannel();
072: *
073: * default.info(this, "Hello there");
074: *
075: *
076: * // You can also write to multiple channels
077: * String channels[] = new String[] { "first-channel", "second-channel" };
078: * Channel lotsaChannels = Channel.getChannel(channels);
079: *
080: * lotsChannels.info(this, "Hello there");
081: *
082: *
083: * // And finally, you can delegate the list of channels to
084: * // an object that implements the SyslogChannelAware interface
085: * SyslogChannelAware channelAware = new MyChannelAwareObject();
086: * Channel delegatedChannel = Channel.getChannel(channelAware);
087: *
088: * delegatedChannel.info(this, "Hello there");
089: *
090: * </PRE></TD></TR></TABLE></UL><P>
091: *
092: * Basically, rather than using the <TT>infoToChannel(...)</TT>
093: * method and others on <TT>Syslog</TT>, you can simply get a
094: * handle to a channel and call methods on it. Some people
095: * prefer this interface to the regular <tt>Syslog</tt> interface.
096: */
097: public final class Channel {
098: /**
099: * A static utility field, which is the "all" channel.
100: */
101: public static final Channel ALL = Channel.getAllChannel();
102:
103: /**
104: * A static utility field, which is the "default" channel.
105: */
106: public static final Channel DEFAULT = Channel.getDefaultChannel();
107:
108: // one or the other of these two is null
109: private String channels[] = null;
110: private SyslogChannelAware channelAware = null;
111:
112: private Channel(String channels[]) {
113: this .channels = channels;
114: }
115:
116: private Channel(SyslogChannelAware channelAware) {
117: this .channelAware = channelAware;
118: }
119:
120: private final Object getChannelForLogging() {
121: return (channels != null) ? channels : channelAware
122: .getSyslogChannel();
123: }
124:
125: /**
126: * Get the list of channels that this <tt>Channel</tt> object writes to.
127: * This method will either return the channel name(s) passed
128: * into the <tt>getChannel()</tt> method that created this
129: * object, or it will ask the <tt>SyslogChannelAware</tt> object
130: * that this <tt>Channel</tt> is associated with what channels
131: * it thinks we should write to.
132: */
133: public String[] getChannelNames() {
134: return Syslog.convertChannelObject(getChannelForLogging());
135: }
136:
137: /**
138: * Get a Channel object for the given channel name.
139: */
140: public static Channel getChannel(String channelName) {
141: return getChannel(new String[] { channelName });
142: }
143:
144: /**
145: * Get a Channel object with the name of the given class.
146: */
147: public static Channel getChannel(Class channelClass) {
148: return getChannel(new String[] { channelClass.getName() });
149: }
150:
151: /**
152: * Get a Channel whose name is the package
153: * that the given class is in. For instace,
154: * if you pass in <tt>com.yourcompany.foo.SomeClass</tt>
155: * the channel name will be <tt>com.yourcompany.foo</tt>.
156: */
157: public static Channel forPackage(Class someClass) {
158: String packageName = someClass.getName();
159: int pos = packageName.lastIndexOf('.');
160: return getChannel(new String[] { packageName.substring(0, pos) });
161: }
162:
163: /**
164: * Get a Channel object for the given set of channels. Each
165: * logging call will send messages to each of these channels.
166: */
167: public static Channel getChannel(String channels[]) {
168: return new Channel(channels);
169: }
170:
171: /**
172: * Get a Channel object which will delegate to the given
173: * channel aware object. Each logging call will call
174: * the <tt>getSyslogChannel()</tt> method on the given
175: * <tt>SyslogChannelAware</tt> object to get the channel
176: * name (or list of channel names) to send messages to.
177: *
178: * @see SyslogChannelAware
179: */
180: public static Channel getChannel(SyslogChannelAware channelAware) {
181: return new Channel(channelAware);
182: }
183:
184: /**
185: * Get a Channel object for the "all" channel.
186: */
187: public static Channel getAllChannel() {
188: return getChannel(Syslog.ALL_CHANNEL);
189: }
190:
191: /**
192: * Get a Channel object for the default channel.
193: */
194: public static Channel getDefaultChannel() {
195: return getChannel(Syslog.DEFAULT_CHANNEL);
196: }
197:
198: /**
199: * Log a debug message.
200: */
201: public void debug(Object caller, Object message) {
202: Syslog.log(Syslog.getLocalHostName(), caller,
203: getChannelForLogging(), message, null, Syslog.DEBUG,
204: Thread.currentThread(), Thread.currentThread()
205: .getName(), System.currentTimeMillis(), 1);
206: }
207:
208: /**
209: * Log a debug message.
210: */
211: public void debug(Object caller, Object message, Object detail) {
212: Syslog.log(Syslog.getLocalHostName(), caller,
213: getChannelForLogging(), message, detail, Syslog.DEBUG,
214: Thread.currentThread(), Thread.currentThread()
215: .getName(), System.currentTimeMillis(), 1);
216: }
217:
218: /**
219: * Log an info message.
220: */
221: public void info(Object caller, Object message) {
222: Syslog.log(Syslog.getLocalHostName(), caller,
223: getChannelForLogging(), message, null, Syslog.INFO,
224: Thread.currentThread(), Thread.currentThread()
225: .getName(), System.currentTimeMillis(), 1);
226: }
227:
228: /**
229: * Log an info message.
230: */
231: public void info(Object caller, Object message, Object detail) {
232: Syslog.log(Syslog.getLocalHostName(), caller,
233: getChannelForLogging(), message, detail, Syslog.INFO,
234: Thread.currentThread(), Thread.currentThread()
235: .getName(), System.currentTimeMillis(), 1);
236: }
237:
238: /**
239: * Log a warning message.
240: */
241: public void warning(Object caller, Object message) {
242: Syslog.log(Syslog.getLocalHostName(), caller,
243: getChannelForLogging(), message, null, Syslog.WARNING,
244: Thread.currentThread(), Thread.currentThread()
245: .getName(), System.currentTimeMillis(), 1);
246: }
247:
248: /**
249: * Log a warning message.
250: */
251: public void warning(Object caller, Object message, Object detail) {
252: Syslog.log(Syslog.getLocalHostName(), caller,
253: getChannelForLogging(), message, detail,
254: Syslog.WARNING, Thread.currentThread(), Thread
255: .currentThread().getName(), System
256: .currentTimeMillis(), 1);
257: }
258:
259: /**
260: * Log an error message.
261: */
262: public void error(Object caller, Object message) {
263: Syslog.log(Syslog.getLocalHostName(), caller,
264: getChannelForLogging(), message, null, Syslog.ERROR,
265: Thread.currentThread(), Thread.currentThread()
266: .getName(), System.currentTimeMillis(), 1);
267: }
268:
269: /**
270: * Log an error message.
271: */
272: public void error(Object caller, Object message, Object detail) {
273: Syslog.log(Syslog.getLocalHostName(), caller,
274: getChannelForLogging(), message, detail, Syslog.ERROR,
275: Thread.currentThread(), Thread.currentThread()
276: .getName(), System.currentTimeMillis(), 1);
277: }
278:
279: /**
280: * Log a fatal message.
281: */
282: public void fatal(Object caller, Object message) {
283: Syslog.log(Syslog.getLocalHostName(), caller,
284: getChannelForLogging(), message, null, Syslog.FATAL,
285: Thread.currentThread(), Thread.currentThread()
286: .getName(), System.currentTimeMillis(), 1);
287: }
288:
289: /**
290: * Log a fatal message.
291: */
292: public void fatal(Object caller, Object message, Object detail) {
293: Syslog.log(Syslog.getLocalHostName(), caller,
294: getChannelForLogging(), message, detail, Syslog.FATAL,
295: Thread.currentThread(), Thread.currentThread()
296: .getName(), System.currentTimeMillis(), 1);
297: }
298:
299: /**
300: * Log a message about the given exception. The
301: * <TT>toString()</TT> method on the exception is used as
302: * the log message. A stack trace from the exception is used
303: * as the message detail, and the message is logged at
304: * the <TT>ERROR</TT> level.
305: */
306: public void log(Object caller, Throwable exception) {
307: Syslog.log(Syslog.getLocalHostName(), caller,
308: getChannelForLogging(), exception.toString(),
309: exception, Syslog.ERROR, Thread.currentThread(), Thread
310: .currentThread().getName(), System
311: .currentTimeMillis(), 1);
312: }
313:
314: /**
315: * Log a message.
316: */
317: public void log(Object caller, Object message, Object detail,
318: int level) {
319: Syslog.log(Syslog.getLocalHostName(), caller,
320: getChannelForLogging(), message, detail, level, Thread
321: .currentThread(), Thread.currentThread()
322: .getName(), System.currentTimeMillis(), 1);
323: }
324:
325: /**
326: * Log a breadcrumb at the debug level. The breadcrumb
327: * includes information from a <tt><a href="../util/StackTraceInfo.html">StackTraceInfo</a></tt>
328: * object.
329: */
330: public void crumb(Object caller) {
331: Syslog
332: .log(
333: Syslog.getLocalHostName(),
334: caller,
335: getChannelForLogging(),
336: MessageFormat
337: .format(
338: Syslog
339: .getResourceString(MessageConstants.CRUMB_MESSAGE),
340: new Object[] { Syslog
341: .whereAmI(1) }), null,
342: Syslog.DEBUG, Thread.currentThread(), Thread
343: .currentThread().getName(), System
344: .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: StackTraceInfo trace = StackTraceUtil.whereAmI(1);
354: Syslog
355: .log(
356: Syslog.getLocalHostName(),
357: trace.className,
358: getChannelForLogging(),
359: MessageFormat
360: .format(
361: Syslog
362: .getResourceString(MessageConstants.CRUMB_MESSAGE),
363: new Object[] { trace }), null,
364: Syslog.DEBUG, Thread.currentThread(), Thread
365: .currentThread().getName(), System
366: .currentTimeMillis(), 1);
367: }
368:
369: }
|