KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > log > Log


1
2
3 /*
4  * The contents of this file are subject to the terms
5  * of the Common Development and Distribution License
6  * (the "License"). You may not use this file except
7  * in compliance with the License.
8  *
9  * You can obtain a copy of the license at
10  * glassfish/bootstrap/legal/CDDLv1.0.txt or
11  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
12  * See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL
16  * HEADER in each file and include the License file at
17  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
18  * add the following below this CDDL HEADER, with the
19  * fields enclosed by brackets "[]" replaced with your
20  * own identifying information: Portions Copyright [yyyy]
21  * [name of copyright owner]
22  *
23  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24  *
25  * Portions Copyright Apache Software Foundation.
26  */

27 package org.apache.tomcat.util.log;
28
29 import java.io.*;
30 import java.lang.reflect.*;
31 import java.util.*;
32
33
34 /**
35  * This is the main class seen by objects that need to log.
36  *
37  * It has a log channel to write to; if it can't find a log with that name,
38  * it outputs to the default
39  * sink. Also prepends a descriptive name to each message
40  * (usually the toString() of the calling object), so it's easier
41  * to identify the source.<p>
42  *
43  * Intended for use by client classes to make it easy to do
44  * reliable, consistent logging behavior, even if you don't
45  * necessarily have a context, or if you haven't registered any
46  * log files yet, or if you're in a non-Tomcat application.
47  * <p>
48  * Usage: <pre>
49  * class Foo {
50  * Log log = Log.getLog("tc_log", "Foo"); // or...
51  * Log log = Log.getLog("tc_log", this); // fills in "Foo" for you
52  * ...
53  * log.log("Something happened");
54  * ...
55  * log.log("Starting something", Log.DEBUG);
56  * ...
57  * catch (IOException e) {
58  * log.log("While doing something", e);
59  * }
60  * </pre>
61  *
62  * As a special feature ( required in tomcat operation ) the
63  * Log can be modified at run-time, by changing and configuring the logging
64  * implementation ( without requiring any special change in the user code ).
65  * That means that the user can do a Log.getLog() at any time,
66  * even before the logging system is fully configured. The
67  * embeding application can then set up or change the logging details,
68  * without any action from the log user.
69  *
70  * @deprecated Commons-logging should be used instead.
71  * @author Alex Chaffee [alex@jguru.com]
72  * @author Costin Manolache
73  **/

74 public class Log implements com.sun.org.apache.commons.logging.Log {
75
76     /**
77      * Verbosity level codes.
78      */

79     public static final int FATAL = Integer.MIN_VALUE;
80     public static final int ERROR = 1;
81     public static final int WARNING = 2;
82     public static final int INFORMATION = 3;
83     public static final int DEBUG = 4;
84     
85
86     // name of the logger ( each logger has a unique name,
87
// used as a key internally )
88
protected String JavaDoc logname;
89
90     // string displayed at the beginning of each log line,
91
// to identify the source
92
protected String JavaDoc prefix;
93
94     /* The "real" logger. This allows the manager to change the
95        sink/logger at runtime, and without requiring the log
96        user to do any special action or be aware of the changes
97     */

98     private LogHandler proxy; // the default
99

100     // Used to get access to other logging channels.
101
// Can be replaced with an application-specific impl.
102
private static LogManager logManager=new LogManager();
103
104
105     // -------------------- Various constructors --------------------
106

107     protected Log(String JavaDoc channel, String JavaDoc prefix, LogHandler proxy, Object JavaDoc owner) {
108     this.logname=channel;
109     this.prefix=prefix;
110     this.proxy=proxy;
111     }
112
113     /**
114      * @param logname name of log to use
115      * @param owner object whose class name to use as prefix
116      **/

117     public static Log getLog( String JavaDoc channel, String JavaDoc prefix ) {
118     return logManager.getLog( channel, prefix, null );
119     }
120     
121     /**
122      * @param logname name of log to use
123      * @param prefix string to prepend to each message
124      **/

125     public static Log getLog( String JavaDoc channel, Object JavaDoc owner ) {
126     return logManager.getLog( channel, null, owner );
127     }
128     
129     // -------------------- Log messages. --------------------
130

131     /**
132      * Logs the message with level INFORMATION
133      **/

134     public void log(String JavaDoc msg)
135     {
136     log(msg, null, INFORMATION);
137     }
138     
139     /**
140      * Logs the Throwable with level ERROR (assumes an exception is
141      * trouble; if it's not, use log(msg, t, level))
142      **/

143     public void log(String JavaDoc msg, Throwable JavaDoc t)
144     {
145     log(msg, t, ERROR);
146     }
147     
148     /**
149      * Logs the message with given level
150      **/

151     public void log(String JavaDoc msg, int level)
152     {
153     log(msg, null, level);
154     }
155     
156     /**
157      * Logs the message and Throwable to its logger or, if logger
158      * not found, to the default logger, which writes to the
159      * default sink, which is usually System.err
160      **/

161     public void log(String JavaDoc msg, Throwable JavaDoc t, int level)
162     {
163     log( prefix, msg, t, level );
164     }
165
166     /**
167      */

168     public void log( String JavaDoc prefix, String JavaDoc msg, Throwable JavaDoc t, int level ) {
169     proxy.log( prefix, msg, t, level );
170     }
171     
172     /** Flush any buffers.
173      * Override if needed.
174      */

175     public void flush() {
176     proxy.flush();
177     }
178
179     public void close() {
180     proxy.close();
181     }
182
183     /** The configured logging level for this channel
184      */

185     public int getLevel() {
186     return proxy.getLevel();
187     }
188
189     // -------------------- Management --------------------
190

191     // No getter for the log manager ( user code shouldn't be
192
// able to control the logger )
193

194     /** Used by the embeding application ( tomcat ) to manage
195      * the logging.
196      *
197      * Initially, the Log is not managed, and the default
198      * manager is used. The application can find what loggers
199      * have been created from the default LogManager, and
200      * provide a special manager implemetation.
201      */

202     public static LogManager setLogManager( LogManager lm ) {
203     // can be changed only once - so that user
204
// code can't change the log manager in running servers
205
if( logManager.getClass() == LogManager.class ) {
206         LogManager oldLM=logManager;
207         logManager=lm;
208         return oldLM;
209     }
210     return null;
211     }
212
213     public String JavaDoc getChannel( LogManager lm ) {
214     if( lm != logManager ) return null;
215     return logname;
216     }
217
218     public void setProxy( LogManager lm, LogHandler l ) {
219     // only the manager can change the proxy
220
if( lm!= logManager ) {
221         log("Attempt to change proxy " + lm + " " + logManager);
222         return;
223     }
224     proxy=l;
225     }
226
227
228     // -------------------- Common-logging impl --------------------
229

230     
231     // -------------------- Log implementation --------------------
232

233     public void debug(Object JavaDoc message) {
234         log(message.toString(), null, DEBUG);
235     }
236
237     public void debug(Object JavaDoc message, Throwable JavaDoc exception) {
238         log(message.toString(), exception, DEBUG);
239     }
240
241     public void error(Object JavaDoc message) {
242         log(message.toString(), null, ERROR);
243     }
244
245     public void error(Object JavaDoc message, Throwable JavaDoc exception) {
246         log(message.toString(), exception, ERROR);
247     }
248
249     public void fatal(Object JavaDoc message) {
250         log(message.toString(), null, FATAL);
251     }
252
253     public void fatal(Object JavaDoc message, Throwable JavaDoc exception) {
254         log(message.toString(), exception, FATAL);
255     }
256
257     public void info(Object JavaDoc message) {
258         log(message.toString(), null, INFORMATION);
259     }
260
261     public void info(Object JavaDoc message, Throwable JavaDoc exception) {
262         log(message.toString(), exception, INFORMATION);
263     }
264     public void trace(Object JavaDoc message) {
265         log(message.toString(), null, DEBUG);
266     }
267     public void trace(Object JavaDoc message, Throwable JavaDoc exception) {
268         log(message.toString(), exception, DEBUG);
269     }
270     public void warn(Object JavaDoc message) {
271         log(message.toString(), null, WARNING);
272     }
273     public void warn(Object JavaDoc message, Throwable JavaDoc exception) {
274         log(message.toString(), exception, WARNING);
275     }
276
277     public boolean isDebugEnabled() {
278         return proxy.getLevel() <= DEBUG;
279     }
280     public boolean isErrorEnabled() {
281         return proxy.getLevel() <= ERROR;
282     }
283     public boolean isFatalEnabled() {
284         return proxy.getLevel() <= FATAL;
285     }
286     public boolean isInfoEnabled() {
287         return proxy.getLevel() <= INFORMATION;
288     }
289     public boolean isTraceEnabled() {
290         return proxy.getLevel() <= DEBUG;
291     }
292     public boolean isWarnEnabled() {
293         return proxy.getLevel() <= WARNING;
294     }
295     
296     /** Security notes:
297
298     Log acts as a facade to an actual logger ( which has setters, etc).
299     
300     The "manager" ( embeding application ) can set the handler, and
301     edit/modify all the properties at run time.
302
303     Applications can log if they get a Log instance. From Log there is
304     no way to get an instance of the LogHandler or LogManager.
305
306     LogManager controls access to the Log channels - a 1.2 implementation
307     would check for LogPermissions ( or other mechanisms - like
308     information about the current thread, etc ) to determine if the
309     code can get a Log.
310     
311     The "managing application" ( tomcat for example ) can control
312     any aspect of the logging and change the actual logging
313     implementation behind the scenes.
314
315     One typical usage is that various components will use Log.getLog()
316     at various moments. The "manager" ( tomcat ) will initialize the
317     logging system by setting a LogManager implementation ( which
318     can't be changed by user code after that ). It can then set the
319     actual implementation of the logger at any time. ( or provide
320     access to trusted code to it ).
321
322     Please review.
323     */

324     
325     
326 }
327
Popular Tags