Home » apache-tomcat-6.0.26-src » org.apache » tomcat » util » log » [javadoc | source]

    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 java.io;
   43   import java.lang.reflect;
   44   import java.util;
   45   
   46   
   47   /**
   48    * This is the main class seen by objects that need to log. 
   49    * 
   50    * It has a log channel to write to; if it can't find a log with that name,
   51    * it outputs to the default
   52    * sink.  Also prepends a descriptive name to each message
   53    * (usually the toString() of the calling object), so it's easier
   54    * to identify the source.<p>
   55    *
   56    * Intended for use by client classes to make it easy to do
   57    * reliable, consistent logging behavior, even if you don't
   58    * necessarily have a context, or if you haven't registered any
   59    * log files yet, or if you're in a non-Tomcat application.
   60    * <p>
   61    * Usage: <pre>
   62    * class Foo {
   63    *   Log log = Log.getLog("tc_log", "Foo"); // or...
   64    *   Log log = Log.getLog("tc_log", this); // fills in "Foo" for you
   65    *   ...
   66    *     log.log("Something happened");
   67    *     ...
   68    *     log.log("Starting something", Log.DEBUG);
   69    *     ...
   70    *     catch (IOException e) {
   71    *       log.log("While doing something", e);
   72    *     }
   73    * </pre>
   74    * 
   75    *  As a special feature ( required in tomcat operation ) the
   76    *  Log can be modified at run-time, by changing and configuring the logging
   77    *  implementation ( without requiring any special change in the user code ).
   78    *  That means that the  user can do a Log.getLog() at any time,
   79    *  even before the logging system is fully configured. The
   80    *  embeding application can then set up or change the logging details,
   81    *  without any action from the log user.
   82    *
   83    * @deprecated Commons-logging should be used instead.
   84    * @author Alex Chaffee [alex@jguru.com]
   85    * @author Costin Manolache
   86    **/
   87   public class Log implements com.sun.org.apache.commons.logging.Log {
   88   
   89       /**
   90        * Verbosity level codes.
   91        */
   92       public static final int FATAL = Integer.MIN_VALUE;
   93       public static final int ERROR = 1;
   94       public static final int WARNING = 2;
   95       public static final int INFORMATION = 3;
   96       public static final int DEBUG = 4;
   97       
   98   
   99       // name of the logger ( each logger has a unique name,
  100       // used as a key internally )
  101       protected final String logname;
  102   
  103       // string displayed at the beginning of each log line,
  104       // to identify the source
  105       protected final String prefix;
  106   
  107       /* The "real" logger. This allows the manager to change the
  108          sink/logger at runtime, and without requiring the log
  109          user to do any special action or be aware of the changes
  110       */
  111       private LogHandler proxy; // the default
  112   
  113       // Used to get access to other logging channels.
  114       // Can be replaced with an application-specific impl.
  115       private static LogManager logManager=new LogManager();
  116   
  117   
  118       // -------------------- Various constructors --------------------
  119   
  120       protected Log(String channel, String prefix, LogHandler proxy, Object owner) {
  121   	this.logname=channel;
  122   	this.prefix=prefix;
  123   	this.proxy=proxy;
  124       }
  125   
  126       /**
  127        * @param logname name of log to use
  128        * @param owner object whose class name to use as prefix
  129        **/
  130       public static Log getLog( String channel, String prefix ) {
  131   	return logManager.getLog( channel, prefix, null );
  132       }
  133       
  134       /**
  135        * @param logname name of log to use
  136        * @param prefix string to prepend to each message
  137        **/
  138       public static Log getLog( String channel, Object owner ) {
  139   	return logManager.getLog( channel, null, owner );
  140       }
  141       
  142       // -------------------- Log messages. --------------------
  143       
  144       /**
  145        * Logs the message with level INFORMATION
  146        **/
  147       public void log(String msg) 
  148       {
  149   	log(msg, null, INFORMATION);
  150       }
  151       
  152       /**
  153        * Logs the Throwable with level ERROR (assumes an exception is
  154        * trouble; if it's not, use log(msg, t, level))
  155        **/
  156       public void log(String msg, Throwable t) 
  157       {
  158   	log(msg, t, ERROR);
  159       }
  160       
  161       /**
  162        * Logs the message with given level
  163        **/
  164       public void log(String msg, int level) 
  165       {
  166   	log(msg, null, level);
  167       }
  168       
  169       /**
  170        * Logs the message and Throwable to its logger or, if logger
  171        * not found, to the default logger, which writes to the
  172        * default sink, which is usually System.err
  173        **/
  174       public void log(String msg, Throwable t, int level)
  175       {
  176   	log( prefix, msg, t, level );
  177       }
  178   
  179       /** 
  180        */
  181       public void log( String prefix, String msg, Throwable t, int level ) {
  182   	proxy.log( prefix, msg, t, level );
  183       }
  184       
  185       /** Flush any buffers.
  186        *  Override if needed.
  187        */
  188       public void flush() {
  189   	proxy.flush();
  190       }
  191   
  192       public void close() {
  193   	proxy.close();
  194       }
  195   
  196       /** The configured logging level for this channel
  197        */
  198       public int getLevel() {
  199   	return proxy.getLevel();
  200       }
  201   
  202       // -------------------- Management --------------------
  203   
  204       // No getter for the log manager ( user code shouldn't be
  205       // able to control the logger )
  206       
  207       /** Used by the embeding application ( tomcat ) to manage
  208        *  the logging.
  209        *
  210        *  Initially, the Log is not managed, and the default
  211        *  manager is used. The application can find what loggers
  212        *  have been created from the default LogManager, and 
  213        *  provide a special manager implemetation.
  214        */
  215       public static synchronized LogManager setLogManager( LogManager lm ) {
  216   	// can be changed only once - so that user
  217   	// code can't change the log manager in running servers
  218   	if( logManager.getClass() == LogManager.class ) {
  219   	    LogManager oldLM=logManager;
  220   	    logManager=lm;
  221   	    return oldLM;
  222   	}
  223   	return null;
  224       }
  225   
  226       public String  getChannel( LogManager lm ) {
  227   	if( lm != logManager ) return null;
  228   	return logname;
  229       }
  230   
  231       public synchronized void setProxy( LogManager lm, LogHandler l ) {
  232   	// only the manager can change the proxy
  233   	if( lm!= logManager ) {
  234   	    log("Attempt to change proxy " + lm + " " + logManager);
  235   	    return;
  236   	}
  237   	proxy=l;
  238       }
  239   
  240   
  241       // -------------------- Common-logging impl --------------------
  242   
  243       
  244       // -------------------- Log implementation -------------------- 
  245   
  246       public void debug(Object message) {
  247           log(message.toString(), null, DEBUG);
  248       }
  249   
  250       public void debug(Object message, Throwable exception) {
  251           log(message.toString(), exception, DEBUG);
  252       }
  253   
  254       public void error(Object message) {
  255           log(message.toString(), null, ERROR);
  256       }
  257   
  258       public void error(Object message, Throwable exception) {
  259           log(message.toString(), exception, ERROR);
  260       }
  261   
  262       public void fatal(Object message) {
  263           log(message.toString(), null, FATAL);
  264       }
  265   
  266       public void fatal(Object message, Throwable exception) {
  267           log(message.toString(), exception, FATAL);
  268       }
  269   
  270       public void info(Object message) {
  271           log(message.toString(), null, INFORMATION);
  272       }
  273   
  274       public void info(Object message, Throwable exception) {
  275           log(message.toString(), exception, INFORMATION);
  276       }
  277       public void trace(Object message) {
  278           log(message.toString(), null, DEBUG);
  279       }
  280       public void trace(Object message, Throwable exception) {
  281           log(message.toString(), exception, DEBUG);
  282       }
  283       public void warn(Object message) {
  284           log(message.toString(), null, WARNING);
  285       }
  286       public void warn(Object message, Throwable exception) {
  287           log(message.toString(), exception, WARNING);
  288       }
  289   
  290       public boolean isDebugEnabled() {
  291           return proxy.getLevel() <= DEBUG;
  292       }
  293       public boolean isErrorEnabled() {
  294           return proxy.getLevel() <= ERROR;
  295       }
  296       public boolean isFatalEnabled() {
  297           return proxy.getLevel() <= FATAL;
  298       }
  299       public boolean isInfoEnabled() {
  300           return proxy.getLevel() <= INFORMATION;
  301       }
  302       public boolean isTraceEnabled() {
  303           return proxy.getLevel() <= DEBUG;
  304       }
  305       public boolean isWarnEnabled() {
  306           return proxy.getLevel() <= WARNING;
  307       }
  308       
  309       /** Security notes:
  310   
  311       Log acts as a facade to an actual logger ( which has setters, etc).
  312       
  313       The "manager" ( embeding application ) can set the handler, and
  314       edit/modify all the properties at run time.
  315   
  316       Applications can log if they get a Log instance. From Log there is
  317       no way to get an instance of the LogHandler or LogManager.
  318   
  319       LogManager controls access to the Log channels - a 1.2 implementation
  320       would check for LogPermissions ( or other mechanisms - like
  321       information about the current thread, etc ) to determine if the
  322       code can get a Log.
  323       
  324       The "managing application" ( tomcat for example ) can control
  325       any aspect of the logging and change the actual logging
  326       implementation behind the scenes.
  327   
  328       One typical usage is that various components will use Log.getLog()
  329       at various moments. The "manager" ( tomcat ) will initialize the
  330       logging system by setting a LogManager implementation ( which
  331       can't be changed by user code after that ). It can then set the
  332       actual implementation of the logger at any time. ( or provide
  333       access to trusted code to it ).
  334   
  335       Please review.
  336       */
  337       
  338       
  339   }    

Home » apache-tomcat-6.0.26-src » org.apache » tomcat » util » log » [javadoc | source]