Is it possible to log a method call in java (log4j

2019-02-26 16:54发布

Is it possible to log any method call in log4j Java?

Thanks!

3条回答
劳资没心,怎么记你
2楼-- · 2019-02-26 17:28

You can use %I in your Log4j conversion pattern to print out the name of the method. BUT be warned that "generating caller location information is extremely slow and should be avoided unless execution speed is not an issue."

查看更多
男人必须洒脱
3楼-- · 2019-02-26 17:28

You can do it with 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();
}

In SLF4J log output you will see method execution time, its parameters, and its name.

查看更多
Deceive 欺骗
4楼-- · 2019-02-26 17:31

No, not without editing either the call-sites or the method itself. I think what you're after is something towards aspect oriented programming. Have a look at AspectJ for example.

查看更多
登录 后发表回答