You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by jo...@apache.org on 2001/03/12 08:19:54 UTC

cvs commit: jakarta-velocity/src/java/org/apache/velocity/runtime/log AvalonLogSystem.java LogSystem.java LogManager.java

jon         01/03/11 23:19:54

  Modified:    src/java/org/apache/velocity/runtime/log LogManager.java
  Added:       src/java/org/apache/velocity/runtime/log
                        AvalonLogSystem.java LogSystem.java
  Log:
  ok, this is the start of the new logging system which is interface
  based. I took the existing Avalon based one and refactored it into
  the new way of doing things.
  
  note, LogManager still needs to be fixed to be able to instantiate
  the right class with class.forName().
  
  this is just the initial pass in the refactoring. no existing
  funtionality has been lost and things should compile just dandy.
  
  let me know if there are problems
  
  -jon
  
  Revision  Changes    Path
  1.3       +12 -48    jakarta-velocity/src/java/org/apache/velocity/runtime/log/LogManager.java
  
  Index: LogManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/log/LogManager.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- LogManager.java	2001/03/05 11:46:28	1.2
  +++ LogManager.java	2001/03/12 07:19:53	1.3
  @@ -54,64 +54,28 @@
    * <http://www.apache.org/>.
    */
   
  -import java.io.File;
   
  -import java.net.URL;
  -
  -import org.apache.log.Category;
  -import org.apache.log.Formatter;
  -import org.apache.log.Priority;
  -import org.apache.log.Logger;
  -import org.apache.log.LogKit;
  -import org.apache.log.LogTarget;
  -import org.apache.log.output.FileOutputLogTarget;
  -
   /**
    * LogManager.java
  - *
  - * Very rudimentary log manager. Lifted in part from the
  - * SAR deployer in Avalon. This is how the logging system
  - * is supposed to be used. The Velocity runtime uses
  - * this class for logging.
  + * This class is responsible for instantiating the correct LoggingSystem
  + * Right now, it is hard coded with a single Logging System. Eventually
  + * we will have more LoggingSystems.
    *
    * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
  - * @version $Id: LogManager.java,v 1.2 2001/03/05 11:46:28 jvanzyl Exp $
  + * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
  + * @version $Id: LogManager.java,v 1.3 2001/03/12 07:19:53 jon Exp $
    */
   public class LogManager
   {
  -    /*
  -     * This method was removed from Avalon, it was noted that this
  -     * method should be moved to a LogManager of some sort so that's
  -     * what I'm doing. This is the start of a LogManager because there
  -     * doesn't appear to be one in Avalon.
  +    /**
  +     * This class should be fixed to get its log system from
  +     * a properties file.
        */
  -    public static Logger createLogger(String logFile)
  +    public static LogSystem createLogSystem(String logFile)
           throws Exception
       {
  -        String targetName = "velocity";
  -        String priority = "DEBUG";
  -                
  -        Category category = LogKit.createCategory( 
  -            targetName, LogKit.getPriorityForName( priority ) );
  -        
  -        /*
  -         * Just create a FileOutputLogTarget, this is taken
  -         * from the SAR deployer in Avalon.
  -         */
  -        FileOutputLogTarget target = new FileOutputLogTarget();
  -        File logFileLocation = new File (logFile);
  -        
  -        target.setFilename(logFileLocation.getAbsolutePath());
  -        target.setFormatter(new VelocityFormatter());
  -        target.setFormat("%{time} %{message}\\n%{throwable}" );
  -        
  -        LogTarget logTargets[] = null;
  -                
  -        if ( null != target ) 
  -        {
  -            logTargets = new LogTarget[] { target };
  -        }            
  -                
  -        return LogKit.createLogger( category, logTargets );
  +        LogSystem ls = new AvalonLogSystem();
  +        ls.init(logFile);
  +        return ls;
       }
   }
  
  
  
  1.1                  jakarta-velocity/src/java/org/apache/velocity/runtime/log/AvalonLogSystem.java
  
  Index: AvalonLogSystem.java
  ===================================================================
  package org.apache.velocity.runtime.log;
  
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.io.File;
  
  import java.net.URL;
  
  import org.apache.log.Category;
  import org.apache.log.Formatter;
  import org.apache.log.Priority;
  import org.apache.log.Logger;
  import org.apache.log.LogKit;
  import org.apache.log.LogTarget;
  import org.apache.log.output.FileOutputLogTarget;
  
  import org.apache.velocity.util.StringUtils;
  
  /**
   * Implementation of a Avalon logger.
   *
   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
   * @version $Id: AvalonLogSystem.java,v 1.1 2001/03/12 07:19:51 jon Exp $
   */
  public class AvalonLogSystem implements LogSystem
  {
      private Logger logger = null;
      private boolean stackTrace = false;
      
      public void init(String logFile)
          throws Exception
      {
          String targetName = "velocity";
          String priority = "DEBUG";
                  
          Category category = LogKit.createCategory( 
              targetName, LogKit.getPriorityForName( priority ) );
          
          /*
           * Just create a FileOutputLogTarget, this is taken
           * from the SAR deployer in Avalon.
           */
          FileOutputLogTarget target = new FileOutputLogTarget();
          File logFileLocation = new File (logFile);
          
          target.setFilename(logFileLocation.getAbsolutePath());
          target.setFormatter(new VelocityFormatter());
          target.setFormat("%{time} %{message}\\n%{throwable}" );
          
          LogTarget logTargets[] = null;
                  
          if ( null != target ) 
          {
              logTargets = new LogTarget[] { target };
          }            
                  
          logger = LogKit.createLogger( category, logTargets );
      }
      
      public void log (int type, Object message)
      {
  		switch (type)
  		{
  			case LogSystem.WARN_ID:
  						 warn(message);
  						 break;
  			case LogSystem.INFO_ID:
  						 info(message);
  						 break;
  			case LogSystem.DEBUG_ID:
  						 debug(message);
  						 break;
  			case LogSystem.ERROR_ID:
  						 error(message);
  						 break;
  			default:
  				info(message);
  				break;
  		}
      }
      /**
       * Handle logging.
       *
       * @param Object message to log
       */
      public void warn(Object message)
      {
          String out = null;
          
          if ( getStackTrace() &&
              (message instanceof Throwable || message instanceof Exception) )
          {
              out = StringUtils.stackTrace((Throwable)message);
          }
          else
          {
              out = message.toString();    
          }
          logger.warn(WARN + out);
      }
  
      /**
       * Handle logging.
       *
       * @param Object message to log
       */
      public void info(Object message)
      {
          String out = null;
          
          if ( getStackTrace() &&
              (message instanceof Throwable || message instanceof Exception) )
          {
              out = StringUtils.stackTrace((Throwable)message);
          }
          else
          {
              out = message.toString();    
          }
          logger.info(INFO + out);
      }
  
      /**
       * Handle logging.
       *
       * @param Object message to log
       */
      public void error(Object message)
      {
          String out = null;
          
          if ( getStackTrace() &&
              ( message instanceof Throwable || message instanceof Exception ) )
          {
              out = StringUtils.stackTrace((Throwable)message);
          }
          else
          {
              out = message.toString();    
          }
          logger.error(ERROR + out);
      }
  
      /**
       * Handle logging.
       *
       * @param Object message to log
       */
      public void debug(Object message)
      {
          if (!DEBUG_ON) return;
          
          String out = null;
          
          if ( getStackTrace() &&
              ( message instanceof Throwable || message instanceof Exception ) )
          {
              out = StringUtils.stackTrace((Throwable)message);
          }
          else
          {
              out = message.toString();    
          }
          logger.debug(DEBUG + out);
      }
      
      public boolean getStackTrace()
      {
          return stackTrace;
      }
      
      public void setStackTrace(boolean value)
      {
          stackTrace = value;
      }
  }
  
  
  
  1.1                  jakarta-velocity/src/java/org/apache/velocity/runtime/log/LogSystem.java
  
  Index: LogSystem.java
  ===================================================================
  package org.apache.velocity.runtime.log;
  
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  /**
   * Base interface that Logging systems need to implement.
   *
   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
   * @version $Id: LogSystem.java,v 1.1 2001/03/12 07:19:53 jon Exp $
   */
  public interface LogSystem
  {
      public final static boolean DEBUG_ON = true;
  
      /** 
       * Prefix for warning messages.
       */
      public final static String WARN  = "  [warn] ";
      public final static int WARN_ID = 0;
  
      /** 
       * Prefix for info messages.
       */
      public final static String INFO  = "  [info] ";
      public final static int INFO_ID = 1;
      
      /** 
       * Prefix for debug messages.
       */
      public final static String DEBUG = " [debug] ";
      public final static int DEBUG_ID = 2;
      
      /** 
       * Prefix for error messages.
       */
      public final static String ERROR = " [error] ";
      public final static int ERROR_ID = 3;
  
      public void init(String logFile) throws Exception;
      
      public void info (Object messsage);
      public void error (Object messsage);
      public void debug (Object messsage);
      public void warn (Object messsage);
      public void log (int type, Object messsage);
      
      public boolean getStackTrace();
      public void setStackTrace(boolean value);
  }