mcconnell 2003/10/13 07:11:25
Modified:
merlin/composition/impl/src/java/org/apache/avalon/composition/logging/impl
DefaultLoggingManager.java
Added:
merlin/composition/impl/src/java/org/apache/avalon/composition/logging/impl
StandardFormatter.java
Log:
Setup local log formatter.
Revision Changes Path
1.2 +4 -4
avalon/merlin/composition/impl/src/java/org/apache/avalon/composition/logging/impl/DefaultLoggingManager.java
Index: DefaultLoggingManager.java
===================================================================
RCS file:
/home/cvs/avalon/merlin/composition/impl/src/java/org/apache/avalon/composition/logging/impl/DefaultLoggingManager.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DefaultLoggingManager.java 24 Sep 2003 09:31:48 -0000 1.1
+++ DefaultLoggingManager.java 13 Oct 2003 14:11:25 -0000 1.2
@@ -62,7 +62,6 @@
import org.apache.avalon.excalibur.i18n.ResourceManager;
import org.apache.avalon.excalibur.i18n.Resources;
import org.apache.avalon.framework.activity.Initializable;
-import org.apache.avalon.framework.logger.AvalonFormatter;
import org.apache.avalon.framework.logger.LogKitLogger;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.composition.data.CategoryDirective;
@@ -90,7 +89,7 @@
ResourceManager.getPackageResources( DefaultLoggingManager.class );
private static final StreamTarget DEFAULT_STREAM =
- new StreamTarget( System.out, new AvalonFormatter( DEFAULT_FORMAT ) );
+ new StreamTarget( System.out, new StandardFormatter( DEFAULT_FORMAT ) );
//---------------------------------------------------------------
// state
@@ -396,11 +395,12 @@
{
FileTargetProvider fileProvider = (FileTargetProvider) provider;
String filename = fileProvider.getLocation();
- final AvalonFormatter formatter = new AvalonFormatter( DEFAULT_FORMAT );
+ final StandardFormatter formatter = new StandardFormatter(
DEFAULT_FORMAT );
File file = new File( m_baseDirectory, filename );
try
{
- FileTarget logTarget = new FileTarget( file.getAbsoluteFile(),
false, formatter );
+ FileTarget logTarget =
+ new FileTarget( file.getAbsoluteFile(), false, formatter );
m_targets.put( name, logTarget );
} catch( final IOException ioe )
{
1.1
avalon/merlin/composition/impl/src/java/org/apache/avalon/composition/logging/impl/StandardFormatter.java
Index: StandardFormatter.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 1997-2003 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 acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software
* itself, if and wherever such third-party acknowledgments
* normally appear.
*
* 4. The names "Jakarta", "Avalon", 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 [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* 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/>.
*/
package org.apache.avalon.composition.logging.impl;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.framework.ExceptionUtil;
import org.apache.log.LogEvent;
import org.apache.log.format.ExtendedPatternFormatter;
import org.apache.log.format.PatternFormatter;
import org.apache.log.util.StackIntrospector;
/**
* This formatter extends ExtendedPatternFormatter so that
* CascadingExceptions are formatted with all nested exceptions.
*
* <ul>
* <li><code>class</code> : outputs the name of the class that has logged the
* message. The optional <code>short</code> subformat removes the
* package name. Warning : this pattern works only if formatting occurs in
* the same thread as the call to Logger, i.e. it won't work with
* <code>AsyncLogTarget</code>.</li>
* </ul>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Avalon Development Team</a>
* @version CVS $Revision: 1.1 $ $Date: 2003/10/13 14:11:25 $
*/
public class StandardFormatter
extends ExtendedPatternFormatter
{
private static final int TYPE_CLASS = MAX_TYPE + 1;
private static final String TYPE_CLASS_STR = "class";
private static final String TYPE_CLASS_SHORT_STR = "short";
/**
* The constant defining the default stack depth when
* none other is specified.
*
*/
public static final int DEFAULT_STACK_DEPTH = 8;
/**
* The constant defining the default behaviour for printing
* nested exceptions.
*
* @since 4.1.2
*/
public static final boolean DEFAULT_PRINT_CASCADING = true;
//The depth to which stacktraces are printed out
private final int m_stackDepth;
//Determines if nested exceptions should be logged
private final boolean m_printCascading;
/**
* Construct the formatter with the specified pattern
* and which which prints out exceptions to stackDepth of 8.
*
* @param pattern The pattern to use to format the log entries
* @since 4.1
*/
public StandardFormatter( final String pattern )
{
this( pattern, DEFAULT_STACK_DEPTH, DEFAULT_PRINT_CASCADING );
}
/**
* Construct the formatter with the specified pattern
* and which which prints out exceptions to stackDepth specified.
*
* @param pattern The pattern to use to format the log entries
* @param stackDepth The depth to which stacktraces are printed out
* @param printCascading true enables printing of nested exceptions,
* false only prints out the outermost exception
* @since 4.1.2
*/
public StandardFormatter( final String pattern, final int stackDepth,
final boolean printCascading )
{
super( pattern );
m_stackDepth = stackDepth;
m_printCascading = printCascading;
}
/**
* Utility method to format stack trace.
*
* @param throwable the throwable instance
* @param format ancilliary format parameter - allowed to be null
* @return the formatted string
*/
protected String getStackTrace( final Throwable throwable, final String format )
{
if( null == throwable )
{
return "";
}
return ExceptionUtil.printStackTrace( throwable, m_stackDepth,
m_printCascading );
}
/**
* Retrieve the type-id for a particular string.
*
* @param type the string
* @return the type-id
*/
protected int getTypeIdFor( final String type )
{
if( type.equalsIgnoreCase( TYPE_CLASS_STR ) )
{
return TYPE_CLASS;
}
else
{
return super.getTypeIdFor( type );
}
}
/**
* Return the result of formaltting a pattern run.
* @param event the log event
* @param run the patter formatter pattern run
* @return the formatted string
*/
protected String formatPatternRun( LogEvent event, PatternFormatter.PatternRun
run )
{
switch( run.m_type )
{
case TYPE_CLASS:
return getClass( run.m_format );
default:
return super.formatPatternRun( event, run );
}
}
/**
* Finds the class that has called Logger.
*/
private String getClass( String format )
{
final Class clazz = StackIntrospector.getCallerClass( Logger.class );
if( null == clazz )
{
return "Unknown-class";
}
else
{
// Found : the caller is the previous stack element
String className = clazz.getName();
// Handle optional format
if( TYPE_CLASS_SHORT_STR.equalsIgnoreCase( format ) )
{
int pos = className.lastIndexOf( '.' );
if( pos >= 0 )
{
className = className.substring( pos + 1 );
}
}
return className;
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]