Return-Path: Delivered-To: apmail-jakarta-avalon-dev-archive@apache.org Received: (qmail 93815 invoked from network); 3 Feb 2003 10:51:26 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 3 Feb 2003 10:51:26 -0000 Received: (qmail 6641 invoked by uid 97); 3 Feb 2003 10:53:07 -0000 Delivered-To: qmlist-jakarta-archive-avalon-dev@nagoya.betaversion.org Received: (qmail 6629 invoked from network); 3 Feb 2003 10:53:06 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 3 Feb 2003 10:53:06 -0000 Received: (qmail 93558 invoked by uid 500); 3 Feb 2003 10:51:23 -0000 Mailing-List: contact avalon-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Avalon Developers List" Reply-To: "Avalon Developers List" Delivered-To: mailing list avalon-dev@jakarta.apache.org Received: (qmail 93545 invoked from network); 3 Feb 2003 10:51:23 -0000 Received: from mail021.syd.optusnet.com.au (210.49.20.161) by daedalus.apache.org with SMTP; 3 Feb 2003 10:51:23 -0000 Received: from brainstem.dyndns.org (c17243.thoms1.vic.optusnet.com.au [210.49.246.103]) by mail021.syd.optusnet.com.au (8.11.1/8.11.1) with ESMTP id h13ApUv32616 for ; Mon, 3 Feb 2003 21:51:34 +1100 From: Peter Donald To: avalon-dev@jakarta.apache.org Subject: [patch] Add listeners for logger creation Date: Mon, 3 Feb 2003 22:06:13 +1100 User-Agent: KMail/1.4.2 X-Notice: Duplication and redistribution prohibited without consent of the author. X-Copyright: (C) 2002 Peter Donald. X-Wisdom: A right is not what someone gives you; it's what no one can take from you. MIME-Version: 1.0 Content-Type: Multipart/Mixed; boundary="------------Boundary-00=_D6CQT5258U66QFRB0FWE" Message-Id: <200302032206.13720.peter@realityforge.org> X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N --------------Boundary-00=_D6CQT5258U66QFRB0FWE Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable --=20 Cheers, Peter Donald ------------------------------------------------------ Mark Twain: "In the real world, the right thing never happens in the right place at the right time. It is=20 the task of journalists and historians to rectify=20 this error." ------------------------------------------------------=20 --------------Boundary-00=_D6CQT5258U66QFRB0FWE Content-Type: text/plain; charset="us-ascii"; name="patch.txt" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="patch.txt" ? 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(); --------------Boundary-00=_D6CQT5258U66QFRB0FWE Content-Type: text/x-java; charset="us-ascii"; name="LoggerListener.java" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="LoggerListener.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.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 Peter Donald */ 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 ); } --------------Boundary-00=_D6CQT5258U66QFRB0FWE Content-Type: text/x-java; charset="us-ascii"; name="LoggerListenerTestCase.java" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="LoggerListenerTestCase.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.test; import junit.framework.TestCase; import org.apache.log.Hierarchy; import org.apache.log.Logger; /** * Test suite for logger listener features of Logger. * * @author Peter Donald */ 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 ] ); } } --------------Boundary-00=_D6CQT5258U66QFRB0FWE Content-Type: text/x-java; charset="us-ascii"; name="RecordingLoggerListener.java" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="RecordingLoggerListener.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.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 Peter Donald * @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() ] ); } } --------------Boundary-00=_D6CQT5258U66QFRB0FWE Content-Type: text/plain; charset=us-ascii --------------------------------------------------------------------- To unsubscribe, e-mail: avalon-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: avalon-dev-help@jakarta.apache.org --------------Boundary-00=_D6CQT5258U66QFRB0FWE--