donaldp     01/12/21 18:54:07

  Modified:    src/test/org/apache/log/test InheritanceTestCase.java
               src/java/org/apache/log Logger.java
  Log:
  Maed sure that additivity is transitive - even when you only inherit your 
loggers from your parent.
  
  Submitted By: <[EMAIL PROTECTED]>
  
  Revision  Changes    Path
  1.3       +31 -0     
jakarta-avalon-logkit/src/test/org/apache/log/test/InheritanceTestCase.java
  
  Index: InheritanceTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-logkit/src/test/org/apache/log/test/InheritanceTestCase.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- InheritanceTestCase.java  2001/11/19 12:18:35     1.2
  +++ InheritanceTestCase.java  2001/12/22 02:54:07     1.3
  @@ -293,4 +293,35 @@
           bcd.debug( MSG );
           assertEquals( "Additivity debug output", RMSG, getResult( output ) );
       }
  +
  +    public void testChainedAdditivity()
  +        throws Exception
  +    {
  +        final Hierarchy hierarchy = new Hierarchy();
  +        final ByteArrayOutputStream output1 = new ByteArrayOutputStream();
  +        final ByteArrayOutputStream output2 = new ByteArrayOutputStream();
  +        final StreamTarget target1 = new StreamTarget( output1, FORMATTER );
  +        final StreamTarget target2 = new StreamTarget( output2, FORMATTER );
  +
  +        final LogTarget[] targets1 = new LogTarget[] { target1 };
  +        final LogTarget[] targets2 = new LogTarget[] { target2 };
  +
  +        final Logger b = hierarchy.getLoggerFor( "b" );
  +        final Logger bc = hierarchy.getLoggerFor( "b.c" );
  +        final Logger bcd = hierarchy.getLoggerFor( "b.c.d" );
  +        
  +        b.setLogTargets( targets1 );
  +        bc.setLogTargets( targets2 );
  +        bc.setAdditivity( true );
  +        bcd.setAdditivity( true );
  +
  +        b.debug( MSG );
  +        assertEquals( "Additivity debug output1", RMSG, getResult( output1 ) 
);
  +        bc.debug( MSG );
  +        assertEquals( "Additivity debug output1", RMSG, getResult( output1 ) 
);
  +        assertEquals( "Additivity debug output2", RMSG, getResult( output2 ) 
);
  +        bcd.debug( MSG );
  +        assertEquals( "Additivity debug output1", RMSG, getResult( output1 ) 
);
  +        assertEquals( "Additivity debug output2", RMSG, getResult( output2 ) 
);
  +    }
   }
  
  
  
  1.21      +26 -13    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.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- Logger.java       2001/12/13 10:09:03     1.20
  +++ Logger.java       2001/12/22 02:54:07     1.21
  @@ -560,24 +560,37 @@
               final String message = "LogTarget is null for category '" + 
m_category + "'";
               m_errorHandler.error( message, null, event );
           }
  +        else if( !m_additivity )
  +        {
  +            fireEvent( event, targets );
  +        }
           else
           {
  -            for( int i = 0; i < targets.length; i++ )
  +            //If log targets were not inherited, additivity is true
  +            //then fire an event to local targets 
  +            if( m_logTargetsForceSet )
               {
  -                //No need to clone array as addition of a log-target 
  -                //will result in changin whole array                
  -                targets[ i ].processEvent( event );
  +                fireEvent( event, targets );
               }
   
  -            //If log targets were not inherited, additivity is true
  -            //and we have a parent Logger then send log event to parent
  -            if( m_logTargetsForceSet && m_additivity && null != m_parent )
  +            //if we have a parent Logger then send log event to parent
  +            if( null != m_parent )
               {
                   m_parent.output( event );
               }
           }
       }
   
  +    private final void fireEvent( final LogEvent event, final LogTarget[] 
targets )
  +    {
  +        for( int i = 0; i < targets.length; i++ )
  +        {
  +            //No need to clone array as addition of a log-target 
  +            //will result in changin whole array                
  +            targets[ i ].processEvent( event );
  +        }
  +    }
  +
       /**
        * Update priority of children if any.
        */
  @@ -605,7 +618,7 @@
           {
               m_priorityForceSet = false;
           }
  -        else if( m_priorityForceSet ) 
  +        else if( m_priorityForceSet )
           {
               return;
           }
  @@ -616,27 +629,27 @@
   
       /**
        * Retrieve logtarget array contained in logger.
  -     * This method is provided so that child Loggers can access a 
  +     * This method is provided so that child Loggers can access a
        * copy of  parents LogTargets.
        *
        * @return the array of LogTargets
        */
       private synchronized LogTarget[] safeGetLogTargets()
       {
  -        if( null == m_logTargets ) 
  +        if( null == m_logTargets )
           {
               if( null == m_parent ) return new LogTarget[ 0 ];
               else return m_parent.safeGetLogTargets();
           }
           else
           {
  -            final LogTarget[] logTargets = new LogTarget[ 
m_logTargets.length ];       
  +            final LogTarget[] logTargets = new LogTarget[ 
m_logTargets.length ];
   
               for( int i = 0; i < logTargets.length; i++ )
               {
                   logTargets[ i ] = m_logTargets[ i ];
               }
  -            
  +
               return logTargets;
           }
       }
  @@ -683,7 +696,7 @@
           {
               m_logTargetsForceSet = false;
           }
  -        else if( m_logTargetsForceSet ) 
  +        else if( m_logTargetsForceSet )
           {
               return;
           }
  
  
  

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

Reply via email to