Eclipse - create a template for log method arguments

I would like to create a template in eclipse that will put a log with all the arguments of the nested methods and their values. For example, for a method:

foo(int i,String s) 

the template should be evaluated something like this:

 myLogger.log("i="+i+",s=",s); 

Is there something like this?

thanks

+4
source share
2 answers

FYI - I found an eclipse plugin that helps me do what I need. See here: http://code.google.com/p/eclipse-log-param/

+3
source

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; } } 
+1
source

Source: https://habr.com/ru/post/1411065/


All Articles