I would rather use AspectJ than the template here.
See for example General login of method parameters?
/** * */ package at.systemone.examples.aop; import org.apache.log4j.*; import org.aspectj.lang.*; import org.aspectj.lang.reflect.*; public aspect LogMethod { pointcut logMethod(): call (public * AOPLogging.*(..)); before(): logMethod(){ Logger log = Logger.getLogger(assembleFullName(thisJoinPointStaticPart)); Object[] paramValues= thisJoinPoint.getArgs(); String[] paramNames= ((CodeSignature)thisJoinPointStaticPart.getSignature()).getParameterNames(); logParamValues(log, paramNames, paramValues); } after() returning(Object o): logMethod(){ Logger log = Logger.getLogger(assembleFullName(thisJoinPointStaticPart)); // Object returnValue= thisJoinPoint.getArgs()[0]; if (log.isEnabledFor(Level.INFO)){ log.info("returns: '"+o+"'"); } } private void logParamValues(Logger log, String[] paramNames, Object[] paramValues) { if (log.isEnabledFor(Level.INFO)) { // you can even gain perfomance by using the (otherwise unsexy) fast if clause String nv="("; for (int i=0; i<paramValues.length; i++) { nv += paramNames[i]+" = '" + paramValues[i] +(i+1==paramValues.length?"')":"', "); } log.info(nv); } } private String assembleFullName(JoinPoint.StaticPart joinPointStaticPart) { Signature sig = joinPointStaticPart.getSignature(); String sign= sig.getDeclaringType().getName()+"."+sig.getName(); return sign; } }
source share