Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License. You can obtain
  * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
  * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
  * Sun designates this particular file as subject to the "Classpath" exception
  * as provided by Sun in the GPL Version 2 section of the License file that
  * accompanied this code.  If applicable, add the following below the License
  * Header, with the fields enclosed by brackets [] replaced by your own
  * identifying information: "Portions Copyrighted [year]
  * [name of copyright owner]"
  *
  * Contributor(s):
  *
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  *
  *
  * This file incorporates work covered by the following copyright and
  * permission notice:
  *
  * Copyright 2004 The Apache Software Foundation
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 
 package org.apache.tomcat.util.log;
 
 import java.io.*;
 import java.util.*;


This is the main class seen by objects that need to log. It has a log channel to write to; if it can't find a log with that name, it outputs to the default sink. Also prepends a descriptive name to each message (usually the toString() of the calling object), so it's easier to identify the source.

Intended for use by client classes to make it easy to do reliable, consistent logging behavior, even if you don't necessarily have a context, or if you haven't registered any log files yet, or if you're in a non-Tomcat application.

Usage:

 class Foo {
   Log log = Log.getLog("tc_log", "Foo"); // or...
   Log log = Log.getLog("tc_log", this); // fills in "Foo" for you
   ...
     log.log("Something happened");
     ...
     log.log("Starting something", Log.DEBUG);
     ...
     catch (IOException e) {
       log.log("While doing something", e);
     }
 
As a special feature ( required in tomcat operation ) the Log can be modified at run-time, by changing and configuring the logging implementation ( without requiring any special change in the user code ). That means that the user can do a Log.getLog() at any time, even before the logging system is fully configured. The embeding application can then set up or change the logging details, without any action from the log user.

Deprecated:
Commons-logging should be used instead.
Author(s):
Alex Chaffee [alex@jguru.com]
Costin Manolache
public class Log implements com.sun.org.apache.commons.logging.Log {

    
Verbosity level codes.
    public static final int FATAL = .;
    public static final int ERROR = 1;
    public static final int WARNING = 2;
    public static final int INFORMATION = 3;
    public static final int DEBUG = 4;
    
    // name of the logger ( each logger has a unique name,
    // used as a key internally )
    protected final String logname;
    // string displayed at the beginning of each log line,
    // to identify the source
    protected final String prefix;
    /* The "real" logger. This allows the manager to change the
       sink/logger at runtime, and without requiring the log
       user to do any special action or be aware of the changes
    */
    private LogHandler proxy// the default
    // Used to get access to other logging channels.
    // Can be replaced with an application-specific impl.
    private static LogManager logManager=new LogManager();
    // -------------------- Various constructors --------------------
    protected Log(String channelString prefixLogHandler proxyObject owner) {
	this.=channel;
	this.=prefix;
	this.=proxy;
    }

    

Parameters:
logname name of log to use
owner object whose class name to use as prefix
    public static Log getLogString channelString prefix ) {
	return .getLogchannelprefixnull );
    }
    
    

Parameters:
logname name of log to use
prefix string to prepend to each message
    public static Log getLogString channelObject owner ) {
	return .getLogchannelnullowner );
    }
    
    // -------------------- Log messages. --------------------
    
    
Logs the message with level INFORMATION
    public void log(String msg
    {
	log(msgnull);
    }
    
    
Logs the Throwable with level ERROR (assumes an exception is trouble; if it's not, use log(msg, t, level))
    public void log(String msgThrowable t
    {
	log(msgt);
    }
    
    
Logs the message with given level
    public void log(String msgint level
    {
	log(msgnulllevel);
    }
    
    
Logs the message and Throwable to its logger or, if logger not found, to the default logger, which writes to the default sink, which is usually System.err
    public void log(String msgThrowable tint level)
    {
	logmsgtlevel );
    }

    
    public void logString prefixString msgThrowable tint level ) {
	.logprefixmsgtlevel );
    }
    
    
Flush any buffers. Override if needed.
    public void flush() {
    }
    public void close() {
    }

    
The configured logging level for this channel
    public int getLevel() {
	return .getLevel();
    }
    // -------------------- Management --------------------
    // No getter for the log manager ( user code shouldn't be
    // able to control the logger )
    
    
Used by the embeding application ( tomcat ) to manage the logging. Initially, the Log is not managed, and the default manager is used. The application can find what loggers have been created from the default LogManager, and provide a special manager implemetation.
    public static synchronized LogManager setLogManagerLogManager lm ) {
	// can be changed only once - so that user
	// code can't change the log manager in running servers
	if.getClass() == LogManager.class ) {
	    LogManager oldLM=;
	    =lm;
	    return oldLM;
	}
	return null;
    }
    public String  getChannelLogManager lm ) {
	iflm !=  ) return null;
	return ;
    }
    public synchronized void setProxyLogManager lmLogHandler l ) {
	// only the manager can change the proxy
	iflm!=  ) {
	    log("Attempt to change proxy " + lm + " " + );
	    return;
	}
    }
    // -------------------- Common-logging impl --------------------
    
    // -------------------- Log implementation -------------------- 
    public void debug(Object message) {
        log(message.toString(), null);
    }
    public void debug(Object messageThrowable exception) {
        log(message.toString(), exception);
    }
    public void error(Object message) {
        log(message.toString(), null);
    }
    public void error(Object messageThrowable exception) {
        log(message.toString(), exception);
    }
    public void fatal(Object message) {
        log(message.toString(), null);
    }
    public void fatal(Object messageThrowable exception) {
        log(message.toString(), exception);
    }
    public void info(Object message) {
        log(message.toString(), null);
    }
    public void info(Object messageThrowable exception) {
        log(message.toString(), exception);
    }
    public void trace(Object message) {
        log(message.toString(), null);
    }
    public void trace(Object messageThrowable exception) {
        log(message.toString(), exception);
    }
    public void warn(Object message) {
        log(message.toString(), null);
    }
    public void warn(Object messageThrowable exception) {
        log(message.toString(), exception);
    }
    public boolean isDebugEnabled() {
        return .getLevel() <= ;
    }
    public boolean isErrorEnabled() {
        return .getLevel() <= ;
    }
    public boolean isFatalEnabled() {
        return .getLevel() <= ;
    }
    public boolean isInfoEnabled() {
        return .getLevel() <= ;
    }
    public boolean isTraceEnabled() {
        return .getLevel() <= ;
    }
    public boolean isWarnEnabled() {
        return .getLevel() <= ;
    }
    
    
Security notes: Log acts as a facade to an actual logger ( which has setters, etc). The "manager" ( embeding application ) can set the handler, and edit/modify all the properties at run time. Applications can log if they get a Log instance. From Log there is no way to get an instance of the LogHandler or LogManager. LogManager controls access to the Log channels - a 1.2 implementation would check for LogPermissions ( or other mechanisms - like information about the current thread, etc ) to determine if the code can get a Log. The "managing application" ( tomcat for example ) can control any aspect of the logging and change the actual logging implementation behind the scenes. One typical usage is that various components will use Log.getLog() at various moments. The "manager" ( tomcat ) will initialize the logging system by setting a LogManager implementation ( which can't be changed by user code after that ). It can then set the actual implementation of the logger at any time. ( or provide access to trusted code to it ). Please review.
    
    
}    
New to GrepCode? Check out our FAQ X