KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.tomcat.util.log.*;
30 import java.io.Writer JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.io.FileWriter JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.OutputStreamWriter JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.StringWriter JavaDoc;
37
38 import java.util.*;
39
40
41 /**
42  * Log destination ( or channel ). This is the base class that will be
43  * extended by log handlers - tomcat uses util.qlog.QueueLogger,
44  * in future we'll use log4j or java.util.logger adapters.
45  *
46  * The base class writes to a (default) writer, and it can
47  * be used for very simple logging.
48  *
49  * @author Anil Vijendran (akv@eng.sun.com)
50  * @author Alex Chaffee (alex@jguru.com)
51  * @author Ignacio J. Ortega (nacho@siapi.es)
52  * @author Costin Manolache
53  */

54 public class LogHandler {
55
56     protected PrintWriter JavaDoc sink = defaultSink;
57     protected int level = Log.INFORMATION;
58
59     
60     /**
61      * Prints log message and stack trace.
62      * This method should be overriden by real logger implementations
63      *
64      * @param prefix optional prefix.
65      * @param message the message to log.
66      * @param t the exception that was thrown.
67      * @param verbosityLevel what type of message is this?
68      * (WARNING/DEBUG/INFO etc)
69      */

70     public void log(String JavaDoc prefix, String JavaDoc msg, Throwable JavaDoc t,
71             int verbosityLevel)
72     {
73     if( sink==null ) return;
74     // default implementation ( in case no real logging is set up )
75
if( verbosityLevel > this.level ) return;
76     
77     if (prefix != null)
78         sink.println(prefix + ": " + msg );
79     else
80         sink.println( msg );
81     
82     if( t!=null )
83         t.printStackTrace( sink );
84     }
85
86     /**
87      * Flush the log.
88      */

89     public void flush() {
90     if( sink!=null)
91         sink.flush();
92     }
93
94
95     /**
96      * Close the log.
97      */

98     public synchronized void close() {
99     this.sink = null;
100     }
101     
102     /**
103      * Set the verbosity level for this logger. This controls how the
104      * logs will be filtered.
105      *
106      * @param level one of the verbosity level codes.
107      */

108     public void setLevel(int level) {
109     this.level = level;
110     }
111     
112     /**
113      * Get the current verbosity level.
114      */

115     public int getLevel() {
116     return this.level;
117     }
118
119
120     // -------------------- Default sink
121

122     protected static PrintWriter JavaDoc defaultSink =
123     new PrintWriter JavaDoc( new OutputStreamWriter JavaDoc(System.err), true);
124
125     /**
126      * Set the default output stream that is used by all logging
127      * channels.
128      *
129      * @param w the default output stream.
130      */

131     public static void setDefaultSink(Writer JavaDoc w) {
132     if( w instanceof PrintWriter JavaDoc )
133         defaultSink=(PrintWriter JavaDoc)w;
134     else
135         defaultSink = new PrintWriter JavaDoc(w);
136     }
137
138     // -------------------- General purpose utilitiy --------------------
139

140     
141
142 }
143
Popular Tags