/*****************************************************************************
 * Copyright (C) PicoContainer Organization. All rights reserved.            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *                                                                           *
 * Original code by Mauro Talevi                                             *
 *****************************************************************************/

package org.picocontainer.gems.monitors;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.monitors.AbstractComponentMonitor;

/**
 * A {@link ComponentMonitor} which writes to a Commons Logging {@link Log} instance. 
 * 
 * @author Mauro Talevi
 * @version $Revision: $
 */
public class CommonsLoggingComponentMonitor extends AbstractComponentMonitor {

    private Log log;
    
    /**
     * Creates a CommonsLoggingComponentMonitor using the 
     * CommonsLoggingComponentMonitor class instance.
     */
    public CommonsLoggingComponentMonitor() {
        this(CommonsLoggingComponentMonitor.class);
    }

    /**
     * Creates a CommonsLoggingComponentMonitor with a given Log instance class.
     * The class name is used to retrieve the Log instance.
     * 
     * @param logClass the class of the Log 
     */
    public CommonsLoggingComponentMonitor(Class logClass) {
        this(logClass.getName());
    }

    /**
     * Creates a CommonsLoggingComponentMonitor with a given Log instance name.
     * It uses the {@link LogFactory LogFactory} to create the Log instance.
     * 
     * @param logName the name of the Log 
     */
    public CommonsLoggingComponentMonitor(String logName) {
        this(LogFactory.getLog(logName));
    }

    /**
     * Creates a CommonsLoggingComponentMonitor with a given Log instance
     *
     * @param log the Log to write to
     */
    public CommonsLoggingComponentMonitor(Log log) {
        this.log = log;
    }
    
    public void instantiating(Constructor constructor) {
        log.debug(format(INSTANTIATING, new Object[]{constructor}));
    }

    public void instantiated(Constructor constructor, long duration) {
        log.debug(format(INSTANTIATED, new Object[]{constructor, new Long(duration)}));
    }

    public void instantiationFailed(Constructor constructor, Exception e) {
        log.warn(format(INSTANTIATION_FAILED, new Object[]{constructor, e.getMessage()}));
    }

    public void invoking(Method method, Object instance) {
        log.debug(format(INVOKING, new Object[]{method, instance}));
    }

    public void invoked(Method method, Object instance, long duration) {
        log.debug(format(INVOKED, new Object[]{method, instance, new Long(duration)}));
    }

    public void invocationFailed(Method method, Object instance, Exception e) {
        log.warn(format(INVOCATION_FAILED, new Object[]{method, instance, e.getMessage()}));
    }

}

@@ -748,16 +748,45 @@
 itors;%0A%0A
+import java.io.Serializable;%0A
 import j
@@ -1038,16 +1038,17 @@
 nitor;%0A%0A
+%0A
 /**%0A * A
@@ -1128,17 +1128,16 @@
 nstance.
- 
 %0A * %0A * 
@@ -1263,16 +1263,40 @@
 Monitor 
+implements Serializable 
 %7B%0A%0A    p
@@ -1311,20 +1311,16 @@
 og log;%0A
-    
 %0A    /**
@@ -1378,24 +1378,16 @@
 ing the 
-%0A     * 
 CommonsL
@@ -1626,23 +1626,16 @@
 e class.
-%0A     *
  The cla
@@ -1661,24 +1661,31 @@
 retrieve the
+%0A     *
  Log instanc
@@ -1730,33 +1730,32 @@
 class of the Log
- 
 %0A     */%0A    pub
@@ -1935,23 +1935,16 @@
 ce name.
-%0A     *
  It uses
@@ -1947,16 +1947,23 @@
 uses the
+%0A     *
  %7B@link 
@@ -2062,17 +2062,16 @@
  the Log
- 
 %0A     */
@@ -2268,16 +2268,17 @@
 e%0A     *
+ 
 %0A     * 
@@ -2399,20 +2399,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub
@@ -2453,24 +2453,64 @@
 structor) %7B%0A
+        if (log.isDebugEnabled()) %7B%0A    
         log.
@@ -2562,24 +2562,34 @@
 tructor%7D));%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -2646,32 +2646,72 @@
 ong duration) %7B%0A
+        if (log.isDebugEnabled()) %7B%0A    
         log.debu
@@ -2774,32 +2774,42 @@
 g(duration)%7D));%0A
+        %7D%0A
     %7D%0A%0A    publi
@@ -2867,32 +2867,71 @@
  Exception e) %7B%0A
+        if (log.isWarnEnabled()) %7B%0A    
         log.warn
@@ -3001,24 +3001,34 @@
 ssage()%7D));%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -3077,24 +3077,64 @@
 instance) %7B%0A
+        if (log.isDebugEnabled()) %7B%0A    
         log.
@@ -3186,24 +3186,34 @@
 nstance%7D));%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -3272,32 +3272,72 @@
 ong duration) %7B%0A
+        if (log.isDebugEnabled()) %7B%0A    
         log.debu
@@ -3404,24 +3404,34 @@
 ration)%7D));%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -3497,32 +3497,71 @@
  Exception e) %7B%0A
+        if (log.isWarnEnabled()) %7B%0A    
         log.warn
@@ -3633,21 +3633,31 @@
 ssage()%7D));%0A
+        %7D%0A
     %7D%0A%0A%7D%0A

Improve logging implementation of CLM.