-- 
Cheers,

Peter Donald
------------------------------------------------------
 Mark Twain: "In the real world, the right thing never
happens in the right place at the right time. It is 
the task of journalists and historians to rectify 
this error."
------------------------------------------------------ 
? logkit.txt
? patch.txt
? lib/activation.jar
? lib/jms.jar
? lib/mail_1_2.jar
? src/java/org/apache/log/util/LoggerListener.java
? src/test/org/apache/log/test/LoggerListenerTestCase.java
? src/test/org/apache/log/test/RecordingLoggerListener.java
Index: src/java/org/apache/log/Hierarchy.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-avalon-logkit/src/java/org/apache/log/Hierarchy.java,v
retrieving revision 1.18
diff -u -r1.18 Hierarchy.java
--- src/java/org/apache/log/Hierarchy.java      28 Sep 2002 04:30:04 -0000      1.18
+++ src/java/org/apache/log/Hierarchy.java      3 Feb 2003 10:49:44 -0000
@@ -10,6 +10,7 @@
 import org.apache.log.format.PatternFormatter;
 import org.apache.log.output.io.StreamTarget;
 import org.apache.log.util.DefaultErrorHandler;
+import org.apache.log.util.LoggerListener;
 
 /**
  * This class encapsulates a basic independent log hierarchy.
@@ -30,6 +31,9 @@
     ///Error Handler associated with hierarchy
     private ErrorHandler m_errorHandler;
 
+    ///LoggerListener associated with hierarchy
+    private LoggerListener m_loggerListener;
+
     ///The root logger which contains all Loggers in this hierarchy
     private Logger m_rootLogger;
 
@@ -56,7 +60,9 @@
     public Hierarchy()
     {
         m_errorHandler = new DefaultErrorHandler();
-        m_rootLogger = new Logger( new InnerErrorHandler(), "", null, null );
+        m_rootLogger = new Logger( new InnerErrorHandler(),
+                                   new InnerLoggerListener(),
+                                   "", null, null );
 
         //Setup default output target to print to console
         final PatternFormatter formatter = new PatternFormatter( FORMAT );
@@ -141,6 +147,16 @@
     }
 
     /**
+     * Set the LoggerListener associated with hierarchy.
+     *
+     * @param loggerListener the LoggerListener
+     */
+    public synchronized void setLoggerListener( final LoggerListener loggerListener )
+    {
+        m_loggerListener = loggerListener;
+    }
+
+    /**
      * Retrieve a logger for named category.
      *
      * @param category the context
@@ -176,6 +192,47 @@
         log( message, null );
     }
 
+    /**
+     * Notify logger listener (if any) that a new logger was created.
+     *
+     * @param category the category of new logger
+     * @param logger the logger
+     */
+    private synchronized void notifyLoggerCreated( final String category,
+                                                   final Logger logger )
+    {
+        if( null != m_loggerListener )
+        {
+            m_loggerListener.loggerCreated( category, logger );
+        }
+    }
+
+    /**
+     * Inner class to redirect to hierarchys real LoggerListener if any.
+     * Used so that all the loggers will not have to be updated
+     * when LoggerListener changes.
+     */
+    private class InnerLoggerListener
+        extends LoggerListener
+    {
+        /**
+         * Notify listener that a logger was created.
+         *
+         * @param category the category of logger
+         * @param logger the logger object
+         */
+        public void loggerCreated( final String category,
+                                   final Logger logger )
+        {
+            notifyLoggerCreated( category, logger );
+        }
+    }
+
+    /**
+     * Inner class to redirect to hierarchys real error handler.
+     * Used so that all the loggers will not have to be updated
+     * when error handler changes.
+     */
     private class InnerErrorHandler
         implements ErrorHandler
     {
Index: src/java/org/apache/log/Logger.java
===================================================================
RCS file: /home/cvspublic/jakarta-avalon-logkit/src/java/org/apache/log/Logger.java,v
retrieving revision 1.32
diff -u -r1.32 Logger.java
--- src/java/org/apache/log/Logger.java 12 Dec 2002 01:51:05 -0000      1.32
+++ src/java/org/apache/log/Logger.java 3 Feb 2003 10:49:45 -0000
@@ -7,6 +7,8 @@
  */
 package org.apache.log;
 
+import org.apache.log.util.LoggerListener;
+
 /**
  * The object interacted with by client objects to perform logging.
  *
@@ -22,6 +24,9 @@
     ///The ErrorHandler associated with Logger
     private final ErrorHandler m_errorHandler;
 
+    ///The ErrorHandler associated with Logger
+    private final LoggerListener m_loggerListener;
+
     ///Logger to inherit logtargets and priorities from
     private final Logger m_parent;
 
@@ -59,11 +64,13 @@
      * @param parent the parent logger (used for inheriting from)
      */
     Logger( final ErrorHandler errorHandler,
+            final LoggerListener loggerListener,
             final String category,
             final LogTarget[] logTargets,
             final Logger parent )
     {
         m_errorHandler = errorHandler;
+        m_loggerListener = loggerListener;
         m_category = category;
         m_logTargets = logTargets;
         m_parent = parent;
@@ -426,7 +433,6 @@
         }
 
         final Logger[] children = new Logger[ m_children.length ];
-
         for( int i = 0; i < children.length; i++ )
         {
             children[ i ] = m_children[ i ];
@@ -498,12 +504,14 @@
         }
 
         //Create new logger
-        final Logger child = new Logger( m_errorHandler, category, null, this );
+        final Logger child =
+            new Logger( m_errorHandler, m_loggerListener, category, null, this );
 
         if( m_additivity )
         {
             child.setAdditivity( true );
         }
+        m_loggerListener.loggerCreated( child.m_category, child );
 
         //Add new logger to child list
         if( null == m_children )
Index: src/test/org/apache/log/test/InheritanceTestCase.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-avalon-logkit/src/test/org/apache/log/test/InheritanceTestCase.java,v
retrieving revision 1.4
diff -u -r1.4 InheritanceTestCase.java
--- src/test/org/apache/log/test/InheritanceTestCase.java       27 Mar 2002 22:07:58 
-0000      1.4
+++ src/test/org/apache/log/test/InheritanceTestCase.java       3 Feb 2003 10:49:47 
+-0000
@@ -24,8 +24,6 @@
 public final class InheritanceTestCase
     extends TestCase
 {
-    private final static String POSITIVE = "+1 Positive - yay - lets do the chicken 
dance.";
-
     private final static String PATTERN = "%{priority}-%{message}";
     private final static PatternFormatter FORMATTER = new PatternFormatter( PATTERN );
 
Index: src/test/org/apache/log/util/test/UtilTestCase.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-avalon-logkit/src/test/org/apache/log/util/test/UtilTestCase.java,v
retrieving revision 1.4
diff -u -r1.4 UtilTestCase.java
--- src/test/org/apache/log/util/test/UtilTestCase.java 27 Mar 2002 22:07:58 -0000     
 1.4
+++ src/test/org/apache/log/util/test/UtilTestCase.java 3 Feb 2003 10:49:47 -0000
@@ -30,7 +30,6 @@
 
     private final static String MSG = "No soup for you!";
     private final static String RMSG = MSG;
-    private final static String METHOD_RESULT = UtilTestCase.class.getName() + ".";
 
     public UtilTestCase( final String name )
     {
@@ -48,6 +47,7 @@
         throws Exception
     {
         /*
+        final String METHOD_RESULT = UtilTestCase.class.getName() + ".";
         final ByteArrayOutputStream output = new ByteArrayOutputStream();
         final StreamTarget target = new StreamTarget( output, METHOD_FORMATTER );
         final Hierarchy hierarchy = new Hierarchy();
/*
 * 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.util;

import org.apache.log.Logger;

/**
 * The LoggerListener class is used to notify listeners
 * when a new Logger object is created. Loggers are created
 * when a client requests a new Logger via {@link Logger#getChildLogger}.
 *
 * @author <a href="mailto:[EMAIL PROTECTED]";>Peter Donald</a>
 */
public abstract class LoggerListener
{
    /**
     * Notify listener that Logger was created.
     *
     * @param category the error message
     * @param logger the logger that was created
     */
    public abstract void loggerCreated( String category, Logger logger );
}
/*
 * 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.test;

import junit.framework.TestCase;
import org.apache.log.Hierarchy;
import org.apache.log.Logger;

/**
 * Test suite for logger listener features of Logger.
 *
 * @author <a href="mailto:[EMAIL PROTECTED]";>Peter Donald</a>
 */
public final class LoggerListenerTestCase
    extends TestCase
{
    public LoggerListenerTestCase( final String name )
    {
        super( name );
    }

    public void testPriorityInheritance()
        throws Exception
    {
        final RecordingLoggerListener listener = new RecordingLoggerListener();
        final Hierarchy hierarchy = new Hierarchy();
        hierarchy.setLoggerListener( listener );

        final Logger root = hierarchy.getRootLogger();
        final Logger l1 = root.getChildLogger( "logger1" );
        final Logger l2 = root.getChildLogger( "logger2" );
        final Logger l3 = root.getChildLogger( "logger1.logger3" );
        final Logger l4 = root.getChildLogger( "logger5.logger4" );
        final Logger l5 = root.getChildLogger( "logger5" );

        final Logger[] loggers = listener.getLoggers();
        assertEquals( "Logger Count", 5, loggers.length );
        assertEquals( "Logger[0]", l1, loggers[ 0 ] );
        assertEquals( "Logger[1]", l2, loggers[ 1 ] );
        assertEquals( "Logger[2]", l3, loggers[ 2 ] );
        assertEquals( "Logger[3]", l5, loggers[ 3 ] );
        assertEquals( "Logger[4]", l4, loggers[ 4 ] );
    }
}
/*
 * 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.log.test;

import java.util.ArrayList;
import java.util.List;
import org.apache.log.Logger;
import org.apache.log.util.LoggerListener;

/**
 * A logger listener that records the log messages it receives.
 *
 * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
 * @version $Revision:$ $Date:$
 */
class RecordingLoggerListener
    extends LoggerListener
{
    //The listeners that have been created
    private final List m_loggers = new ArrayList();

    public void loggerCreated( final String category,
                               final Logger logger )
    {
        m_loggers.add( logger );
    }

    public Logger[] getLoggers()
    {
        return (Logger[])m_loggers.toArray( new Logger[ m_loggers.size() ] );
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to