是否可以在 java (log4j) 中记录方法调用?

发布于 2024-10-06 13:05:17 字数 46 浏览 0 评论 0 原文

是否可以在 log4j Java 中记录任何方法调用?

谢谢!

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

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

琉璃梦幻 2024-10-13 13:05:17

不,如果不编辑调用站点或方法本身就不行。我认为您所追求的是面向方面的编程。例如,看看 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.

沉溺在你眼里的海 2024-10-13 13:05:17

您可以使用 AOP 和 Java 注释来做到这一点。我建议使用 @Loggable 注释以及来自 jcabi-aspects 的 AspectJ 方面(我是开发人员):

@Loggable(Loggable.DEBUG)
public String load(URL url) {
  return url.openConnection().getContent();
}

在 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):

@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.

私野 2024-10-13 13:05:17

您可以在 %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."

多像笑话 2024-10-13 13:05:17

如何在 log4j.xml 中使用 (%C.%M.%L) ,如下所示

<layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern"
        value="%d - MyApplication - %-4p - (%C.%M.%L) -- %m%n" />
</layout>

How about using (%C.%M.%L) as shown below in log4j.xml

<layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern"
        value="%d - MyApplication - %-4p - (%C.%M.%L) -- %m%n" />
</layout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文