记录操作的优雅方式?

发布于 2024-10-09 21:18:52 字数 150 浏览 1 评论 0原文

假设您有一个包含一系列操作的应用程序。您将如何编写它以便在触发操作时记录操作?

  1. 使用模板模式。
  2. 使用AOP。
  3. 使用监听器和事件。
  4. 使用以上的组合。
  5. 还有别的事(解释一下)。

Say you have an application with a series of actions. How would you write it such that actions are logged when they are triggered?

  1. Use a Template pattern.
  2. Use AOP.
  3. Use a Listener and Events.
  4. Use a combination of the above.
  5. Something else (explain).

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

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

发布评论

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

评论(4

甜是你 2024-10-16 21:18:52

为了消除冗余,我会投票支持 AOP,但前提是您的项目中有 AOP 的空间。
您可以使用项目中其他地方的方法中的日志记录库进行自定义日志记录,但在拥有许多相似类并希望为所有类记录相似内容的特定组件中,复制/粘贴并确保它们可能会很痛苦每个人都知道如何登录。

编辑:关于其他列举的方法,我认为 AOP 的优点是它不需要您专门为此主题设计代码(模板方法或事件)。当发现横切关注点并如此对待它们时,您通常不会失去任何代码灵活性,而不是仅仅为了使用一致的日志记录而重新设计整个类层次结构。

I'd vote for AOP for the sake of eliminating redundancy, but only if there's room for AOP in your project.
You can have custom logging using a logging library from your methods elsewhere in your project, but in a particular component where you hold many similar classes and want to log similar things for all of them, it can be painful to copy/paste and make sure everyone knows how to log.

EDIT: regarding the other enumerated approaches, I think the advantage of AOP would be that it doesn't require you to design your code (template methods or events) specifically for this topic. You usually don't lose any of the code flexibility when spotting cross-cutting concerns and treating them as such, as opposed to redesigning a whole class hierarchy just to make use of consistent logging.

撑一把青伞 2024-10-16 21:18:52

我认为最好的答案是在一个方面内使用 log4j (或 sli4j,如果这是最新的)。

日志记录是 AOP 的“hello world”。如果您不使用 AOP,那么您就做错了。

I think the best answer is using log4j (or sli4j, if that's the latest) inside an aspect.

Logging is the "hello world" of AOP. If you aren't using AOP, you're doing it wrong.

奢华的一滴泪 2024-10-16 21:18:52

这确实取决于您的具体情况。特别是关于正在跟踪的内容以及应用程序当前的工作方式。如果操作都是具有公共基类的类,并且您关心的只是操作的名称,那么简单添加登录此类将是一个不错的选择。如果您的操作分布在多个代码层中,那么 AOP 或侦听器/事件类型解决方案可能会更好。如果该应用程序是 Web 应用程序与桌面应用程序,或者您最终需要将日志馈送到数据库、Web 服务,或者只是想要文本文件,那么所有这些都会有所不同。

It really would depend on your specific context. Specifically on what was being tracked and how the application currently worked. If the actions are classes that all have a common base class and all you care about is the name of the action, then a simple addition to log in this class would be a great choice. If you have actions spread across several layers of code, then an AOP or Listener/Event type solution might work better. If that application was a web app vs desktop or if you ultimately need the logs feed to a database, webservice, or just want text files all make a difference.

始终不够爱げ你 2024-10-16 21:18:52

如果您只想记录特定操作,最简单的方法可能是使用 日志记录 api,例如 commons-logginglog4j 等在您希望跟踪的代码中添加日志语句。

if you simply want to log particular actions it's probably simplest to use a logging api such as commons-logging or log4j etc add a log statement in the code you wish to track.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文