Differences between revisions 1 and 2
Revision 1 as of 2005-10-02 05:00:33
Size: 5246
Comment: move from contrib to Wiki
Revision 2 as of 2009-09-20 22:06:15
Size: 5246
Editor: localhost
Comment: converted to 1.6 markup
No differences found!

Contributed by Christoph Reck <Christoph.Reck@dlr.de> with the following [edited] message to the user list:

For anyone interested, I've created a simple StderrLogSystem as a drop-in for Velocity - see attachment. Either it can be taken into the velocity contribution section, or put into velocity as the standard logger, or you can change the package name to whatever you want and include it in your distribution.

The reason for this is that I do not want stray files created (velocity.log) in my file system when running my Anakia commandline tool.

I use it as follows:

within main()

    // The classname of the default logger to be used
    String loggerClass =
      "StderrLogSystem";

    Vector vargs = new Vector();
    for( int i = 0; i < args.length; ++i )
    {
      if( args[i].equals("-quiet") )
        loggerClass = "org.apache.velocity.runtime.log.NullLogSystem";
      else if( args[i].equals("-verbose") )
        loggerClass = "StderrLogSystem";
      else
        vargs.add( args[i] );
    }

and for the ve.init():

    templatePath = new File(templatePath).getCanonicalPath();
    velocity = new VelocityEngine();
    velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templatePath);
    velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, loggerClass);
    velocity.init();

NOTE: templatePath is "." for my current XmlTransformer batch tool.

StderrLogSystem.java:

/*
 * Copyright 2001-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.
 */

import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.runtime.RuntimeConstants;

import org.apache.velocity.runtime.log.LogSystem;

/**
 * Implementation of a Simple logger to output messages to STDERR.
 *
 * @author    <a href="mailto:Christoph.Reck@dlr.de">Christoph Reck</a>
 * @version   $Id: StderrLogSystem.java 191743 2005-06-21 23:22:20Z dlr $
 */
public class StderrLogSystem implements LogSystem
{
    /**
     * Empty constructor.
     */
    public StderrLogSystem()
    {
        // nothing to do
    }
    
    /**
     * Do the initialization (this logger does not do anything here).
     */
    public void init( RuntimeServices rs ) throws Exception
    {
        // nothing to do
    }
    
    /**
     * Does the acutal message logging.
     *
     * @param level    severity level
     * @param message  complete error message
     */
    public void logVelocityMessage( int level, String message )
    {
        switch ( level )
        {
        case LogSystem.WARN_ID:
            System.err.println( RuntimeConstants.WARN_PREFIX + message );
            break;
        case LogSystem.INFO_ID:
            System.err.println( RuntimeConstants.INFO_PREFIX + message );
            break;
        case LogSystem.DEBUG_ID:
            System.err.println( RuntimeConstants.DEBUG_PREFIX + message );
            break;
        case LogSystem.ERROR_ID:
            System.err.println( RuntimeConstants.ERROR_PREFIX + message );
            break;
        default:
            System.err.println( message );
            break;
        }
    }
}

Test.java

/*
 * Copyright 2001-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.
 */

import org.apache.velocity.app.VelocityEngine;

/**
 *  simple class to demonstrate how to use the
 *  StderrLogSystem.  It doesn't do anything but
 *  setup and init(), but that should produce
 *  enough output...
 *
 *  @author <a href="geirm@apache.org">Geir Magnusson Jr.</a>
 *  @version $Id: Test.java 191743 2005-06-21 23:22:20Z dlr $
 */
public class Test
{
    Test()
    {
        VelocityEngine ve = new VelocityEngine();

        ve.setProperty("runtime.log.logsystem.class", "StderrLogSystem");

        try
        {
            ve.init();
        }
        catch(Exception e )
        {
            System.out.println( e );
        }
    }

    public static void main( String args[] )
    {
        Test t = new Test();
    }
}

StdErrLogSystem (last edited 2009-09-20 22:06:15 by localhost)