avalon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Leif Mortenson <l...@tanukisoftware.com>
Subject Re: Question + Patch about use of LogKitLoggerManager with a log prefix
Date Wed, 07 Aug 2002 03:37:11 GMT
No comments on this and I need to proceed, so I'll home it doesn't cause 
any pain and move forward,

Cheers,
Leif

Leif Mortenson wrote:

> While looking into logging issues with fortress, I came across what I 
> think is a problem in
> LogKitLoggerManager.
>
> If I create a LogKitLoggerManager specifying a prefix of "fortress" 
> (As is done in fortress)
> the categories specified in the configuration file for the 
> LoggerManager will not be mapped
> to Loggers obtained by getLoggerForCategory.
>
> The problem is that if I specify a category in the conf file called 
> "inst", it will be remapped
> to a category called "fortress.inst" in the setupLoggers method.  That 
> seems correct.
>
> The problem is that when I call getLoggerForCategory( "inst" ), I am 
> given a logger named
> "inst"  I should be given a logger named "fortress.inst".  Shouldn't I?
>
> I came up with a patch to fix this.  However, as this has the 
> potential to affect a lot of people,
> I wanted to make sure that those who designed this class agree with 
> the patch before I apply
> it.
>
> I also made a change so that if a category "" is specified, the 
> resulting logger will be named
> "fortress" rather than "fortress."
>
> Specifying a prefix seems to lead to some pretty long names.  But if 
> this is what was intended
> then that is fine.
>
> Examples:
> prefix=null, category=null => ""
> prefix=null, category="" => ""
> prefix="", category="" => ""
> prefix=null, category="logkit" => "logkit"
> prefix=null, category="inst" => "inst"
> prefix="fortress", category=null => "fortress"
> prefix="fortress", category="" => "fortress"
> prefix="fortress", category="logkit" => "fortress.logkit"
> prefix="fortress", category="inst" => "fortress.inst"
>
> Cheers,
> Leif
>
> Patch: ( I also attached the new file to make it easier to try out.)
> diff -r1.3 LogKitLoggerManager.java
> 109c109,111
> <         final Logger logger = (Logger)m_loggers.get( categoryName );
> ---
> >         final String fullCategoryName = getFullCategoryName( 
> m_prefix, categoryName );
> >        >         final Logger logger = (Logger)m_loggers.get( 
> fullCategoryName );
> 115c117
> <                 m_defaultLogger.debug( "Logger for category " + 
> categoryName + " returned" );
> ---
> >                 m_defaultLogger.debug( "Logger for category " + 
> fullCategoryName + " returned" );
> 122,123c124,125
> <             m_defaultLogger.debug( "Logger for category " + 
> categoryName
> <                                    + " not defined in configuration. 
> New Logger created and returned" );
> ---
> >             m_defaultLogger.debug( "Logger for category " + 
> fullCategoryName + " not defined in "
> >                                     + "configuration. New Logger 
> created and returned" );
> 126c128
> <         return new LogKitLogger( m_hierarchy.getLoggerFor( 
> categoryName ) );
> ---
> >         return new LogKitLogger( m_hierarchy.getLoggerFor( 
> fullCategoryName ) );
> 226a229,261
> >    >     /**
> >      * Generates a full category name given a prefix and category.  
> Either may be
> >      *  null.
> >      *
> >      * @param prefix Prefix or parent category.
> >      * @param category Child category name.
> >      */
> >     private final String getFullCategoryName( String prefix, String 
> category )
> >     {
> >         if( ( null == prefix ) || ( prefix.length() == 0 )  )
> >         {
> >             if ( category == null )
> >             {
> >                 return "";
> >             }
> >             else
> >             {
> >                 return category;
> >             }
> >         }
> >         else
> >         {
> >             if( ( null == category ) || ( category.length() == 0 ) )
> >             {
> >                 return prefix;
> >             }
> >             else
> >             {
> >                 return prefix + 
> org.apache.log.Logger.CATEGORY_SEPARATOR + category;
> >             }
> >         }
> >     }
> 258,266c293
> <             final String fullCategory;
> <             if( null == parentCategory )
> <             {
> <                 fullCategory = category;
> <             }
> <             else
> <             {
> <                 fullCategory = parentCategory + 
> org.apache.log.Logger.CATEGORY_SEPARATOR + category;
> <             }
> ---
> >             final String fullCategory = getFullCategoryName( 
> parentCategory, category );
>
>
>------------------------------------------------------------------------
>
>/*
> * 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.txt file.
> */
>package org.apache.avalon.excalibur.logger;
>
>import java.util.HashMap;
>import java.util.Map;
>import org.apache.avalon.framework.configuration.Configurable;
>import org.apache.avalon.framework.configuration.Configuration;
>import org.apache.avalon.framework.configuration.ConfigurationException;
>import org.apache.avalon.framework.context.Context;
>import org.apache.avalon.framework.context.ContextException;
>import org.apache.avalon.framework.context.Contextualizable;
>import org.apache.avalon.framework.logger.LogEnabled;
>import org.apache.avalon.framework.logger.LogKitLogger;
>import org.apache.avalon.framework.logger.Logger;
>import org.apache.log.Hierarchy;
>import org.apache.log.LogTarget;
>import org.apache.log.Priority;
>
>/**
> * LogKitLoggerManager implementation.  It populates the LoggerManager
> * from a configuration file.
> *
> * @author <a href="mailto:giacomo@apache.org">Giacomo Pati</a>
> * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
> * @version CVS $Revision: 1.3 $ $Date: 2002/07/09 11:18:38 $
> * @since 4.0
> */
>public class LogKitLoggerManager
>    implements LoggerManager, Contextualizable, Configurable
>{
>    /** Map for name to logger mapping */
>    final private Map m_loggers = new HashMap();
>
>    /** The context object */
>    private Context m_context;
>
>    /** The hierarchy private to LogKitManager */
>    private Hierarchy m_hierarchy;
>
>    /** The root logger to configure */
>    private String m_prefix;
>
>    /** The default logger used for this system */
>    final private Logger m_defaultLogger;
>
>    /**
>     * Creates a new <code>DefaultLogKitManager</code>. It will use a new
<code>Hierarchy</code>.
>     */
>    public LogKitLoggerManager()
>    {
>        this( new Hierarchy() );
>    }
>
>    /**
>     * Creates a new <code>DefaultLogKitManager</code> with an existing <code>Hierarchy</code>.
>     */
>    public LogKitLoggerManager( final Hierarchy hierarchy )
>    {
>        this( null, hierarchy );
>    }
>
>    /**
>     * Creates a new <code>DefaultLogKitManager</code> using
>     * specified logger name as root logger.
>     */
>    public LogKitLoggerManager( final String prefix )
>    {
>        this( prefix, new Hierarchy() );
>    }
>
>    /**
>     * Creates a new <code>DefaultLogKitManager</code> with an existing <code>Hierarchy</code>
using
>     * specified logger name as root logger.
>     */
>    public LogKitLoggerManager( final String prefix, final Hierarchy hierarchy )
>    {
>        this( prefix, hierarchy,
>              new LogKitLogger( hierarchy.getLoggerFor( "" ) ) );
>    }
>
>    /**
>     * Creates a new <code>DefaultLogKitManager</code> with an existing <code>Hierarchy</code>
using
>     * specified logger name as root logger.
>     */
>    public LogKitLoggerManager( final String prefix, final Hierarchy hierarchy, final
Logger defaultLogger )
>    {
>        m_prefix = prefix;
>        m_hierarchy = hierarchy;
>        m_defaultLogger = defaultLogger;
>    }
>
>    /**
>     * Retrieves a Logger from a category name. Usually
>     * the category name refers to a configuration attribute name.  If
>     * this LogKitManager does not have the match the default Logger will
>     * be returned and a warning is issued.
>     *
>     * @param categoryName  The category name of a configured Logger.
>     * @return the Logger.
>     */
>    public final Logger getLoggerForCategory( final String categoryName )
>    {
>        final String fullCategoryName = getFullCategoryName( m_prefix, categoryName );
>        
>        final Logger logger = (Logger)m_loggers.get( fullCategoryName );
>
>        if( null != logger )
>        {
>            if( m_defaultLogger.isDebugEnabled() )
>            {
>                m_defaultLogger.debug( "Logger for category " + fullCategoryName + " returned"
);
>            }
>            return logger;
>        }
>
>        if( m_defaultLogger.isDebugEnabled() )
>        {
>            m_defaultLogger.debug( "Logger for category " + fullCategoryName + " not defined
in "
>                                    + "configuration. New Logger created and returned"
);
>        }
>
>        return new LogKitLogger( m_hierarchy.getLoggerFor( fullCategoryName ) );
>    }
>
>    public final Logger getDefaultLogger()
>    {
>        return m_defaultLogger;
>    }
>
>    /**
>     * Reads a context object that will be supplied to the log target factory manager.
>     *
>     * @param context The context object.
>     * @throws ContextException if the context is malformed
>     */
>    public final void contextualize( final Context context )
>        throws ContextException
>    {
>        m_context = context;
>    }
>
>    /**
>     * Reads a configuration object and creates the category mapping.
>     *
>     * @param configuration  The configuration object.
>     * @throws ConfigurationException if the configuration is malformed
>     */
>    public final void configure( final Configuration configuration )
>        throws ConfigurationException
>    {
>        final Configuration factories = configuration.getChild( "factories" );
>        final LogTargetFactoryManager targetFactoryManager = setupTargetFactoryManager(
factories );
>
>        final Configuration targets = configuration.getChild( "targets" );
>        final LogTargetManager targetManager = setupTargetManager( targets, targetFactoryManager
);
>
>        final Configuration categories = configuration.getChild( "categories" );
>        final Configuration[] category = categories.getChildren( "category" );
>        setupLoggers( targetManager, m_prefix, category );
>    }
>
>    /**
>     * Setup a LogTargetFactoryManager
>     *
>     * @param configuration  The configuration object.
>     * @throws ConfigurationException if the configuration is malformed
>     */
>    private final LogTargetFactoryManager setupTargetFactoryManager( final Configuration
configuration )
>        throws ConfigurationException
>    {
>        final DefaultLogTargetFactoryManager targetFactoryManager = new DefaultLogTargetFactoryManager();
>        if( targetFactoryManager instanceof LogEnabled )
>        {
>            targetFactoryManager.enableLogging( m_defaultLogger );
>        }
>
>        if( targetFactoryManager instanceof Contextualizable )
>        {
>            try
>            {
>                targetFactoryManager.contextualize( m_context );
>            }
>            catch( final ContextException ce )
>            {
>                throw new ConfigurationException( "cannot contextualize default factory
manager", ce );
>            }
>        }
>
>        targetFactoryManager.configure( configuration );
>
>        return targetFactoryManager;
>    }
>
>    /**
>     * Setup a LogTargetManager
>     *
>     * @param configuration  The configuration object.
>     * @throws ConfigurationException if the configuration is malformed
>     */
>    private final LogTargetManager setupTargetManager( final Configuration configuration,
>                                                       final LogTargetFactoryManager targetFactoryManager
)
>        throws ConfigurationException
>    {
>        final DefaultLogTargetManager targetManager = new DefaultLogTargetManager();
>
>        if( targetManager instanceof LogEnabled )
>        {
>            targetManager.enableLogging( m_defaultLogger );
>        }
>
>        if( targetManager instanceof LogTargetFactoryManageable )
>        {
>            targetManager.setLogTargetFactoryManager( targetFactoryManager );
>        }
>
>        if( targetManager instanceof Configurable )
>        {
>            targetManager.configure( configuration );
>        }
>
>        return targetManager;
>    }
>    
>    /**
>     * Generates a full category name given a prefix and category.  Either may be
>     *  null.
>     *
>     * @param prefix Prefix or parent category.
>     * @param category Child category name.
>     */
>    private final String getFullCategoryName( String prefix, String category )
>    {
>        if( ( null == prefix ) || ( prefix.length() == 0 )  )
>        {
>            if ( category == null )
>            {
>                return "";
>            }
>            else
>            {
>                return category;
>            }
>        }
>        else
>        {
>            if( ( null == category ) || ( category.length() == 0 ) )
>            {
>                return prefix;
>            }
>            else
>            {
>                return prefix + org.apache.log.Logger.CATEGORY_SEPARATOR + category;
>            }
>        }
>    }
>
>    /**
>     * Setup Loggers
>     *
>     * @param categories []  The array object of configurations for categories.
>     * @throws ConfigurationException if the configuration is malformed
>     */
>    private final void setupLoggers( final LogTargetManager targetManager,
>                                     final String parentCategory,
>                                     final Configuration[] categories )
>        throws ConfigurationException
>    {
>        for( int i = 0; i < categories.length; i++ )
>        {
>            final String category = categories[ i ].getAttribute( "name" );
>            final String loglevel = categories[ i ].getAttribute( "log-level" ).toUpperCase();
>
>            final Configuration[] targets = categories[ i ].getChildren( "log-target"
);
>            final LogTarget[] logTargets = new LogTarget[ targets.length ];
>            for( int j = 0; j < targets.length; j++ )
>            {
>                final String id = targets[ j ].getAttribute( "id-ref" );
>                logTargets[ j ] = targetManager.getLogTarget( id );
>            }
>
>            if( "".equals( category ) && logTargets.length > 0 )
>            {
>                m_hierarchy.setDefaultPriority( Priority.getPriorityForName( loglevel
) );
>                m_hierarchy.setDefaultLogTargets( logTargets );
>            }
>
>            final String fullCategory = getFullCategoryName( parentCategory, category
);
>
>            final org.apache.log.Logger logger = m_hierarchy.getLoggerFor( fullCategory
);
>            m_loggers.put( fullCategory, new LogKitLogger( logger ) );
>            if( m_defaultLogger.isDebugEnabled() )
>            {
>                m_defaultLogger.debug( "added logger for category " + fullCategory );
>            }
>            logger.setPriority( Priority.getPriorityForName( loglevel ) );
>            logger.setLogTargets( logTargets );
>
>            final Configuration[] subCategories = categories[ i ].getChildren( "category"
);
>            if( null != subCategories )
>            {
>                setupLoggers( targetManager, fullCategory, subCategories );
>            }
>        }
>    }
>}
>
>  
>
>------------------------------------------------------------------------
>
>--
>To unsubscribe, e-mail:   <mailto:avalon-dev-unsubscribe@jakarta.apache.org>
>For additional commands, e-mail: <mailto:avalon-dev-help@jakarta.apache.org>
>



--
To unsubscribe, e-mail:   <mailto:avalon-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-dev-help@jakarta.apache.org>


Mime
View raw message