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/19 09:05:09 UTC

cvs commit: jakarta-velocity/src/java/org/apache/velocity/runtime/log Log4JLogSystem.java

jon         01/03/19 00:05:09

  Modified:    src/java/org/apache/velocity/runtime Runtime.java
                        RuntimeConstants.java
               src/java/org/apache/velocity/runtime/defaults
                        velocity.properties
  Added:       src/java/org/apache/velocity/runtime/log Log4JLogSystem.java
  Log:
  fixed a bug in Runtime that was a copy/paste error by geir in logging
  stuff
  
  added Log4JLogSystem
  
  added necessary properties
  
  added necessary stuff to RuntimeConstants
  
  Revision  Changes    Path
  1.100     +2 -2      jakarta-velocity/src/java/org/apache/velocity/runtime/Runtime.java
  
  Index: Runtime.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/Runtime.java,v
  retrieving revision 1.99
  retrieving revision 1.100
  diff -u -r1.99 -r1.100
  --- Runtime.java	2001/03/19 06:15:54	1.99
  +++ Runtime.java	2001/03/19 08:05:07	1.100
  @@ -139,7 +139,7 @@
    * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
    * @author <a href="mailto:jlb@houseofdistraction.com">Jeff Bowden</a>
    * @author <a href="mailto:geirm@optonline.net">Geir Magusson Jr.</a>
  - * @version $Id: Runtime.java,v 1.99 2001/03/19 06:15:54 jon Exp $
  + * @version $Id: Runtime.java,v 1.100 2001/03/19 08:05:07 jon Exp $
    */
   public class Runtime implements RuntimeConstants
   {    
  @@ -633,7 +633,7 @@
                   out = DEBUG_PREFIX;
                   break;
               case LogSystem.INFO_ID :
  -                out = WARN_PREFIX;
  +                out = INFO_PREFIX;
                   break;
               case LogSystem.WARN_ID :
                   out = WARN_PREFIX;
  
  
  
  1.21      +93 -9     jakarta-velocity/src/java/org/apache/velocity/runtime/RuntimeConstants.java
  
  Index: RuntimeConstants.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/RuntimeConstants.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- RuntimeConstants.java	2001/03/19 05:20:30	1.20
  +++ RuntimeConstants.java	2001/03/19 08:05:07	1.21
  @@ -54,6 +54,16 @@
    * <http://www.apache.org/>.
    */
   
  +/**
  + * This class defines the keys that are used in the 
  + * velocity.properties file so that they can be referenced as a constant
  + * within Java code.
  + *
  + * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
  + * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
  + * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
  + * @version $Id: RuntimeConstants.java,v 1.21 2001/03/19 08:05:07 jon Exp $
  + */
   public interface RuntimeConstants
   {
       /*
  @@ -73,18 +83,20 @@
       /** 
        * Location of the velocity log file.
        */
  -    public static final String RUNTIME_LOG = "runtime.log";
  +    public static final String RUNTIME_LOG = 
  +        "runtime.log";
       
       /**
        *  externally provided logger
        */
  -    public static final String RUNTIME_LOG_LOGSYSTEM = "runtime.log.logsystem";
  +    public static final String RUNTIME_LOG_LOGSYSTEM = 
  +        "runtime.log.logsystem";
   
  -
       /**
        *  class of log system to use
        */
  -    public static final String RUNTIME_LOG_LOGSYSTEM_CLASS = "runtime.log.logsystem.class";
  +    public static final String RUNTIME_LOG_LOGSYSTEM_CLASS = 
  +        "runtime.log.logsystem.class";
   
       /** 
        * Stack trace output for error messages.
  @@ -110,11 +122,6 @@
       public static final String RUNTIME_LOG_REFERENCE_LOG_INVALID  = 
           "runtime.log.invalid.references";
   
  -    /** 
  -     * The encoding to use for the template.
  -     */
  -    public static final String TEMPLATE_ENCODING = "template.encoding";
  -
       /**
        *  Log message prefixes
        */
  @@ -122,7 +129,80 @@
       public final static String INFO_PREFIX  = "  [info] ";
       public final static String WARN_PREFIX  = "  [warn] ";
       public final static String ERROR_PREFIX = " [error] ";
  +    public final static String UNKNOWN_PREFIX = " [unknown] ";
  +
  +    /**
  +     * Log4J configuration
  +     */
  +    public final static String LOGSYSTEM_LOG4J_PATTERN = 
  +        "runtime.log.logsystem.log4j.pattern";
  +
  +    /**
  +     * Log4J configuration
  +     */
  +    public final static String LOGSYSTEM_LOG4J_FILE_SIZE = 
  +        "runtime.log.logsystem.log4j.file.size";
  +
  +    /**
  +     * Log4J configuration
  +     */
  +    public final static String LOGSYSTEM_LOG4J_FILE_BACKUPS = 
  +        "runtime.log.logsystem.log4j.file.backups";
  +
  +    /**
  +     * Log4J configuration
  +     */
  +    public final static String LOGSYSTEM_LOG4J_SYSLOGD_HOST = 
  +        "runtime.log.logsystem.log4j.syslogd.host";
  +
  +    /**
  +     * Log4J configuration
  +     */
  +    public final static String LOGSYSTEM_LOG4J_SYSLOGD_FACILITY = 
  +        "runtime.log.logsystem.log4j.syslogd.facility";
  +
  +    /**
  +     * Log4J configuration
  +     */
  +    public final static String LOGSYSTEM_LOG4J_REMOTE_HOST = 
  +        "runtime.log.logsystem.log4j.remote.host";
  +
  +    /**
  +     * Log4J configuration
  +     */
  +    public final static String LOGSYSTEM_LOG4J_REMOTE_PORT = 
  +        "runtime.log.logsystem.log4j.remote.port";
  +
  +    /**
  +     * Log4J configuration
  +     */
  +    public final static String LOGSYSTEM_LOG4J_EMAIL_SERVER = 
  +        "runtime.log.logsystem.log4j.email.server";
  +
  +    /**
  +     * Log4J configuration
  +     */
  +    public final static String LOGSYSTEM_LOG4J_EMAIL_FROM = 
  +        "runtime.log.logsystem.log4j.email.from";
   
  +    /**
  +     * Log4J configuration
  +     */
  +    public final static String LOGSYSTEM_LOG4J_EMAIL_TO = 
  +        "runtime.log.logsystem.log4j.email.to";
  +
  +    /**
  +     * Log4J configuration
  +     */
  +    public final static String LOGSYSTEM_LOG4J_EMAIL_SUBJECT = 
  +        "runtime.log.logsystem.log4j.email.subject";
  +
  +    /**
  +     * Log4J configuration
  +     */
  +    public final static String LOGSYSTEM_LOG4J_EMAIL_BUFFER_SIZE = 
  +        "runtime.log.logsystem.log4j.email.buffer.size";
  +
       /*
        * ----------------------------------------------------------------------
        * D I R E C T I V E  C O N F I G U R A T I O N
  @@ -251,6 +331,10 @@
       public static String INTERPOLATE_STRINGLITERALS = 
           "runtime.interpolate.string.literals";
   
  +    /** 
  +     * The encoding to use for the template.
  +     */
  +    public static final String TEMPLATE_ENCODING = "template.encoding";
   
       /*
        * ----------------------------------------------------------------------
  
  
  
  1.20      +26 -1     jakarta-velocity/src/java/org/apache/velocity/runtime/defaults/velocity.properties
  
  Index: velocity.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/defaults/velocity.properties,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- velocity.properties	2001/03/19 05:20:47	1.19
  +++ velocity.properties	2001/03/19 08:05:08	1.20
  @@ -10,10 +10,11 @@
   #----------------------------------------------------------------------------
   
   #----------------------------------------------------------------------------
  -#  default LogSystem to use
  +#  default LogSystem to use: default: AvalonLogSystem
   #----------------------------------------------------------------------------
   
   runtime.log.logsystem.class = org.apache.velocity.runtime.log.AvalonLogSystem
  +#runtime.log.logsystem.class = org.apache.velocity.runtime.log.Log4JLogSystem
   
   #---------------------------------------------------------------------------
   # This is the location of the Velocity Runtime log.
  @@ -31,6 +32,30 @@
   runtime.log.warn.stacktrace = false
   runtime.log.info.stacktrace = false
   runtime.log.invalid.reference = true
  +
  +#----------------------------------------------------------------------------
  +# Configuration for the Log4JLogSystem. 
  +# You must define the runtime.log.logsystem.class property to be: 
  +#   org.apache.velocity.runtime.log.Log4JLogSystem
  +#
  +# You must also include Log4J's .jar files into your classpath. They are
  +# included with the Velocity distribution in the build/lib directory.
  +#
  +# There are several different options that you can configure. 
  +# Uncomment the ones that you want and also define their settings.
  +#----------------------------------------------------------------------------
  +#runtime.log.logsystem.log4j.pattern=%d - %m%n
  +#runtime.log.logsystem.log4j.file.size=100000
  +#runtime.log.logsystem.log4j.file.backups=1
  +#runtime.log.logsystem.log4j.syslogd.host=my.syslog.server.com
  +#runtime.log.logsystem.log4j.syslogd.facility=LOG_DAEMON
  +#runtime.log.logsystem.log4j.remote.host=my.remote.server.com
  +#runtime.log.logsystem.log4j.remote.port=1099
  +#runtime.log.logsystem.log4j.email.server=localhost
  +#runtime.log.logsystem.log4j.email.from=root@localhost
  +#runtime.log.logsystem.log4j.email.to=root@localhost
  +#runtime.log.logsystem.log4j.email.subject=Velocity Error Report
  +#runtime.log.logsystem.log4j.email.buffer.size=512
   
   #----------------------------------------------------------------------------
   # T E M P L A T E  E N C O D I N G
  
  
  
  1.1                  jakarta-velocity/src/java/org/apache/velocity/runtime/log/Log4JLogSystem.java
  
  Index: Log4JLogSystem.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", "Velocity", 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.util.Enumeration;
  
  import org.apache.log4j.*;
  import org.apache.log4j.net.*;
  import org.apache.log4j.spi.*;
  
  import org.apache.velocity.runtime.Runtime;
  
  /**
   * Implementation of a Log4J logger.
   *
   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
   * @version $Id: Log4JLogSystem.java,v 1.1 2001/03/19 08:05:09 jon Exp $
   */
  public class Log4JLogSystem implements LogSystem
  {
      /** log4java logging interface */
      protected Category logger = null;
  
      /** logging layout */
      protected Layout layout = null;
  
      /** the runtime.log property value */
      private String logfile = "";
      
      /**
       *  default CTOR.  Initializes itself using the property RUNTIME_LOG
       *  from the Velocity properties
       */
      public Log4JLogSystem()
      {
          /*
           *  since this is a Velocity-provided logger, we will
           *  use the Runtime configuration
           */
          logfile = Runtime.getString( Runtime.RUNTIME_LOG );
  
          /*
           *  now init.  If we can't, panic!
           */
          try
          {
              init();
  
              logVelocityMessage( 0, 
                  "Log4JLogSystem initialized using logfile " + logfile );
          }
          catch( Exception e )
          {
              System.out.println( 
                  "PANIC : error configuring Log4JLogSystem : " + e );
          }
      }
  
      /**
       *  initializes the log system using the logfile argument
       *
       *  @param logFile   file for log messages
       */
      public void init()
          throws Exception
      {
          logger = Category.getInstance("");
          logger.setAdditivity(false);
          // Priority is set for DEBUG becouse this implementation checks 
          // log level.
          logger.setPriority(Priority.DEBUG);
  
          String pattern = Runtime.getString( Runtime.LOGSYSTEM_LOG4J_PATTERN );
          if (pattern == null || pattern.length() == 0)
          {
              pattern = "%d - %m%n";
          }
          layout = new PatternLayout(pattern);
          configureFile();
          configureRemote();
          configureSyslog();
          configureEmail();
      }
  
      /**
       * Configures the logging to a file.
       */
      private void configureFile()
          throws Exception
      {
          int backupFiles = 
              Runtime.getInt(Runtime.LOGSYSTEM_LOG4J_FILE_BACKUPS, 1);
          int fileSize = 
              Runtime.getInt(Runtime.LOGSYSTEM_LOG4J_FILE_SIZE, 100000);
  		Appender appender = new RollingFileAppender(layout,logfile,true);
  		((RollingFileAppender)appender).setMaxBackupIndex(backupFiles);
  		//finding file size
  		if (fileSize > -1)
  		{
  			((RollingFileAppender)appender).setMaxFileSize(fileSize);
  		}
          logger.addAppender(appender);
      }
  
      /**
       * Configures the logging to a remote server
       */
      private void configureRemote()
          throws Exception
      {
          String remoteHost = 
              Runtime.getString(Runtime.LOGSYSTEM_LOG4J_REMOTE_HOST);
          int remotePort = 
              Runtime.getInt(Runtime.LOGSYSTEM_LOG4J_REMOTE_PORT, 1099);
          if (remoteHost == null || remoteHost.trim().equals("") || 
              remotePort <= 0)
          {
              return;
          }
          Appender appender=new SocketAppender(remoteHost,remotePort);
          logger.addAppender(appender);
      }
  
      /**
       * Configures the logging to syslogd
       */
      private void configureSyslog()
          throws Exception
      {
          String syslogHost = 
              Runtime.getString(Runtime.LOGSYSTEM_LOG4J_SYSLOGD_HOST);
          String syslogFacility = 
              Runtime.getString(Runtime.LOGSYSTEM_LOG4J_SYSLOGD_FACILITY);
          if (syslogHost == null || syslogHost.trim().equals("") || 
              syslogFacility == null )
          {
              return;
          }
  
          Appender appender = new SyslogAppender();
          ((SyslogAppender)appender).setLayout(layout);
          ((SyslogAppender)appender).setSyslogHost(syslogHost);
          ((SyslogAppender)appender).setFacility(syslogFacility);
          logger.addAppender(appender);
      }
  
      /**
       * Configures the logging to syslogd
       */
      private void configureEmail()
          throws Exception
      {
          String smtpHost = 
              Runtime.getString(Runtime.LOGSYSTEM_LOG4J_EMAIL_SERVER);
          String emailFrom = 
              Runtime.getString(Runtime.LOGSYSTEM_LOG4J_EMAIL_FROM);
          String emailTo = 
              Runtime.getString(Runtime.LOGSYSTEM_LOG4J_EMAIL_TO);
          String emailSubject = 
              Runtime.getString(Runtime.LOGSYSTEM_LOG4J_EMAIL_SUBJECT);
          String bufferSize = 
              Runtime.getString(Runtime.LOGSYSTEM_LOG4J_EMAIL_BUFFER_SIZE);
  
          if (smtpHost == null || smtpHost.trim().equals("")
                  || emailFrom == null || smtpHost.trim().equals("")
                  || emailTo == null || emailTo.trim().equals("")
                  || emailSubject == null || emailSubject.trim().equals("")
                  || bufferSize == null || bufferSize.trim().equals("") )
          {
              return;
          }
  
          SMTPAppender appender = new SMTPAppender();
          appender.setOption(SMTPAppender.SMTP_HOST_OPTION, smtpHost);
          appender.setOption(SMTPAppender.FROM_OPTION, emailFrom);
          appender.setOption(SMTPAppender.TO_OPTION, emailTo);
          appender.setOption(SMTPAppender.SUBJECT_OPTION, emailSubject);
          appender.setOption(SMTPAppender.BUFFER_SIZE_OPTION, bufferSize);
          appender.setLayout(layout);
          appender.activateOptions();
          logger.addAppender(appender);
      }
  
      /**
       *  logs messages
       *
       *  @param level severity level
       *  @param message complete error message
       */
      public void logVelocityMessage(int level, String message)
      {
          switch (level) 
          {
              case LogSystem.WARN_ID:
                  logger.warn( message );
                  break;
              case LogSystem.INFO_ID:
                  logger.info(message);
                  break;
              case LogSystem.DEBUG_ID:
                  logger.debug(message);
                  break;
              case LogSystem.ERROR_ID:
                  logger.error(message);
                  break;
              default:
                  logger.debug(message);
                  break;
          }
      }
  
      /**
       * Also do a shutdown if the object is destroy()'d.
       */
      protected void finalize() throws Throwable
      {
          shutdown();
      }
  
      /** Close all destinations*/
      public void shutdown()
      {
          Enumeration appenders = logger.getAllAppenders();
          while (appenders.hasMoreElements())
          {
              Appender appender = (Appender)appenders.nextElement();
              appender.close();
          }
      }
  }