KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > util > debug > MethodCallLogAdvice


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

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 /**
26  * Performs writing to DEBUG of incoming arguments and outgoing results for a method call.<br>
27  * If the method invocation throws an exception, then the incoming arguments are
28  * logged to DEBUG as well.<br>
29  * The implementation adds very little overhead to a normal method
30  * call by only building log messages when required.
31  * <p>
32  * The logging is done against the logger retrieved using the names:
33  * <p>
34  * <pre>
35  * org.alfresco.util.debug.MethodCallLogAdvice
36  * AND
37  * targetClassName
38  * targetClassName.methodName
39  * targetClassName.methodName.exception
40  * </pre>
41  * <p>
42  * The following examples show how to control the log levels:
43  * <p>
44  * <pre>
45  * org.alfresco.util.debug.MethodCallLogAdvice=DEBUG # activate method logging
46  * AND
47  * x.y.MyClass=DEBUG # log debug for all method calls on MyClass
48  * x.y.MyClass.doSomething=DEBUG # log debug for all doSomething method calls
49  * x.y.MyClass.doSomething.exception=DEBUG # only log debug for doSomething() upon exception
50  * </pre>
51  * <p>
52  *
53  * @author Derek Hulley
54  */

55 public class MethodCallLogAdvice implements MethodInterceptor
56 {
57     private static final Log logger = LogFactory.getLog(MethodCallLogAdvice.class);
58
59     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc
60     {
61         if (logger.isDebugEnabled())
62         {
63             return invokeWithLogging(invocation);
64         }
65         else
66         {
67             // no logging required
68
return invocation.proceed();
69         }
70     }
71     
72     /**
73      * Only executes logging code if logging is required
74      */

75     private Object JavaDoc invokeWithLogging(MethodInvocation invocation) throws Throwable JavaDoc
76     {
77         String JavaDoc methodName = invocation.getMethod().getName();
78         String JavaDoc className = invocation.getMethod().getDeclaringClass().getName();
79         
80         // execute as normal
81
try
82         {
83             Object JavaDoc ret = invocation.proceed();
84             // logging
85
Log methodLogger = LogFactory.getLog(className + "." + methodName);
86             if (methodLogger.isDebugEnabled())
87             {
88                 // log success
89
StringBuffer JavaDoc sb = getInvocationInfo(className, methodName, invocation.getArguments());
90                 sb.append(" Result: ").append(ret);
91                 methodLogger.debug(sb);
92             }
93             // done
94
return ret;
95         }
96         catch (Throwable JavaDoc e)
97         {
98             Log exceptionLogger = LogFactory.getLog(className + "." + methodName + ".exception");
99             if (exceptionLogger.isDebugEnabled())
100             {
101                 StringBuffer JavaDoc sb = getInvocationInfo(className, methodName, invocation.getArguments());
102                 sb.append(" Failure: ").append(e.getClass().getName()).append(" - ").append(e.getMessage());
103                 exceptionLogger.debug(sb);
104             }
105             // rethrow
106
throw e;
107         }
108     }
109     
110     /**
111      * Return format:
112      * <pre>
113      * Method: className#methodName
114      * Argument: arg0
115      * Argument: arg1
116      * ...
117      * Argument: argN {newline}
118      * </pre>
119      *
120      * @param className
121      * @param methodName
122      * @param args
123      * @return Returns a StringBuffer containing the details of a method call
124      */

125     private StringBuffer JavaDoc getInvocationInfo(String JavaDoc className, String JavaDoc methodName, Object JavaDoc[] args)
126     {
127         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(250);
128         sb.append("\nMethod: ").append(className).append("#").append(methodName).append("\n");
129         sb.append(" Transaction: ").append(AlfrescoTransactionSupport.getTransactionId()).append("\n");
130         for (Object JavaDoc arg : args)
131         {
132             sb.append(" Argument: ").append(arg).append("\n");
133         }
134         return sb;
135     }
136 }
137
Popular Tags