集中应用程序日志的优缺点是什么?
What is the pros and cons of centralizing applications logs?
我想了解使用 AOP(例如 使用 AspectJ)将所有应用程序日志集中到单独文件中的优缺点。
Logging is know for being a cross-cutting concern。但是,我从未见过有人将所有日志集中到一个或一堆文件中。因此,我想知道为什么。
这样做的利弊是什么?
I am looking to understand the pros and cons of centralizing all
applications logs into separate files with AOP (e.g. with AspectJ).
我将交替使用术语 AOP 和 AspectJ。尽管如此,AOP 是范例,AspectJ 实现了它,就像 OOP 和 Java.
使用 AOP 将横切关注点 (CCC) 集中到它们自己的模块(例如, 方面)的好处类似于 AOP 的模块化关注点 OOP
。这些好处在论文和书籍的文献中有所描述,例如:
- Aspect-Oriented Programming
- Towards a Common Reference Architecture for Aspect-Oriented Modeling
- Impact of aspect-oriented programming on software development efficiency and design quality: an empirical study.
- AspectJ in Action: Enterprise AOP with Spring
和许多其他人。可以将其中一些好处总结如下:
- 减少代码重复;与其在几个不同的地方重复一个功能(例如,日志),一个人把它放在一个单方面。然后可以使用 pointcut;
的概念将该功能应用于那几个地方
- 减少领域相关和横切相关关注点之间的耦合(即关注点分离);例如,从域代码中删除日志记录遵循 single-responsibility principle;
- 代码可重用性的增强;由于前面提到的关注点分离,一个(例如)增加了封装基础代码的模块和封装日志的模块的可重用性;
- 处理代码tangling and scattering issues;如下图所示
与其让日志记录 1 和 2 直接与域代码纠缠,不如在不同的模块中进行复制和分散(即, Class A
和 Class B
),我们将那些与日志记录相关的功能封装到一个方面。
文献中有一些论文介绍了 AOP 在横切关注点(例如日志记录)模块化方面的好处,即:
S. Canditt and M. Gunter. Aspect oriented logging in a real-world
system. In First AOSD Workshop on Aspects, Components, and Patterns
for Infrastructure Software (AOSD-2002), March 2002.
人们可以在文献中读到 AOP 的缺点是 those typical of a learning/integrating a new technology,即:
- Hiring skilled programmers
- Following an adoption path to ensure that you don’t risk the project by overextending yourself
- Modifying the build and other development processes
- Dealing with the availability of tools
论文报告的其他缺点,例如:
- An exploratory study of the effect of aspect-oriented programming on maintainability
- Using aspect-orientation in industrial projects: appreciated or damned?
- Using and reusing aspects in aspectj.
- A Case Study Implementing Features Using AspectJ(这个特别好,展示了使用 AOP 的一些问题
可以概括为:
必须学习一种新范式;
缺少工具支持;
编织的性能;
使代码更难推理,因为与 CCC 相关的代码已移至别处。例如,相同的论点可以应用于子类化或装饰模式的使用。然而,IDEs 减轻了这些问题,在 AspectJ
的情况下,通过显示被拦截的连接点。另一个解决方案是例如使用注释并拦截这些注释:
@记录
public无效some_method(){...}
当程序员查看注释时,立即知道该方法正在被切入点拦截。这种方法的缺点是您 种类 再次将 CCC 与域代码混合,尽管只是以注释的形式。
另一方面,人们可以争辩说,由于与领域相关的问题和 CCC 现在封装在不同的模块中,因此更容易孤立地对它们进行推理。
- Pointcut fragility; This problem is similar to the fragile base class problem. From source 一个可以读:
changes to the base-code can lead to join points incorrectly falling
in or out of the scope of pointcuts.
例如,添加新方法或更改它们的签名可能会导致切入点拦截它们或停止拦截它们。有一些技术可以减轻此类问题。但是,在我的选择中,解决方案是工具。当在 Java 中重命名方法时,人们希望 IDE 能够安全地应用这些更改。
- 连接点模块的粒度。使用 AspectJ(使用 Spring AOP 更糟)可能很难获得日志记录所需的本地上下文,例如本地变量,这可能会迫使您重构代码以便公开所需的 joinpoints -- known in requirements engineering as scaffolding. On the other side, refactoring your code might actually improve it. From: "21. Why can't AspectJ pick out local variables (or array elements or ...)?" 可以阅读:
Users have sometimes wanted AspectJ to pick out many more join points,
including method-local field access, array-element access, loop iteration, method parameter evaluation
Most of these have turned out not to make sense, for a variety of
reasons: it is not a commonly-understood unit for Java programmers
there are very few use-cases for advice on the join point
a seemingly-insignificant change to the underlying program causes a
change in the join point
pointcuts can't really distinguish the join point in question
the join point would differ too much for different implementations of
AspectJ, or would only be implementable in one way
We prefer to be very conservative in the join point model for the
language, so a new join point would have to be useful, sensible, and
implementable. The most promising of the new join points proposed are
for exception throws clauses and for synchronized blocks.
您需要评估在您的上下文中是否有回报在您的代码中添加一个额外的层(即 横向模块化)根据;并针对代码生成器、框架或设计模式等替代方法进行评估。
在日志记录的情况下,上述一些问题并不有问题,因为如果出现问题......好吧,你只是 丢失或添加一些日志记录。
意见基础部分
众所周知,日志记录是一个横切关注点。但是,我从未见过有人将所有日志集中到一个或一堆文件中。因此,我想知道为什么。
关注点分离是一种不好的做法吗?! (除非你把它带到极端不)。我肯定会说这不是一个坏习惯,值得吗?!好吧,这更复杂并且取决于上下文。
我个人讨厌看到漂亮的代码片段与日志功能混合在一起。 IMO 考虑到软件必须处理许多其他变量,而不仅仅是每个 si 的代码,例如紧迫的最后期限,不将日志记录与基本代码混合似乎没有更高的优先级。
我想了解使用 AOP(例如 使用 AspectJ)将所有应用程序日志集中到单独文件中的优缺点。
Logging is know for being a cross-cutting concern。但是,我从未见过有人将所有日志集中到一个或一堆文件中。因此,我想知道为什么。
这样做的利弊是什么?
I am looking to understand the pros and cons of centralizing all applications logs into separate files with AOP (e.g. with AspectJ).
我将交替使用术语 AOP 和 AspectJ。尽管如此,AOP 是范例,AspectJ 实现了它,就像 OOP 和 Java.
使用 AOP 将横切关注点 (CCC) 集中到它们自己的模块(例如, 方面)的好处类似于 AOP 的模块化关注点 OOP
。这些好处在论文和书籍的文献中有所描述,例如:
- Aspect-Oriented Programming
- Towards a Common Reference Architecture for Aspect-Oriented Modeling
- Impact of aspect-oriented programming on software development efficiency and design quality: an empirical study.
- AspectJ in Action: Enterprise AOP with Spring
和许多其他人。可以将其中一些好处总结如下:
- 减少代码重复;与其在几个不同的地方重复一个功能(例如,日志),一个人把它放在一个单方面。然后可以使用 pointcut; 的概念将该功能应用于那几个地方
- 减少领域相关和横切相关关注点之间的耦合(即关注点分离);例如,从域代码中删除日志记录遵循 single-responsibility principle;
- 代码可重用性的增强;由于前面提到的关注点分离,一个(例如)增加了封装基础代码的模块和封装日志的模块的可重用性;
- 处理代码tangling and scattering issues;如下图所示
与其让日志记录 1 和 2 直接与域代码纠缠,不如在不同的模块中进行复制和分散(即, Class A
和 Class B
),我们将那些与日志记录相关的功能封装到一个方面。
文献中有一些论文介绍了 AOP 在横切关注点(例如日志记录)模块化方面的好处,即:
S. Canditt and M. Gunter. Aspect oriented logging in a real-world system. In First AOSD Workshop on Aspects, Components, and Patterns for Infrastructure Software (AOSD-2002), March 2002.
人们可以在文献中读到 AOP 的缺点是 those typical of a learning/integrating a new technology,即:
- Hiring skilled programmers
- Following an adoption path to ensure that you don’t risk the project by overextending yourself
- Modifying the build and other development processes
- Dealing with the availability of tools
论文报告的其他缺点,例如:
- An exploratory study of the effect of aspect-oriented programming on maintainability
- Using aspect-orientation in industrial projects: appreciated or damned?
- Using and reusing aspects in aspectj.
- A Case Study Implementing Features Using AspectJ(这个特别好,展示了使用 AOP 的一些问题
可以概括为:
必须学习一种新范式;
缺少工具支持;
编织的性能;
使代码更难推理,因为与 CCC 相关的代码已移至别处。例如,相同的论点可以应用于子类化或装饰模式的使用。然而,IDEs 减轻了这些问题,在
AspectJ
的情况下,通过显示被拦截的连接点。另一个解决方案是例如使用注释并拦截这些注释:@记录 public无效some_method(){...}
当程序员查看注释时,立即知道该方法正在被切入点拦截。这种方法的缺点是您 种类 再次将 CCC 与域代码混合,尽管只是以注释的形式。
另一方面,人们可以争辩说,由于与领域相关的问题和 CCC 现在封装在不同的模块中,因此更容易孤立地对它们进行推理。
- Pointcut fragility; This problem is similar to the fragile base class problem. From source 一个可以读:
changes to the base-code can lead to join points incorrectly falling in or out of the scope of pointcuts.
例如,添加新方法或更改它们的签名可能会导致切入点拦截它们或停止拦截它们。有一些技术可以减轻此类问题。但是,在我的选择中,解决方案是工具。当在 Java 中重命名方法时,人们希望 IDE 能够安全地应用这些更改。
- 连接点模块的粒度。使用 AspectJ(使用 Spring AOP 更糟)可能很难获得日志记录所需的本地上下文,例如本地变量,这可能会迫使您重构代码以便公开所需的 joinpoints -- known in requirements engineering as scaffolding. On the other side, refactoring your code might actually improve it. From: "21. Why can't AspectJ pick out local variables (or array elements or ...)?" 可以阅读:
Users have sometimes wanted AspectJ to pick out many more join points, including method-local field access, array-element access, loop iteration, method parameter evaluation
Most of these have turned out not to make sense, for a variety of reasons: it is not a commonly-understood unit for Java programmers
there are very few use-cases for advice on the join point
a seemingly-insignificant change to the underlying program causes a change in the join point
pointcuts can't really distinguish the join point in question
the join point would differ too much for different implementations of AspectJ, or would only be implementable in one way
We prefer to be very conservative in the join point model for the language, so a new join point would have to be useful, sensible, and implementable. The most promising of the new join points proposed are for exception throws clauses and for synchronized blocks.
您需要评估在您的上下文中是否有回报在您的代码中添加一个额外的层(即 横向模块化)根据;并针对代码生成器、框架或设计模式等替代方法进行评估。
在日志记录的情况下,上述一些问题并不有问题,因为如果出现问题......好吧,你只是 丢失或添加一些日志记录。
意见基础部分
众所周知,日志记录是一个横切关注点。但是,我从未见过有人将所有日志集中到一个或一堆文件中。因此,我想知道为什么。
关注点分离是一种不好的做法吗?! (除非你把它带到极端不)。我肯定会说这不是一个坏习惯,值得吗?!好吧,这更复杂并且取决于上下文。
我个人讨厌看到漂亮的代码片段与日志功能混合在一起。 IMO 考虑到软件必须处理许多其他变量,而不仅仅是每个 si 的代码,例如紧迫的最后期限,不将日志记录与基本代码混合似乎没有更高的优先级。