Home » apache-tomcat-6.0.26-src » org.apache » juli » logging » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    * 
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   
   18   package org.apache.juli.logging;
   19   
   20   import java.util.logging.ConsoleHandler;
   21   import java.util.logging.Formatter;
   22   import java.util.logging.Handler;
   23   import java.util.logging.Level;
   24   import java.util.logging.Logger;
   25   
   26   
   27   /** 
   28    * Hardcoded java.util.logging commons-logging implementation.
   29    * 
   30    * In addition, it curr 
   31    * 
   32    */
   33   class DirectJDKLog implements Log {
   34       // no reason to hide this - but good reasons to not hide
   35       public Logger logger;
   36       
   37       /** Alternate config reader and console format 
   38        */
   39       private static final String SIMPLE_FMT="org.apache.tomcat.util.log.JdkLoggerFormatter";
   40       private static final String SIMPLE_CFG="org.apache.tomcat.util.log.JdkLoggerConfig";
   41   
   42       static {
   43           if( System.getProperty("java.util.logging.config.class") ==null  &&
   44                   System.getProperty("java.util.logging.config.file") ==null ) {
   45               // default configuration - it sucks. Let's override at least the 
   46               // formatter for the console
   47               try {
   48                   Class.forName(SIMPLE_CFG).newInstance();                
   49               } catch( Throwable t ) {                
   50               }
   51               try {
   52                   Formatter fmt=(Formatter)Class.forName(SIMPLE_FMT).newInstance();
   53                   // it is also possible that the user modifed jre/lib/logging.properties - 
   54                   // but that's really stupid in most cases
   55                   Logger root=Logger.getLogger("");
   56                   Handler handlers[]=root.getHandlers();
   57                   for( int i=0; i< handlers.length; i++ ) {
   58                       // I only care about console - that's what's used in default config anyway
   59                       if( handlers[i] instanceof  ConsoleHandler ) {
   60                           handlers[i].setFormatter(fmt);
   61                       }
   62                   }
   63               } catch( Throwable t ) {
   64                   // maybe it wasn't included - the ugly default will be used.
   65               }
   66               
   67           }
   68       }
   69       
   70       public DirectJDKLog(String name ) {
   71           logger=Logger.getLogger(name);        
   72       }
   73       
   74       public final boolean isErrorEnabled() {
   75           return logger.isLoggable(Level.SEVERE);
   76       }
   77       
   78       public final boolean isWarnEnabled() {
   79           return logger.isLoggable(Level.WARNING); 
   80       }
   81       
   82       public final boolean isInfoEnabled() {
   83           return logger.isLoggable(Level.INFO);
   84       }
   85       
   86       public final boolean isDebugEnabled() {
   87           return logger.isLoggable(Level.FINE);
   88       }
   89       
   90       public final boolean isFatalEnabled() {
   91           return logger.isLoggable(Level.SEVERE);
   92       }
   93       
   94       public final boolean isTraceEnabled() {
   95           return logger.isLoggable(Level.FINER);
   96       }
   97       
   98       public final void debug(Object message) {
   99           log(Level.FINE, String.valueOf(message), null);
  100       }
  101       
  102       public final void debug(Object message, Throwable t) {
  103           log(Level.FINE, String.valueOf(message), t);
  104       }
  105       
  106       public final void trace(Object message) {
  107           log(Level.FINER, String.valueOf(message), null);
  108       }
  109       
  110       public final void trace(Object message, Throwable t) {
  111           log(Level.FINER, String.valueOf(message), t);
  112       }
  113       
  114       public final void info(Object message) {
  115           log(Level.INFO, String.valueOf(message), null);
  116       }
  117       
  118       public final void info(Object message, Throwable t) {        
  119           log(Level.INFO, String.valueOf(message), t);
  120       }
  121       
  122       public final void warn(Object message) {
  123           log(Level.WARNING, String.valueOf(message), null);
  124       }
  125       
  126       public final void warn(Object message, Throwable t) {
  127           log(Level.WARNING, String.valueOf(message), t);
  128       }
  129       
  130       public final void error(Object message) {
  131           log(Level.SEVERE, String.valueOf(message), null);
  132       }
  133       
  134       public final void error(Object message, Throwable t) {
  135           log(Level.SEVERE, String.valueOf(message), t);
  136       }
  137       
  138       public final void fatal(Object message) {
  139           log(Level.SEVERE, String.valueOf(message), null);
  140       }
  141       
  142       public final void fatal(Object message, Throwable t) {
  143           log(Level.SEVERE, String.valueOf(message), t);
  144       }    
  145   
  146       // from commons logging. This would be my number one reason why java.util.logging
  147       // is bad - design by comitee can be really bad ! The impact on performance of 
  148       // using java.util.logging - and the ugliness if you need to wrap it - is far
  149       // worse than the unfriendly and uncommon default format for logs. 
  150       
  151       private void log( Level level, String msg, Throwable ex ) {
  152           if (logger.isLoggable(level)) {
  153               // Hack (?) to get the stack trace.
  154               Throwable dummyException=new Throwable();
  155               StackTraceElement locations[]=dummyException.getStackTrace();
  156               // Caller will be the third element
  157               String cname="unknown";
  158               String method="unknown";
  159               if( locations!=null && locations.length >2 ) {
  160                   StackTraceElement caller=locations[2];
  161                   cname=caller.getClassName();
  162                   method=caller.getMethodName();
  163               }
  164               if( ex==null ) {
  165                   logger.logp( level, cname, method, msg );
  166               } else {
  167                   logger.logp( level, cname, method, msg, ex );
  168               }
  169           }
  170       }        
  171   
  172       // for LogFactory
  173       static void release() {
  174           
  175       }
  176       
  177       static Log getInstance(String name) {
  178           return new DirectJDKLog( name );
  179       }
  180   }
  181   
  182   

Home » apache-tomcat-6.0.26-src » org.apache » juli » logging » [javadoc | source]