是否可以在 java (log4j) 中记录方法调用?
是否可以在 log4j Java 中记录任何方法调用?
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是否可以在 log4j Java 中记录任何方法调用?
谢谢!
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
不,如果不编辑调用站点或方法本身就不行。我认为您所追求的是面向方面的编程。例如,看看 AspectJ 。
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.
您可以使用 AOP 和 Java 注释来做到这一点。我建议使用
@Loggable
注释以及来自 jcabi-aspects 的 AspectJ 方面(我是开发人员):在 SLF4J 日志输出中您将看到方法执行时间、其参数及其名称。
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):In SLF4J log output you will see method execution time, its parameters, and its name.
您可以在 %I >Log4j转换模式打印出方法的名称。 但请注意,“生成呼叫者位置信息的速度非常慢,除非执行速度不成问题,否则应避免使用。”
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."如何在 log4j.xml 中使用 (%C.%M.%L) ,如下所示
How about using (%C.%M.%L) as shown below in log4j.xml