我想在 Eclipse 中创建一个模板,该模板将包含所有封闭方法的参数及其值的日志。例如,对于方法:
foo(int i,String s)
模板应评估为:
myLogger.log("i="+i+",s=",s);
有这样的事情吗?
谢谢
仅供参考 - 我找到了一个 eclipse 插件,可以帮助我完成我所需要的。见这里:http ://code.google.com/p/eclipse-log-param/
我宁愿在这里使用AspectJ而不是模板。
参见例如方法参数的通用登录?
/**
*
*/
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;
}
}