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

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

问题描述

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

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

我正在寻找类似的东西:

I am looking to create something like:

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天全站免登陆