donaldp 01/03/26 20:20:53
Modified: src/java/org/apache/log LogKit.java Logger.java
Added: src/java/org/apache/log LogEngine.java
Log:
Updated logkit to support multiple hierarchies via LogEngines.
Revision Changes Path
1.2 +14 -71 jakarta-avalon-logkit/src/java/org/apache/log/LogKit.java
Index: LogKit.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-logkit/src/java/org/apache/log/LogKit.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- LogKit.java 2001/02/20 01:38:04 1.1
+++ LogKit.java 2001/03/27 04:20:53 1.2
@@ -12,6 +12,7 @@
import java.util.*;
import org.apache.log.output.*;
import org.apache.log.output.DefaultOutputLogTarget;
+
/**
* The LogKit provides the access to static methods to
* manipulate the logging sub-system
@@ -21,18 +22,7 @@
public final class LogKit
{
protected static final ThreadLocal c_context = new
ThreadLocal();
- protected static final Hashtable c_loggers = new
Hashtable();
- protected static final Hashtable c_categories = new
Hashtable();
- protected static final Hashtable c_logTargets;
- protected static Priority.Enum c_priority =
Priority.DEBUG;
- protected static LogTarget c_defaultLogTarget;
-
- static
- {
- c_logTargets = new Hashtable();
- c_defaultLogTarget = new DefaultOutputLogTarget();
- c_logTargets.put( "default", c_defaultLogTarget );
- }
+ protected static final LogEngine c_engine = new
LogEngine();
/*
//Need to add this in at LogManager level rather than here ....
@@ -97,8 +87,7 @@
*/
public static void addLogTarget( final String name, final LogTarget target )
{
- if( name.equals("default") ) c_defaultLogTarget = target;
- c_logTargets.put( name, target );
+ c_engine.addLogTarget( name, target );
}
/**
@@ -109,7 +98,7 @@
*/
public static LogTarget getLogTarget( final String name )
{
- return (LogTarget)c_logTargets.get( name );
+ return c_engine.getLogTarget( name );
}
/**
@@ -122,17 +111,7 @@
public static Category createCategory( final String categoryName,
final Priority.Enum priority )
{
- Category category = (Category)c_categories.get( categoryName );
-
- if( null == category )
- {
- category = new Category( categoryName );
- c_categories.put( categoryName, category );
- }
-
- category.setPriority( priority );
-
- return category;
+ return c_engine.createCategory( categoryName, priority );
}
/**
@@ -143,7 +122,7 @@
*/
public static Logger createLogger( final Category category )
{
- return createLogger( category, null );
+ return c_engine.createLogger( category );
}
/**
@@ -155,33 +134,7 @@
*/
public static Logger createLogger( final Category category, final LogTarget
logTargets[] )
{
- final String categoryName = category.getName();
- Logger logger = (Logger)c_loggers.get( categoryName );
-
- if( null == logger )
- {
- final int index = categoryName.lastIndexOf( Category.SEPARATOR );
-
- Logger parent = null;
-
- if( -1 != index )
- {
- final String parentName = categoryName.substring( 0, index );
- parent = getLoggerFor( parentName );
- }
-
- logger = new Logger( category, logTargets, parent );
- c_loggers.put( categoryName, logger );
- }
- else
- {
- if( null != logTargets )
- {
- logger.setLogTargets( logTargets );
- }
- }
-
- return logger;
+ return c_engine.createLogger( category, logTargets );
}
/**
@@ -213,7 +166,7 @@
*/
public static LogTarget getDefaultLogTarget()
{
- return c_defaultLogTarget;
+ return c_engine.getDefaultLogTarget();
}
/**
@@ -223,7 +176,7 @@
*/
public static Priority.Enum getGlobalPriority()
{
- return c_priority;
+ return c_engine.getGlobalPriority();
}
/**
@@ -234,15 +187,7 @@
*/
public static Logger getLoggerFor( final String category )
{
- synchronized( c_loggers )
- {
- Logger logger = (Logger)c_loggers.get( category );
- if( null == logger )
- {
- logger = createLogger( createCategory( category, Priority.DEBUG ) );
- }
- return logger;
- }
+ return c_engine.getLoggerFor( category );
}
/**
@@ -266,8 +211,7 @@
*/
public static void log( final String message, final Throwable t )
{
- System.err.println( "Error: " + message );
- t.printStackTrace();
+ c_engine.log( message, t );
}
/**
@@ -275,7 +219,7 @@
*/
public static void log( final String message )
{
- System.err.println( "Error: " + message );
+ c_engine.log( message );
}
/**
@@ -283,8 +227,7 @@
*/
public static void setDefaultLogTarget( final LogTarget defaultLogTarget )
{
- addLogTarget( "default", defaultLogTarget );
- c_defaultLogTarget = defaultLogTarget;
+ c_engine.setDefaultLogTarget( defaultLogTarget );
}
/**
@@ -293,7 +236,7 @@
*/
public static void setGlobalPriority( final Priority.Enum priority )
{
- c_priority = priority;
+ c_engine.setGlobalPriority( priority );
}
/**
1.2 +28 -23 jakarta-avalon-logkit/src/java/org/apache/log/Logger.java
Index: Logger.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-logkit/src/java/org/apache/log/Logger.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Logger.java 2001/02/20 01:38:04 1.1
+++ Logger.java 2001/03/27 04:20:53 1.2
@@ -16,6 +16,7 @@
{
protected final static long START_TIME =
System.currentTimeMillis();
+ protected final LogEngine m_engine;
protected final Logger m_parent;
protected final Category m_category;
protected LogTarget[] m_logTargets;
@@ -25,9 +26,9 @@
*
* @param category the category
*/
- public Logger( final Category category )
+ public Logger( final LogEngine engine, final Category category )
{
- this( category, null, null );
+ this( engine, category, null, null );
}
/**
@@ -36,21 +37,25 @@
* @param category the category
* @param logTargets the targets
*/
- public Logger( final Category category, final LogTarget[] logTargets )
+ public Logger( final LogEngine engine, final Category category, final
LogTarget[] logTargets )
{
- this( category, logTargets, null );
+ this( engine, category, logTargets, null );
}
- public Logger( final Category category, final LogTarget[] logTargets, final
Logger parent )
+ public Logger( final LogEngine engine, final Category category, final Logger
parent )
{
- m_category = category;
- m_logTargets = logTargets;
- m_parent = parent;
+ this( engine, category, null, parent );
}
- public Logger( final Category category, final Logger parent )
+ public Logger( final LogEngine engine,
+ final Category category,
+ final LogTarget[] logTargets,
+ final Logger parent )
{
- this( category, null, parent );
+ m_engine = engine;
+ m_category = category;
+ m_logTargets = logTargets;
+ m_parent = parent;
}
/**
@@ -62,7 +67,7 @@
public final void debug( final String message, final Throwable throwable )
{
if( m_category.getPriority().isLowerOrEqual( Priority.DEBUG ) &&
- LogKit.getGlobalPriority().isLowerOrEqual( Priority.DEBUG ) )
+ m_engine.getGlobalPriority().isLowerOrEqual( Priority.DEBUG ) )
{
output( Priority.DEBUG, message, throwable );
}
@@ -76,7 +81,7 @@
public final void debug( final String message )
{
if( m_category.getPriority().isLowerOrEqual( Priority.DEBUG ) &&
- LogKit.getGlobalPriority().isLowerOrEqual( Priority.DEBUG ) )
+ m_engine.getGlobalPriority().isLowerOrEqual( Priority.DEBUG ) )
{
output( Priority.DEBUG, message, null );
}
@@ -91,7 +96,7 @@
public final void error( final String message, final Throwable throwable )
{
if( m_category.getPriority().isLowerOrEqual( Priority.ERROR ) &&
- LogKit.getGlobalPriority().isLowerOrEqual( Priority.ERROR ) )
+ m_engine.getGlobalPriority().isLowerOrEqual( Priority.ERROR ) )
{
output( Priority.ERROR, message, throwable );
}
@@ -105,7 +110,7 @@
public final void error( final String message )
{
if( m_category.getPriority().isLowerOrEqual( Priority.ERROR ) &&
- LogKit.getGlobalPriority().isLowerOrEqual( Priority.ERROR ) )
+ m_engine.getGlobalPriority().isLowerOrEqual( Priority.ERROR ) )
{
output( Priority.ERROR, message, null );
}
@@ -120,7 +125,7 @@
public final void fatalError( final String message, final Throwable throwable )
{
if( m_category.getPriority().isLowerOrEqual( Priority.FATAL_ERROR ) &&
- LogKit.getGlobalPriority().isLowerOrEqual( Priority.FATAL_ERROR ) )
+ m_engine.getGlobalPriority().isLowerOrEqual( Priority.FATAL_ERROR ) )
{
output( Priority.FATAL_ERROR, message, throwable );
}
@@ -134,7 +139,7 @@
public final void fatalError( final String message )
{
if( m_category.getPriority().isLowerOrEqual( Priority.FATAL_ERROR ) &&
- LogKit.getGlobalPriority().isLowerOrEqual( Priority.FATAL_ERROR ) )
+ m_engine.getGlobalPriority().isLowerOrEqual( Priority.FATAL_ERROR ) )
{
output( Priority.FATAL_ERROR, message, null );
}
@@ -159,7 +164,7 @@
public final void info( final String message )
{
if( m_category.getPriority().isLowerOrEqual( Priority.INFO ) &&
- LogKit.getGlobalPriority().isLowerOrEqual( Priority.INFO ) )
+ m_engine.getGlobalPriority().isLowerOrEqual( Priority.INFO ) )
{
output( Priority.INFO, message, null );
}
@@ -177,7 +182,7 @@
final Throwable throwable )
{
if( m_category.getPriority().isLowerOrEqual( priority ) &&
- LogKit.getGlobalPriority().isLowerOrEqual( priority ) )
+ m_engine.getGlobalPriority().isLowerOrEqual( priority ) )
{
output( priority, message, throwable );
}
@@ -192,7 +197,7 @@
public final void log( final Priority.Enum priority, final String message )
{
if( m_category.getPriority().isLowerOrEqual( priority ) &&
- LogKit.getGlobalPriority().isLowerOrEqual( priority ) )
+ m_engine.getGlobalPriority().isLowerOrEqual( priority ) )
{
output( priority, message, null );
}
@@ -244,7 +249,7 @@
}
else
{
- LogKit.getDefaultLogTarget().processEntry( entry );
+ m_engine.getDefaultLogTarget().processEntry( entry );
}
}
else
@@ -267,7 +272,7 @@
public final void warn( final String message, final Throwable throwable )
{
if( m_category.getPriority().isLowerOrEqual( Priority.WARN ) &&
- LogKit.getGlobalPriority().isLowerOrEqual( Priority.WARN ) )
+ m_engine.getGlobalPriority().isLowerOrEqual( Priority.WARN ) )
{
output( Priority.WARN, message, throwable );
}
@@ -281,7 +286,7 @@
public final void warn( final String message )
{
if( m_category.getPriority().isLowerOrEqual( Priority.WARN ) &&
- LogKit.getGlobalPriority().isLowerOrEqual( Priority.WARN ) )
+ m_engine.getGlobalPriority().isLowerOrEqual( Priority.WARN ) )
{
output( Priority.WARN, message, null );
}
@@ -318,6 +323,6 @@
{
final String categoryName =
m_category.getName() + Category.SEPARATOR + subcategory;
- return LogKit.getLoggerFor( categoryName );
+ return m_engine.getLoggerFor( categoryName );
}
}
1.1 jakarta-avalon-logkit/src/java/org/apache/log/LogEngine.java
Index: LogEngine.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.log;
import java.io.File;
import java.lang.reflect.*;
import java.util.*;
import org.apache.log.output.*;
import org.apache.log.output.DefaultOutputLogTarget;
/**
* This defines the basic interface to a log engine.
* The log engine represents an independent hierarchy.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
*/
public final class LogEngine
{
protected final Hashtable c_loggers = new Hashtable();
protected final Hashtable c_categories = new Hashtable();
protected final Hashtable c_logTargets;
protected Priority.Enum c_priority = Priority.DEBUG;
protected LogTarget c_defaultLogTarget;
public LogEngine()
{
c_logTargets = new Hashtable();
c_defaultLogTarget = new DefaultOutputLogTarget();
c_logTargets.put( "default", c_defaultLogTarget );
}
/**
* Add a named log target to global list.
*
* @param name the name of target
* @param target the target
*/
public void addLogTarget( final String name, final LogTarget target )
{
if( name.equals("default") ) c_defaultLogTarget = target;
c_logTargets.put( name, target );
}
/**
* Retrieve named log target if it exists.
*
* @param name the name of log target
* @return the LogTarget
*/
public LogTarget getLogTarget( final String name )
{
return (LogTarget)c_logTargets.get( name );
}
/**
* Create or update named category and retrieve it.
*
* @param categoryName name of category
* @param priority the priority of categroy
* @return the catgeory
*/
public Category createCategory( final String categoryName, final Priority.Enum
priority )
{
Category category = (Category)c_categories.get( categoryName );
if( null == category )
{
category = new Category( categoryName );
c_categories.put( categoryName, category );
}
category.setPriority( priority );
return category;
}
/**
* Create or update instance of logger.
*
* @param category the category that logger logs about
* @return the Logger
*/
public Logger createLogger( final Category category )
{
return createLogger( category, null );
}
/**
* Create or update instance of logger.
*
* @param logTargets[] the list of log targets logger is to output to.
* @param category the category that logger logs about
* @return the Logger
*/
public Logger createLogger( final Category category, final LogTarget
logTargets[] )
{
final String categoryName = category.getName();
Logger logger = (Logger)c_loggers.get( categoryName );
if( null == logger )
{
final int index = categoryName.lastIndexOf( Category.SEPARATOR );
Logger parent = null;
if( -1 != index )
{
final String parentName = categoryName.substring( 0, index );
parent = getLoggerFor( parentName );
}
logger = new Logger( this, category, logTargets, parent );
c_loggers.put( categoryName, logger );
}
else
{
if( null != logTargets )
{
logger.setLogTargets( logTargets );
}
}
return logger;
}
/**
* Retrieve the default log target.
*
* @return the default LogTarget
*/
public LogTarget getDefaultLogTarget()
{
return c_defaultLogTarget;
}
/**
* Return VM global priority.
*
* @return the priority
*/
public Priority.Enum getGlobalPriority()
{
return c_priority;
}
/**
* Retrieve a logger for named category.
*
* @param category the context
* @return the Logger
*/
public Logger getLoggerFor( final String category )
{
synchronized( c_loggers )
{
Logger logger = (Logger)c_loggers.get( category );
if( null == logger )
{
logger = createLogger( createCategory( category, Priority.DEBUG ) );
}
return logger;
}
}
/**
* Log an error message and exception to stderr.
* TODO: replace this with an error handler
*/
public void log( final String message, final Throwable t )
{
System.err.println( "Error: " + message );
t.printStackTrace();
}
/**
* Logs an error message to stderr.
* TODO: replace this with an error handler
*/
public void log( final String message )
{
System.err.println( "Error: " + message );
}
/**
* Sets the default LogTarget for the default logger.
*/
public void setDefaultLogTarget( final LogTarget defaultLogTarget )
{
addLogTarget( "default", defaultLogTarget );
c_defaultLogTarget = defaultLogTarget;
}
/**
* Set the global priority for this virtual machine. Nothing below
* this level will be logged when using this LogKit.
*/
public void setGlobalPriority( final Priority.Enum priority )
{
c_priority = priority;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]