1 2 3 /* 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 5 * 6 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. 7 * 8 * Portions Copyright Apache Software Foundation. 9 * 10 * The contents of this file are subject to the terms of either the GNU 11 * General Public License Version 2 only ("GPL") or the Common Development 12 * and Distribution License("CDDL") (collectively, the "License"). You 13 * may not use this file except in compliance with the License. You can obtain 14 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html 15 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific 16 * language governing permissions and limitations under the License. 17 * 18 * When distributing the software, include this License Header Notice in each 19 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. 20 * Sun designates this particular file as subject to the "Classpath" exception 21 * as provided by Sun in the GPL Version 2 section of the License file that 22 * accompanied this code. If applicable, add the following below the License 23 * Header, with the fields enclosed by brackets [] replaced by your own 24 * identifying information: "Portions Copyrighted [year] 25 * [name of copyright owner]" 26 * 27 * Contributor(s): 28 * 29 * If you wish your version of this file to be governed by only the CDDL or 30 * only the GPL Version 2, indicate your decision by adding "[Contributor] 31 * elects to include this software in this distribution under the [CDDL or GPL 32 * Version 2] license." If you don't indicate a single choice of license, a 33 * recipient has the option to distribute your version of this file under 34 * either the CDDL, the GPL Version 2 or to extend the choice of license to 35 * its licensees as provided above. However, if you add GPL Version 2 code 36 * and therefore, elected the GPL Version 2 license, then the option applies 37 * only if the new code is made subject to such option by the copyright 38 * holder. 39 */ 40 package org.apache.tomcat.util.log; 41 42 import org.apache.tomcat.util.log; 43 import java.io.Writer; 44 import java.io.PrintWriter; 45 import java.io.FileWriter; 46 import java.io.File; 47 import java.io.OutputStreamWriter; 48 import java.io.IOException; 49 import java.io.StringWriter; 50 51 import java.util; 52 53 54 /** 55 * Log destination ( or channel ). This is the base class that will be 56 * extended by log handlers - tomcat uses util.qlog.QueueLogger, 57 * in future we'll use log4j or java.util.logger adapters. 58 * 59 * The base class writes to a (default) writer, and it can 60 * be used for very simple logging. 61 * 62 * @author Anil Vijendran (akv@eng.sun.com) 63 * @author Alex Chaffee (alex@jguru.com) 64 * @author Ignacio J. Ortega (nacho@siapi.es) 65 * @author Costin Manolache 66 */ 67 public class LogHandler { 68 69 protected PrintWriter sink = defaultSink; 70 protected int level = Log.INFORMATION; 71 72 73 /** 74 * Prints log message and stack trace. 75 * This method should be overriden by real logger implementations 76 * 77 * @param prefix optional prefix. 78 * @param message the message to log. 79 * @param t the exception that was thrown. 80 * @param verbosityLevel what type of message is this? 81 * (WARNING/DEBUG/INFO etc) 82 */ 83 public void log(String prefix, String msg, Throwable t, 84 int verbosityLevel) 85 { 86 if( sink==null ) return; 87 // default implementation ( in case no real logging is set up ) 88 if( verbosityLevel > this.level ) return; 89 90 if (prefix != null) 91 sink.println(prefix + ": " + msg ); 92 else 93 sink.println( msg ); 94 95 if( t!=null ) 96 t.printStackTrace( sink ); 97 } 98 99 /** 100 * Flush the log. 101 */ 102 public void flush() { 103 if( sink!=null) 104 sink.flush(); 105 } 106 107 108 /** 109 * Close the log. 110 */ 111 public synchronized void close() { 112 this.sink = null; 113 } 114 115 /** 116 * Set the verbosity level for this logger. This controls how the 117 * logs will be filtered. 118 * 119 * @param level one of the verbosity level codes. 120 */ 121 public void setLevel(int level) { 122 this.level = level; 123 } 124 125 /** 126 * Get the current verbosity level. 127 */ 128 public int getLevel() { 129 return this.level; 130 } 131 132 133 // -------------------- Default sink 134 135 protected static PrintWriter defaultSink = 136 new PrintWriter( new OutputStreamWriter(System.err), true); 137 138 /** 139 * Set the default output stream that is used by all logging 140 * channels. 141 * 142 * @param w the default output stream. 143 */ 144 public static void setDefaultSink(Writer w) { 145 if( w instanceof PrintWriter ) 146 defaultSink=(PrintWriter)w; 147 else 148 defaultSink = new PrintWriter(w); 149 } 150 151 // -------------------- General purpose utilitiy -------------------- 152 153 154 155 }