java 使用 Spring AOP 进行日志记录是个好主意吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2071050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:15:45  来源:igfitidea点击:

Is using Spring AOP for logging a good idea?

javaspringloggingaopspring-aop

提问by Omar Kooheji

I'm reading up on Spring at the moment and one of the examples used for a use of AOP is logging the start and end of method calls.

我目前正在阅读 Spring,其中一个用于使用 AOP 的示例是记录方法调用的开始和结束。

I've also read that using AOP can impact performance.

我还读到使用 AOP 会影响性能。

Is using Spring AOP a good idea for this type of logging? My understanding is that Spring uses Dynamic AOP would it be be better to use Static AOP (Like AspectJ) for this type of AOP.

对于这种类型的日志记录,使用 Spring AOP 是一个好主意吗?我的理解是 Spring 使用动态 AOP,对于这种类型的 AOP,使用静态 AOP(如 AspectJ)会更好。

Curently the coding policy of the company I work for requires a ridiculous amount of logging and i want to reduce the ammount of logging code I have to write and improve the readability of my code.

目前,我工作的公司的编码政策需要大量的日志记录,我想减少我必须编写的日志记录代码的数量并提高代码的可读性。

Am I barking up the wrong tree?

我是不是叫错了树?

采纳答案by Bozho

Read this blog-postabout your performance concerns.

阅读这篇关于您的性能问题的博客文章

The way to think of AOP is to put the provided functional benefits in the first place. If automated logging is your requirement and AOP fits it - go for it.

考虑 AOP 的方式是将提供的功能优势放在首位。如果自动日志记录是您的要求并且 AOP 适合它 - 去吧。

That said, load-time weaving is, of course, preferred if fine-grained logging is required.

也就是说,如果需要细粒度的日志记录,加载时间编织当然是首选。

回答by Rorick

I used Spring AOP for implementing logging so I share my observations:

我使用 Spring AOP 来实现日志记录,所以我分享了我的观察:

  • Performance impact is not sufficient, it is less than impact of logging itself
  • Having aspects configured in Spring configuration, you are able to completely disable logging code if necessary
  • Debugging becomes more problematic as stack traces become rather longer
  • Such decision sufficiently affects design. It is not only that you get a bunch of interfaces and aspect classes, but you production classes must be very "thin". Don't forget, you are unable to intercept calls to non-public methods. Self-calls (even to public methods) also cannot be intercepted (as you working with naked thishandle instead of handle wrapped by AOP) and thus cannot be logged. So all logging can happen only on interface boundaries. (This concerns using Proxy-based aspect weaving, there's an option of runtime subclassing with cglib, but I didn't use it)
  • Writing pointcuts can be very tricky. IntelliJ Idea helps greatly determining which methods are to be adviced by pointcut.
  • Generally, I liked this approach and think it is worth using, but it appeared far more complicated than I expected
  • 性能影响不够,小于日志本身的影响
  • 在 Spring 配置中配置方面后,您可以在必要时完全禁用日志记录代码
  • 随着堆栈跟踪变得更长,调试变得更成问题
  • 这样的决定足以影响设计。不仅您获得了一堆接口和方面类,而且您的生产类必须非常“瘦”。不要忘记,您无法拦截对非公共方法的调用。自调用(甚至是公共方法)也不能被拦截(因为你使用裸this句柄而不是 AOP 包裹的句柄),因此不能被记录。因此,所有日志记录只能发生在接口边界上。(这涉及使用基于代理的方面编织,有一个使用 cglib 进行运行时子类化的选项,但我没有使用它)
  • 编写切入点可能非常棘手。IntelliJ Idea 有助于极大地确定切入点建议哪些方法。
  • 总的来说,我喜欢这种方法并认为它值得使用,但它看起来比我预期的要复杂得多

回答by lisak

I did it similarly to the way described in this blog post. It's the best I found and it also has a samplethat shows nicely the difference between using and not using AOP.

我的做法与这篇博文中描述的方法类似。这是我发现的最好的,它还有一个示例,它很好地展示了使用和不使用 AOP 之间的区别。

Imho it isn't worth it, unless you're doing something fancier then logging, like error management with persistence. If you have a good exception hierarchy (domain, system) and properly set logging boundaries, you wont reduce much logging code.

恕我直言,这是不值得的,除非您正在做一些比日志记录更有趣的事情,例如具有持久性的错误管理。如果您有良好的异常层次结构(域、系统)并正确设置日志记录边界,则不会减少太多日志记录代码。