Logging a methods's name and parameters

前端 未结 4 1471
星月不相逢
星月不相逢 2021-02-10 15:12

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 so

相关标签:
4条回答
  • 2021-02-10 15:20

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

    0 讨论(0)
  • 2021-02-10 15:27

    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.

    0 讨论(0)
  • 2021-02-10 15:37

    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.

    0 讨论(0)
  • 2021-02-10 15:37

    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();
    }
    
    0 讨论(0)
提交回复
热议问题