KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > log > Syslog


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

52
53 package com.go.trove.log;
54
55 import java.io.*;
56
57 /******************************************************************************
58  * Contains references to a static Log instance that can be used for general
59  * system-wide logging. By default, log messages are written to System.out
60  * or System.err. When "installed", all output provided to System.out
61  * and System.err is redirected into the system log.
62  *
63  * @author Brian S O'Neill
64  * @version
65  * <!--$$Revision:--> 15 <!-- $-->, <!--$$JustDate:--> 9/07/00 <!-- $-->
66  * @see Log
67  */

68 public class Syslog {
69     private static final Log cLog;
70     private static final LogListener cSystemLogEventPrinter;
71
72     private static LogEventParsingOutputStream cSystemOut;
73     private static LogEventParsingOutputStream cSystemErr;
74
75     private static PrintStream cOriginalOut;
76     private static PrintStream cOriginalErr;
77     private static boolean cInstalled;
78
79     static {
80         cLog = new Log(null, null);
81         cLog.setDescription("System Log");
82
83         cSystemLogEventPrinter = new LogListener() {
84             public void logMessage(LogEvent e) {
85                 PrintStream ps = getPrintStream(e);
86                 String JavaDoc message = e.getMessage();
87                 if (message == null) {
88                     ps.println();
89                 }
90                 else {
91                     ps.println(message);
92                 }
93             }
94             
95             public void logException(LogEvent e) {
96                 Throwable JavaDoc t = e.getException();
97                 if (t == null) {
98                     logMessage(e);
99                 }
100                 else {
101                     t.printStackTrace(getPrintStream(e));
102                 }
103             }
104                 
105             private PrintStream getPrintStream(LogEvent e) {
106                 synchronized (log()) {
107                     switch (e.getType()) {
108                         case LogEvent.DEBUG_TYPE:
109                         case LogEvent.INFO_TYPE:
110                         return (cInstalled) ? cOriginalOut : System.out;
111                     }
112                     
113                     return (cInstalled) ? cOriginalErr : System.err;
114                 }
115             }
116         };
117         
118         log().addLogListener(cSystemLogEventPrinter);
119     }
120
121     /**
122      * Returns the system Log instance that, by default, only has one
123      * LogListener. It prints debug and info LogEvent messages to System.out
124      * and other LogEvent messages to System.err.
125      *
126      * @see #getSystemLogEventPrinter()
127      */

128     public static Log log() {
129         return cLog;
130     }
131
132     /**
133      * Returns a simple LogListener that prints debug and info LogEvent
134      * messages to System.out and other LogEvent messages to System.err.
135      * Remove this listener to disable printing to System.out and System.err.
136      */

137     public static LogListener getSystemLogEventPrinter() {
138         return cSystemLogEventPrinter;
139     }
140
141     /**
142      * When installed, System.out and System.err are redirected to Syslog.log.
143      * System.out produces info events, and System.err produces error events.
144      */

145     public static void install() {
146         synchronized (log()) {
147             if (!cInstalled) {
148                 cInstalled = true;
149                 cOriginalOut = System.out;
150                 cOriginalErr = System.err;
151                 
152                 cSystemOut = new LogEventParsingOutputStream
153                     (log(), LogEvent.INFO_TYPE)
154                 {
155                     public boolean isEnabled() {
156                         return log().isInfoEnabled();
157                     }
158                 };
159                 cSystemOut.addLogListener(log());
160                 System.setOut(new PrintStream(cSystemOut, true));
161
162                 cSystemErr = new LogEventParsingOutputStream
163                     (log(), LogEvent.ERROR_TYPE)
164                 {
165                     public boolean isEnabled() {
166                         return log().isErrorEnabled();
167                     }
168                 };
169                 cSystemErr.addLogListener(log());
170                 System.setErr(new PrintStream(cSystemErr, true));
171             }
172         }
173     }
174
175     /**
176      * Uninstalls by restoring System.out and System.err.
177      */

178     public static void uninstall() {
179         synchronized (log()) {
180             if (cInstalled) {
181                 cInstalled = false;
182                 System.setOut(cOriginalOut);
183                 System.setErr(cOriginalErr);
184                 cOriginalOut = null;
185                 cOriginalErr = null;
186                 cSystemOut = null;
187                 cSystemErr = null;
188             }
189         }
190     }
191
192     /**
193      * Shortcut to {@link Log#debug(String) Syslog.log().debug(String)}.
194      */

195     public static void debug(String JavaDoc s) {
196         log().debug(s);
197     }
198
199     /**
200      * Shortcut to {@link Log#debug(Throwable) Syslog.log().debug(Throwable)}.
201      */

202     public static void debug(Throwable JavaDoc t) {
203         log().debug(t);
204     }
205
206     /**
207      * Shortcut to {@link Log#info(String) Syslog.log().info(String)}.
208      */

209     public static void info(String JavaDoc s) {
210         log().info(s);
211     }
212
213     /**
214      * Shortcut to {@link Log#info(Throwable) Syslog.log().info(Throwable)}.
215      */

216     public static void info(Throwable JavaDoc t) {
217         log().info(t);
218     }
219
220     /**
221      * Shortcut to {@link Log#warn(String) Syslog.log().warn(String)}.
222      */

223     public static void warn(String JavaDoc s) {
224         log().warn(s);
225     }
226
227     /**
228      * Shortcut to {@link Log#warn(Throwable) Syslog.log().warn(Throwable)}.
229      */

230     public static void warn(Throwable JavaDoc t) {
231         log().warn(t);
232     }
233
234     /**
235      * Shortcut to {@link Log#error(String) Syslog.log().error(String)}.
236      */

237     public static void error(String JavaDoc s) {
238         log().error(s);
239     }
240
241     /**
242      * Shortcut to {@link Log#error(Throwable) Syslog.log().error(Throwable)}.
243      */

244     public static void error(Throwable JavaDoc t) {
245         log().error(t);
246     }
247
248     private Syslog() {
249     }
250 }
251
Popular Tags