Java eclipse—为日志方法创建模板';s的论点
我想在eclipse中创建一个模板,该模板将放置一个包含所有封闭方法参数及其值的日志。 例如,对于方法:Java eclipse—为日志方法创建模板';s的论点,java,eclipse,Java,Eclipse,我想在eclipse中创建一个模板,该模板将放置一个包含所有封闭方法参数及其值的日志。 例如,对于方法: foo(int i,String s) 模板的计算结果应类似于: myLogger.log("i="+i+",s=",s); 有这样的东西吗 谢谢我宁愿在这里使用而不是模板。 例如,见 /** * */ 包at.systemone.examples.aop; 导入org.apache.log4j.*; 导入org.aspectj.lang.*; 导入org.aspectj.lang.re
foo(int i,String s)
模板的计算结果应类似于:
myLogger.log("i="+i+",s=",s);
有这样的东西吗
谢谢我宁愿在这里使用而不是模板。例如,见
/**
*
*/
包at.systemone.examples.aop;
导入org.apache.log4j.*;
导入org.aspectj.lang.*;
导入org.aspectj.lang.reflect.*;
公共方面日志方法{
切入点日志方法():调用(public*aoploging.*(..);
before():logMethod(){
Logger log=Logger.getLogger(AssemblyFileName(thisJoinPointStaticPart));
Object[]paramValues=thisJoinPoint.getArgs();
字符串[]paramNames=((CodeSignature)thisJoinPointStaticPart.getSignature()).getParameterNames();
logParamValues(log、ParamName、paramValues);
}
after()返回(对象o):logMethod(){
Logger log=Logger.getLogger(AssemblyFileName(thisJoinPointStaticPart));
//Object returnValue=thisJoinPoint.getArgs()[0];
if(log.isEnabledFor(Level.INFO)){
log.info(“返回:“+o+””);}
}
私有void logParamValues(记录器日志,字符串[]ParamName,对象[]paramValues){
if(log.isenablefor(Level.INFO)){//您甚至可以通过使用(否则为unsexy)fast if子句获得性能
字符串nv=“(”;
对于(inti=0;iFYI),我发现了一个eclipse插件,它可以帮助我完成我所需要的工作。
请参见此处:有趣。+1.我仍然建议您使用aspect,但您的插件确实满足了您的需求。@duduamar我没有直接测试它。我只是想指出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;
}
}