5

I've been working with a company that, in this current project, has to implement a policy of writing lots of trace logging in code (JAVA) that has already been completed for some time.

I am trying to avoid changing every single method just to write a logger.log('desired values') like code line on all of them. it's just too counter-productive.

Is there a generic way to log a method name, the parameters that it received, without changing much of the code? I've been researching annotations and stuff like that but there are a lot of methods with different parameters so i haven't been able to come up with a good solution.

EDIT 1: The project is being developed on eclipse. I'm doing some changes in a portal using Liferay and JBoss.

EDIT 2: I've followed a solution given to me here and used interceptors. The only change i had to do to the existing methods was to add an annotation to them, which was quite acceptable. For more info search in this link: http://docs.oracle.com/javaee/6/tutorial/doc/gkeed.html

  • 3
    You should considere AOP, see [How to use AOP with AspectJ for logging?](http://stackoverflow.com/questions/8839077/how-to-use-aop-with-aspectj-for-logging), [Traditional logging vs AOP logging](http://stackoverflow.com/questions/1555969), [Is using Spring AOP for logging a good idea?](http://stackoverflow.com/questions/2071050) and [Spring: Standard Logging aspect (interceptor)](http://stackoverflow.com/questions/7302090) – Tomasz Nurkiewicz Oct 30 '12 at 10:14
  • Yes, i do. i just edited the post to answear that more clearly for everyone. thanks. – Rafael El Bundas Fernandez Oct 30 '12 at 10:18

4 Answers4

6

The best way is to use AOP and Java annotations. I would recommend to use @Loggable annotation and an AspectJ aspect from jcabi-aspects (I'm a developer):

@Loggable(Loggable.DEBUG)
public String load(URL url) {
  return url.openConnection().getContent();
}
yegor256
  • 102,010
  • 123
  • 446
  • 597
  • 1
    If you go with this approach do not forget to add jcabi build plugin to maven pom. Thank you for the suggestion. It really was something I was looking for. Great library. – Stan Sokolov Jun 11 '18 at 18:04
4

Go to Window->Preferences, select Java->Editor->Templates, create a new template named "logmeth" with Pattern i.e.:

if(logger.isDebug())logger.debug("${exception_variable_name} ${return_type} "+getClass().getName()+"${enclosing_method}(${enclosing_method_arguments})"+String.format("***",${enclosing_method_arguments}));

and press OK.

In java Editor write logmeth and press Strg+space+space and Enter and Eclipse will write i.e.:

if(logger.isDebug())logger.debug("e boolean hasFuture(man, woman)"
            + String.format("***", man, woman));

Eclipse is so cool.

Grim
  • 1,938
  • 10
  • 56
  • 123
2

You can use interceptors http://docs.oracle.com/javaee/6/tutorial/doc/gkeed.html to intercept calls to public methods without any code changes, it is impossible to use this technique with non-public methods though.

Germann Arlington
  • 3,315
  • 2
  • 17
  • 19
1

You should take a look at AOP. It enables you to inject code at runtime and thus add logging before/after each method.

Tomer
  • 17,787
  • 15
  • 78
  • 137
  • I've already read a bit about this. but it didn't seem like it would help me log different methods like: public String method1(String a, int b, boolean c) public int method2(long d) without changing the inner code of each individual method. – Rafael El Bundas Fernandez Oct 30 '12 at 10:20
  • 1
    look here: http://stackoverflow.com/questions/8839077/how-to-use-aop-with-aspectj-for-logging – Tomer Oct 30 '12 at 10:23