You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by "Daniel L. Rall" <dl...@collab.net> on 2000/09/13 23:12:11 UTC
Re: cvs commit: jakarta-velocity/src/java/org/apache/velocity/log
Log.java LogHandle.java LogImpl.java Test.java
Thanks Bob! Any chance of making this coding conventions compliant?
werken@locus.apache.org wrote:
>
> werken 00/09/13 13:28:47
>
> Added: src/java/org/apache/velocity/log Log.java LogHandle.java
> LogImpl.java Test.java
> Log:
> Integrating logging subsystem donated by the kind folks
> at ifleet.com.
>
> Submitted by: james ce johnson (jcej@ifleet.com)
>
> Revision Changes Path
> 1.1 jakarta-velocity/src/java/org/apache/velocity/log/Log.java
>
> Index: Log.java
> ===================================================================
> /* ====================================================================
> * Copyright (c) iFleet, Inc. 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. All advertising materials mentioning features or use of this
> * software must display the following acknowledgment:
> * "This product includes software developed by the Apache Group
> * for use in the Apache HTTP server project (http://www.apache.org/)."
> *
> * 4. The names "Apache Server" and "Apache Group" 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.
> *
> * 6. Redistributions of any form whatsoever must retain the following
> * acknowledgment:
> * "This product includes software developed by the Apache Group
> * for use in the Apache HTTP server project (http://www.apache.org/)."
> *
> * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``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 GROUP 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 Group and was originally based
> * on public domain software written at the National Center for
> * Supercomputing Applications, University of Illinois, Urbana-Champaign.
> * For more information on the Apache Group and the Apache HTTP server
> * project, please see <http://www.apache.org/>.
> *
> */
>
> package org.apache.velocity.log;
>
> import java.io.*;
> import java.text.*;
> import java.util.*;
>
> /**
> <p>This class represents a logfile on disk. Actual logging
> is performed using {@link org.apache.velocity.LogHandle}
> instances.
>
> @author bob mcwhirter (bob@werken.com)
> @author James CE Johnson (jcej@tragus.org)
> @author Jim Crossley (jcrossly@ifleet.com)
> @author John Quinn (jquinn@ifleet.com)
> @author Keith SChnabble
>
> @see org.apache.velocity.LogHandle
> */
> public interface Log
> {
> /** Retrieve a LogHandler from this Log matching
> the identifier
>
> @param identifier String to tag message with
> @return The handle used for logging with the identifier
> */
> LogHandle getHandle(String identifier);
>
> /** Set the target of this Log
>
> @param logfile Filename of the logfile
> */
> void setTarget(String logfile) throws IOException;
>
> /** Set the target of this Log
>
> @param target Output sink for log messages
> */
> void setTarget(PrintWriter target);
>
> /** <p>Log an informative message
>
> @param logMessage The message to log
> */
> void info(String identifier,Object logMessage);
>
> /** <p>Log a warning message
>
> @param identifier Identifying tag for message
> @param logMessage The message to log
> */
> void warning(String identifier, Object logMessage);
>
> /** <p>Log an error message
>
> @param identifier Identifying tag for message
> @param logMessage The message to log
> */
> void error(String identifier,Object logMessage);
>
> /** <p>Log an exception (with stacktrace)
>
> @param identifier Identifying tag for message
> @param logMessage The message to log
> */
> void exception(String identifier, Object logMessage);
>
> /** <p>Log a debug message
>
> @param identifier Identifying tag for message
> @param logMessage The message to log
> */
> void debug(String identifier,Object logMessage);
> }
>
>
>
> 1.1 jakarta-velocity/src/java/org/apache/velocity/log/LogHandle.java
>
> Index: LogHandle.java
> ===================================================================
> /* ====================================================================
> * Copyright (c) iFleet, Inc. 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. All advertising materials mentioning features or use of this
> * software must display the following acknowledgment:
> * "This product includes software developed by the Apache Group
> * for use in the Apache HTTP server project (http://www.apache.org/)."
> *
> * 4. The names "Apache Server" and "Apache Group" 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.
> *
> * 6. Redistributions of any form whatsoever must retain the following
> * acknowledgment:
> * "This product includes software developed by the Apache Group
> * for use in the Apache HTTP server project (http://www.apache.org/)."
> *
> * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``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 GROUP 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 Group and was originally based
> * on public domain software written at the National Center for
> * Supercomputing Applications, University of Illinois, Urbana-Champaign.
> * For more information on the Apache Group and the Apache HTTP server
> * project, please see <http://www.apache.org/>.
> *
> */
>
> package org.apache.velocity.log;
>
> /**
> <p>Handle connected to a {@link org.apache.velocity.Log}
> to provide identified logging.
>
> <p>Typically, a LogHandle should be retrieved from the
> owning Log object. A common usage pattern is for each
> class to contain a static instance of LogHandle, and use
> the package/classname as the identifier.
> */
>
> public class LogHandle implements java.io.Serializable
> {
> public static final int _infoMask = 1;
> public static final int _warningMask = 2;
> public static final int _errorMask = 4;
> public static final int _exceptionMask = 8;
> public static final int _debugMask = 16;
> public static final int _noneMask = 0;
> public static final int _allMask = 255;
>
> /** Mask dictating what gets logged */
> private int _mask = _allMask;
>
> /** Parent 'owning' Log */
> private Log _log = null;
>
> /** Identifier used during loggin */
> private String _identifier = null;
>
> /** <p>Construct a LogHandle linked to a particular
> Log, and tagged with an identifier.
>
> @param log The log to delegate to
> @param identifier The identifier to identify what's getting logged
> */
>
> LogHandle(Log log,
> String identifier)
> {
> _log = log;
> _identifier = identifier;
> }
>
> /** <p>Set the mask to determine what actually gets logged
> through this handle.
>
> @param mask The bitmask
> */
> public void setMask (int mask)
> {
> _mask = mask;
> }
>
> /** <p>Retrieve the current bitmask
>
> @return bitmask
> */
> public int getMask ()
> {
> return _mask;
> }
>
> /** <p>Log an informative message
>
> @param logMessage The message to log
> */
> public void info(Object logMessage)
> {
> if ((_mask & _infoMask) != 0)
> _log.info(_identifier,
> logMessage);
> }
>
> /** <p>Log a warning message
>
> @param logMessage The message to log
> */
> public void warning(Object logMessage)
> {
> if ((_mask & _warningMask) != 0)
> _log.warning(_identifier,
> logMessage);
> }
>
> /** <p>Log an error message
>
> @param logMessage The message to log
> */
> public void error(Object logMessage)
> {
> if ((_mask & _errorMask) != 0)
> _log.error(_identifier,
> logMessage);
> }
>
> /** <p>Log an exception (with stacktrace)
>
> @param logMessage The message to log
> */
> public void exception(Object logMessage)
> {
> if ((_mask & _exceptionMask) != 0)
> _log.exception(_identifier,
> logMessage);
> }
>
> /** <p>Log a debug message
>
> @param logMessage The message to log
> */
> public void debug(Object logMessage)
> {
> if ((_mask & _debugMask) != 0)
> _log.debug(_identifier,
> logMessage);
> }
>
>
> }
>
>
>
>
> 1.1 jakarta-velocity/src/java/org/apache/velocity/log/LogImpl.java
>
> Index: LogImpl.java
> ===================================================================
> /* ====================================================================
> * Copyright (c) iFleet, Inc. 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. All advertising materials mentioning features or use of this
> * software must display the following acknowledgment:
> * "This product includes software developed by the Apache Group
> * for use in the Apache HTTP server project (http://www.apache.org/)."
> *
> * 4. The names "Apache Server" and "Apache Group" 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.
> *
> * 6. Redistributions of any form whatsoever must retain the following
> * acknowledgment:
> * "This product includes software developed by the Apache Group
> * for use in the Apache HTTP server project (http://www.apache.org/)."
> *
> * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``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 GROUP 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 Group and was originally based
> * on public domain software written at the National Center for
> * Supercomputing Applications, University of Illinois, Urbana-Champaign.
> * For more information on the Apache Group and the Apache HTTP server
> * project, please see <http://www.apache.org/>.
> *
> */
>
> package org.apache.velocity.log;
>
> import java.io.*;
> import java.text.*;
> import java.util.*;
>
>
> /**
> <p>Implementation of the Log interface to provide concrete
> logging facilities
>
> <p>Logs thread-id, date, identifier, message, and stack-trace
> if logging an exception.
> */
>
> public class LogImpl implements Log
> {
> /** Format for date logging */
> static private DateFormat dateFmt = DateFormat.getDateTimeInstance(DateFormat.SHORT,
> DateFormat.SHORT);
>
> /** Target for this log */
> private PrintWriter _target = null;
>
> /** Cache of handles (by identifier) */
> private Hashtable _handles = new Hashtable();
>
> /** Flag dictating if anything gets logged */
> private boolean quiet_ = (System.getProperty("log.quiet") != null);
>
> public LogImpl()
> {
> // intentionally left blank
> }
>
> /** Retrieve a LogHandler from this Log matching
> the identifier
>
> @param identifier String to tag message with
> @return The handle used for logging with the identifier
> */
> public synchronized LogHandle getHandle(String identifier)
> {
> LogHandle handle = (LogHandle) _handles.get(identifier);
>
> if (handle == null)
> {
> handle = new LogHandle(this,
> identifier);
> _handles.put(identifier, handle);
> }
> return handle;
> }
>
> /** Set the target of this Log
>
> If the target is null, then System.err. is used.
>
> @param logfile Filename of the logfile
> */
> public void setTarget(String logfile) throws IOException
>
> {
> PrintWriter out;
>
> if (logfile != null)
> {
> out = new PrintWriter(new FileWriter(logfile,true));
> }
> else
> {
> out = new PrintWriter(new OutputStreamWriter(System.err));
> }
>
> setTarget(out);
>
> out.flush();
> }
>
> /** Set the target of this Log
>
> If the target is null, then System.err is used.
>
> @param target Output sink for log messages
> */
> public void setTarget(PrintWriter target) {
>
> if (target == null)
> {
> target = new PrintWriter(new OutputStreamWriter(System.err));
> }
>
> _target = target;
> _target.println("------- LOG BEGINS [" + dateFmt.format(new Date()) + "] -------");
> }
>
> private void writeln(String level,
> String type,
> Object message)
> {
> if (!quiet_) {
> write(level,
> type,
> message);
> _target.println();
> _target.flush();
> }
> }
>
>
> private synchronized void write(String level,
> String type,
> Object message)
> {
> try
> {
> _target.print( new java.sql.Timestamp(System.currentTimeMillis())
> + "|" + Thread.currentThread().hashCode()
> + "|" + level
> + "|" + type
> + ": " + message);
> }
> catch (java.lang.Exception e)
> {
> System.err.println("** COULD NOT WRITE LOG! SWITCHING TO STDERR **");
> System.err.println(dateFmt.format(new Date())
> + "\t" + level + "\t" + message);
> System.err.flush();
> setTarget(new PrintWriter(System.err));
> }
> }
>
> /** <p>Log an informative message
>
> @param logMessage The message to log
> */
> public void info(String identifier,
> Object logMessage)
> {
> writeln("INFO",
> identifier,
> logMessage);
> }
>
> /** <p>Log a warning message
>
> @param identifier Identifying tag for message
> @param logMessage The message to log
> */
> public void warning(String identifier,
> Object logMessage)
> {
> writeln("WARN",
> identifier,
> logMessage);
> }
>
> /** <p>Log an error message
>
> @param identifier Identifying tag for message
> @param logMessage The message to log
> */
> public void error(String identifier,
> Object logMessage)
> {
> writeln("ERROR",
> identifier,
> logMessage);
> }
>
> /** <p>Log an exception (with stacktrace)
>
> @param identifier Identifying tag for message
> @param logMessage The message to log
> */
> public void exception(String identifier,
> Object logMessage)
> {
> if (logMessage instanceof Exception) {
> Exception e = (Exception) logMessage;
> write("EXCPT",
> identifier,
> "");
> e.printStackTrace(_target);
> _target.flush();
> } else {
> writeln("EXCPT",
> identifier,
> logMessage);
> }
> }
>
> /** <p>Log a debug message
>
> @param identifier Identifying tag for message
> @param logMessage The message to log
> */
> public void debug(String identifier,
> Object logMessage)
> {
> writeln("DEBUG",
> identifier,
> logMessage);
> }
>
>
>
> }
>
>
>
> 1.1 jakarta-velocity/src/java/org/apache/velocity/log/Test.java
>
> Index: Test.java
> ===================================================================
>
> package org.apache.velocity.log;
>
> public class Test
> {
> protected LogHandle _log =
> Engine.getLogHandle("Foo");
>
> public void bar()
> {
> _log.info("bar() called");
> }
>
> public static void main( String [] argv )
> {
> Test t = new Test();
> t.bar();
> }
> }
>
>
> class Engine
> {
> public static String LOGFILE = "./test.log";
> protected static LogImpl _logImplementation = null;
>
> public static LogHandle getLogHandle( String id )
> {
> if( _logImplementation == null )
> initialize();
> return _logImplementation.getHandle(id);
> }
>
> private static synchronized void initialize()
> {
> try
> {
> if( _logImplementation == null )
> {
> _logImplementation = new LogImpl();
> _logImplementation.setTarget( LOGFILE );
> }
> }
> catch( java.io.IOException e )
> {
> e.printStackTrace();
> }
> }
>
> }
>
>
>
--
Daniel Rall <dl...@collab.net>
http://collab.net/ | open source | do the right thing
Re: Log stuff
Posted by "Daniel L. Rall" <dl...@collab.net>.
bob wrote:
>
> On Wed, 13 Sep 2000, Daniel L. Rall wrote:
>
> > Thanks Bob! Any chance of making this coding conventions compliant?
>
> Yah. I'm kinda working on it in phases. I originally got it
> bare without javadocs, so I managed to squeeze those in, at least.
>
> Will tweak curlies and variable names Real Soon.
Heh, you rock man. No rush. :)
--
Daniel Rall <dl...@collab.net>
http://collab.net/ | open source | do the right thing
re: Log stuff
Posted by bob <bo...@accurev.com>.
On Wed, 13 Sep 2000, Daniel L. Rall wrote:
> Thanks Bob! Any chance of making this coding conventions compliant?
Yah. I'm kinda working on it in phases. I originally got it
bare without javadocs, so I managed to squeeze those in, at least.
Will tweak curlies and variable names Real Soon.
-bob