1 17 package org.alfresco.util.debug; 18 19 import org.alfresco.repo.transaction.AlfrescoTransactionSupport; 20 import org.aopalliance.intercept.MethodInterceptor; 21 import org.aopalliance.intercept.MethodInvocation; 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 55 public class MethodCallLogAdvice implements MethodInterceptor 56 { 57 private static final Log logger = LogFactory.getLog(MethodCallLogAdvice.class); 58 59 public Object invoke(MethodInvocation invocation) throws Throwable 60 { 61 if (logger.isDebugEnabled()) 62 { 63 return invokeWithLogging(invocation); 64 } 65 else 66 { 67 return invocation.proceed(); 69 } 70 } 71 72 75 private Object invokeWithLogging(MethodInvocation invocation) throws Throwable 76 { 77 String methodName = invocation.getMethod().getName(); 78 String className = invocation.getMethod().getDeclaringClass().getName(); 79 80 try 82 { 83 Object ret = invocation.proceed(); 84 Log methodLogger = LogFactory.getLog(className + "." + methodName); 86 if (methodLogger.isDebugEnabled()) 87 { 88 StringBuffer sb = getInvocationInfo(className, methodName, invocation.getArguments()); 90 sb.append(" Result: ").append(ret); 91 methodLogger.debug(sb); 92 } 93 return ret; 95 } 96 catch (Throwable e) 97 { 98 Log exceptionLogger = LogFactory.getLog(className + "." + methodName + ".exception"); 99 if (exceptionLogger.isDebugEnabled()) 100 { 101 StringBuffer sb = getInvocationInfo(className, methodName, invocation.getArguments()); 102 sb.append(" Failure: ").append(e.getClass().getName()).append(" - ").append(e.getMessage()); 103 exceptionLogger.debug(sb); 104 } 105 throw e; 107 } 108 } 109 110 125 private StringBuffer getInvocationInfo(String className, String methodName, Object [] args) 126 { 127 StringBuffer sb = new StringBuffer (250); 128 sb.append("\nMethod: ").append(className).append("#").append(methodName).append("\n"); 129 sb.append(" Transaction: ").append(AlfrescoTransactionSupport.getTransactionId()).append("\n"); 130 for (Object arg : args) 131 { 132 sb.append(" Argument: ").append(arg).append("\n"); 133 } 134 return sb; 135 } 136 } 137 | Popular Tags |