使用Eclipse AspectJ注入一个记录执行代码的上下文/元数据的记录器? [英] Using Eclipse AspectJ to Inject a Logger which Logs context/meta data of the code execute?

查看:67
本文介绍了使用Eclipse AspectJ注入一个记录执行代码的上下文/元数据的记录器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试定义一个方面来注入记录器.

I am trying to define an aspect to inject a logger.

我正在寻找类似的东西

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public aspect LogInjector {

    private pointcut executionJoinPoints(): !within(LogInjector) && execution (* *.*(..));

    before(): executionJoinPoints(){
        // Get class name of the executed code
        clazz = ...
        final Logger logger = LogManager.getLogger(clazz);

        // Get method name of the executed code
        method = ...

        // Get params name, type and values triplet or values at least if the previous is not possible, of the executed code
        params = ...

        // Get call stack of the executed code
        stack = ...

        logger.trace("{}.{}({}) - {}", clazz.name(), method.name(), params, stack);
    }

    after(): executionJoinPoints(){
        // Get class name of the executed code
        clazz = ...
        final Logger logger = LogManager.getLogger(clazz);

        // Get method name of the executed code
        method = ...

        // Get return value or exception of the executed code
        result = ...

        logger.trace("{}.{} = {}", clazz.name(), method.name(), result);
    }
}

为此,我要检索执行元数据/上下文数据:

For this I want to retrieve execution metadata/context data:

  • 例外
  • 返回值

如何获取此元数据/上下文数据?

How can get this metadata/context data?

推荐答案

为了保持您的方面高效,我建议执行以下操作:

In order to keep your aspect efficient, I recommend the following:

  • 将切入点限制为您真正希望调试的目标包和类.不要记录/追踪整个世界.您还可以将抽象基础方面与抽象切入点一起使用,并将方面扩展为具有具体切入点的具体子方面.如果使用加载时间编织,甚至可以通过XML配置提供后者.
  • 使用 around()建议而不是 before()/ after()对.然后,您只需计算一次某些记录的值,即可在通过 proceed()完成原始方法调用之前和之后使用它们.
  • 仅记录 thisJoinPoint ,而不是默认情况下将其中包含的位拼凑在一起.这将为您提供连接点的类型,方法签名(包括参数类型和返回值).
  • 不记录参数名称,该信息不增加任何实际值.此外,参数名称可以进行重构,并且仅当您的代码是使用调试信息编译时才存在.保持简单,只记录参数值.
  • 在上述 around()建议中,您可以将 proceed()调用包含在 try-catch-finally 中,并方便地处理和记录所有异常和堆栈跟踪和/或将检查的异常包装到AspectJ的 SoftException 或简单的 RuntimeException 中,然后将它们重新抛出.无论哪种情况都适合您.
  • 方法调用结果只是 proceed()的结果,这也是您从 around()建议中返回的结果.如果出于某种原因希望跳过目标方法的执行,也可以返回其他内容(但必须具有正确的返回类型)或完全跳过 proceed().
  • Limit your pointcut to the target packages and classes you really wish to debug. Don't log/trace the whole world. You could also use an abstract base aspect with an abstract pointcut and extend the aspect into a concrete sub-aspect with a concrete pointcut. The latter can even be provided via XML configuration if you use load time weaving.
  • Use an around() advice instead of a before() / after() pair. Then you only need to calculate some of the logged values once and use them both before and after the original method call done via proceed().
  • Simply log thisJoinPoint instead of piecing together bits contained therein by default. This will already give you the type of joinpoint, method signature including parameter types and return value.
  • Don't log parameter names, the information adds no real value. Furthermore, parameter names are subject to refactoring and are only present if your code is compiled with debug information. Keep it simple and only log the parameter values.
  • In the around() advice mentioned above you can enclose the proceed() call into try-catch-finally and conveniently handle and log any exceptions and stack traces and/or wrap checked exceptions into AspectJ's SoftException or a simple RuntimeException and re-throw them. Whatever is applicable to your situation.
  • Method call results are just the results of proceed(), which would you also be what you need to return from the around() advice. You can also return something else instead (but it must have the correct return type) or completely skip proceed() if for whatever reason you wish to skip target method execution.

我刚才说的所有内容都写在AspectJ手册或任何其他AspectJ教程中.您可能想下次再问一些类似的一般问题之前,先阅读其中的一些内容.

All of what I just said is written in the AspectJ manual or in any other AspectJ tutorial. You might want to read some of those next time before asking a general question like this one.

这篇关于使用Eclipse AspectJ注入一个记录执行代码的上下文/元数据的记录器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆