I have put an updated Log.java at the end of this message that gives the
ability to use
multiple log files while preserving the default "logfile=" property. Let
me know if there is anything I missed. Here are the details:
Here is a sample section for the TurbineResources.properties file:
#############################################################################
# The full path name to a log file
# if not given, commands to log events using org.apache.turbine.util.Log
# will be ignored.
# This file must already exist and be writable.
# Default: none
#
logfile=/usr/local/websites/tambora-transport/logs/wm-turbine.log
# Defines the number of custom logs you would like to create
# for use within your Turbine application.
# Default: 0
# Example:
# customlog.count=2
customlog.count=3
# The property name that corresponds to each new custom log.
# There must be the same number of entries here as are specified
# by the customlog.count property above.
#
# Default: none
#
# Example:
# In this example there are 2 custom logs.
# customlog1.property.name=log-db
# log-db=/turbine/logs/turbine-db.log
# customlog2.property.name=log-security
# log-security=/turbine/logs/turbine-sec.log
#
#Custom Log 1
customlog1.property.name=log-system
log-system=/turbine/logs/turbine-system.log
#Custom Log 2
customlog2.property.name=log-db
log-db=/turbine/logs/turbine-db.log
#Custom Log 3
customlog3.property.name=log-sec
log-sec=/turbine/logs/turbine-sec.log
###########################################################################
The Log class will look for both the "logfile=" property as it did
before and will now look for the "customlog.count=" property. If it
finds the customlog.count it will use the number specified to initialize
an array like this:
logfiles[(String)log property name][(FileWriter)logfile]
Using the above example of the TurbineResurces.properties file you can
then write to a log file like this:
// creates an entry in /turbine/logs/turbine-sec.log
Log.warn("log-sec","user x has logged into the system");
// creates an entry in /turbine/logs/turbine-db.log
Log.note("log-db","table z has been updated with the following info:");
Thanks,
John Thorhauer
--
********************************
** John Thorhauer
** [EMAIL PROTECTED]
********************************
#######################################################################
############# Updated Log.java ########################################
#######################################################################
package org.apache.turbine.util;
/*
* Copyright (c) 1997-1999 The Java Apache Project. 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 Java Apache
* Project for use in the Apache JServ servlet engine project
* <http://java.apache.org/>."
*
* 4. The names "Apache JServ", "Apache JServ Servlet Engine",
"Turbine",
* "Apache Turbine", "Turbine Project", "Apache Turbine Project" and
* "Java Apache Project" must not be used to endorse or promote
products
* derived from this software without prior written permission.
*
* 5. Products derived from this software may not be called "Apache
JServ"
* nor may "Apache" nor "Apache JServ" appear in their names without
* prior written permission of the Java Apache Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the Java Apache
* Project for use in the Apache JServ servlet engine project
* <http://java.apache.org/>."
*
* THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "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 JAVA APACHE PROJECT 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 Java Apache Group. For more information
* on the Java Apache Project and the Apache JServ Servlet Engine
project,
* please see <http://java.apache.org/>.
*
*/
// Java Core Classes
import java.util.Date;
import java.io.*;
import org.apache.turbine.services.resources.*;
/**
* This is a utility class that is used for logging events. Entries are
* allowed in 3 levels - Notice, Warning, Error.
* Methods are also available to log exceptions, which are always logged
* at the Error level.
*<p>
* The path to a writable file must be given in
TurbineResources.properties
* for the log to be written. If the file does not already exist, then
the
* file will be created. If the path is invalid, then the file will not
be
* created and nothing will happen.
*<p>
* To use this class outside of Turbine, all you need to do is set the
path
* to the TurbineResources.properties file first. To do this, you would
write
* code like this:
*<p>
* TurbineResources.setPropertiesFileName
("/path/to/TurbineResources.properties");<br>
* Log.note ("this is my notice!");
*
* @author John D. McNally
* @author Jon S. Stevens <a
href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>
*/
public class Log
{
private static String lf = System.getProperty("line.separator",
"\n");
// true if filename is valid and writeable
private static boolean isValid = true;
// filename for log file
private static FileWriter logfile = null;
// array to hold multiple FileWriter objects
private static Object logfiles[][];
// number of individual customlogfiles
private static int logCountInt = 0;
// Levels - Notice, Warning, Error
private static final String NOTICE = "NOTICE ";
private static final String WARNING = "WARNING";
private static final String ERROR = " ERROR ";
static
{
init();
}
/**
* checks to see if log variables are set up properly and
* the initializes the log(s)
*/
private static void init()
{
try
{
String logFileString = TurbineResources.getString("logfile",
null);
logCountInt = TurbineResources.getInt("customlog.count",0);
if (logFileString == null && logCountInt == 0)
{
throw new Exception (
"The logfile= property and the customlog.count= property
in the
TurbineResources.properties file are null or could not be found.\n" +
"One (but not both) of these properties must be defined
in the
TurbineResources.properties file. \n" +
"Please make sure that you have properly defined the
path to the
TurbineResources.properties file\n" +
"and you have called
\"TurbineResourceService.setPropertiesFileName(\"/path/to/TurbineResources.properties\");\"
in your
code.\n" +
"If you are using this class with the Turbine Servlet,
this should have been called for
you already.\n" +
"If you are getting this error using the Turbine
Servlet, the path to the properties file
or\n" +
"the path to the logfile or the logcount was not defined
correctly. Please refer to the
INSTALL \n" +
"document for instructions on how to do this.\n");
}
if (logFileString != null)
{
logfile = new FileWriter( logFileString, true );
}
// if there is a custom logfile count then set up an array
of FileWriters
// for use in the method Log.note(logname,text)
if (logCountInt!=0)
{
logfiles= new Object[logCountInt][2];
for (int i=0;i<logCountInt;i++)
{
int logNumber = i + 1;
String logPropertyName = "customlog" + logNumber +
".property.name";
//if any of the customlog[x].property.name or thier
corresponding
// filenames return null then
if ((TurbineResources.getString(logPropertyName,
null)==null) ||
(TurbineResources.getString(TurbineResources.getString(logPropertyName,
null),
null)==null))
{
throw new Exception(
"There is an error in the
TurbineResources.properties file. Be sure \n" +
"that the logcount= property correctly indicates
the number of logfiles \n" +
"that are setup in the
TurbineResources.properties file and that the \n" +
"log[x].property.name= properties are setup with
thier corresponding \n" +
"logfile entries.\n" +
"Please refer to the comments in the
TurbineResources.properties file \n" +
"or the user documentation for more information
on how to properly \n" +
"implement logging in a Turbine application.
\n");
}
logfiles[i][0]=
(String)TurbineResources.getString(logPropertyName, null);
logfiles[i][1]= (FileWriter) new
FileWriter(TurbineResources.getString((String)logfiles[i][0],
null),true);
}
}
}
catch (Exception e)
{
e.printStackTrace();
isValid = false;
}
catch (Throwable t)
{
isValid = false;
}
}
/*
* Makes a log entry at the NOTICE level
*
* @returns true if entry was entered, false otherwise
*/
public static boolean note(String description)
{
return log(NOTICE, description);
}
/*
* Specifies the log to print to and
* makes a log entry at the NOTICE level
*
* @returns true if entry was entered, false otherwise
*/
public static boolean note(String logname, String description)
{
return log(NOTICE, logname, description, null);
}
/*
* Makes a log entry at the WARNING level
*
* @returns true if entry was entered, false otherwise
*/
public static boolean warn(String description)
{
return log(WARNING, description);
}
/*
* Specifies the log to print to and
* makes a log entry at the WARNING level
*
* @returns true if entry was entered, false otherwise
*/
public static boolean warn(String logname, String description)
{
return log(WARNING, logname, description, null);
}
/*
* Makes a log entry at the ERROR level
*
* @returns true if entry was entered, false otherwise
*/
public static boolean error(String description)
{
System.out.print("Error=" + description + "\n");
return log(ERROR, description);
}
/*
* Specifies the log to print to and
* makes a log entry at the ERROR level
*
* @returns true if entry was entered, false otherwise
*/
public static boolean error(String logname, String description)
{
return log(ERROR, logname, description, null);
}
/*
* Makes a log entry at the ERROR level
*
* @returns true if entry was entered, false otherwise
*/
public static boolean error(String description, Throwable t)
{
return log(ERROR, description, t);
}
/*
* Specifies the log to print to and
* makes a log entry at the ERROR level
*
* @returns true if entry was entered, false otherwise
*/
public static boolean error(String logname, String description,
Throwable t)
{
return log(ERROR, logname, description, t);
}
/*
* Makes a log entry at the ERROR level
*
* @returns true if entry was entered, false otherwise
*/
public static boolean error(Throwable t)
{
return log(ERROR, "", t);
}
private static boolean log(String level, String description)
{
return log(level, description, null);
}
private static boolean log(String level, String description,
Throwable t)
{
if ( isValid )
{
Date date = new Date();
StringBuffer logEntry = new StringBuffer();
logEntry.append ( "[" );
logEntry.append ( date.toString() );
logEntry.append ( "] -- " );
logEntry.append ( level );
logEntry.append ( " -- " );
logEntry.append ( description );
logEntry.append ( lf );
if (t != null)
{
ByteArrayOutputStream ostr = new
ByteArrayOutputStream();
PrintWriter out = new PrintWriter(ostr,true);
out.write(logEntry.toString());
out.write("\tException: ");
out.write(t.toString());
out.write(lf+"\tStack Trace follows:"+lf+"\t");
t.printStackTrace(out);
logEntry = new StringBuffer( ostr.toString() );
}
synchronized( logfile )
{
try
{
logfile.write( logEntry.toString(), 0,
logEntry.length());
logfile.flush();
}
catch(IOException ioe)
{
// ignore, skip log, should not occur since we have
checked file
// but catching the exception allows Log to be used
elsewhere in
// catch blocks.
return false;
}
}
return true;
}
return false;
}
/**
* Creates a log entry in the log passed in as logname
*/
private static boolean log(String level, String logname, String
description, Throwable t)
{
isValid = false;
int i = 0;
int activeLogNumber = 0;
for (i=0;i<logCountInt;i++)
{
if (logfiles[i][0].equals(logname))
{
activeLogNumber = i;
isValid=true;
break;
}
}
if ( isValid )
{
Date date = new Date();
StringBuffer logEntry = new StringBuffer();
logEntry.append ( "[" );
logEntry.append ( date.toString() );
logEntry.append ( "] -- " );
logEntry.append ( level );
logEntry.append ( " -- " );
logEntry.append ( description );
logEntry.append ( lf );
if (t != null)
{
ByteArrayOutputStream ostr = new
ByteArrayOutputStream();
PrintWriter out = new PrintWriter(ostr,true);
out.write(logEntry.toString());
out.write("\tException: ");
out.write(t.toString());
out.write(lf+"\tStack Trace follows:"+lf+"\t");
t.printStackTrace(out);
logEntry = new StringBuffer( ostr.toString() );
}
synchronized( (FileWriter)logfiles[activeLogNumber][1] )
{
try
{
((FileWriter)logfiles[activeLogNumber][1]).write(
logEntry.toString(), 0,
logEntry.length());
((FileWriter)logfiles[activeLogNumber][1]).flush();
}
catch(IOException ioe)
{
// ignore, skip log, should not occur since we have
checked file
// but catching the exception allows Log to be used
elsewhere in
// catch blocks.
return false;
}
}
return true;
}
return false;
}
/**
Attempt to close the log file when the class is GC'd
*/
public void destroy()
{
try
{
logfile.close();
}
catch (Exception e)
{}
}
}
------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Search: <http://www.mail-archive.com/turbine%40list.working-dogs.com/>
Problems?: [EMAIL PROTECTED]