是否有可能“假装"通过方面进行日志记录的课程? [英] Is it possible to "impersonate" a class in logging via aspect?

查看:69
本文介绍了是否有可能“假装"通过方面进行日志记录的课程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个方面,当我进入和退出具有标准格式(同时也记录该类)的类时,将记录该方面,例如:

I developed an aspect that will log when I enter and exit from a class with the standard format that also logs the class, so something like:

2020-03-20 20:05:30.280  INFO 3336 --- [nio-8080-exec-2] c.a.common.aop.LoggingAspect        : Entering method

请注意,日志记录类似乎是"LoggingAspect".而是可以记录代理的类名吗?此刻我得到:

Please notice that the logging class appears to be "LoggingAspect". Would it be possible, instead, to log the proxied class name? At the moment I get:

2020-03-20 20:05:30.280  INFO 3336 --- [nio-8080-exec-2] c.a.common.aop.LoggingAspect        : Entering method 
2020-03-20 20:06:30.280  INFO 3336 --- [nio-8080-exec-2] c.a.my.proxied.Class        : Doing stuff
2020-03-20 20:05:30.280  INFO 3336 --- [nio-8080-exec-2] c.a.common.aop.LoggingAspect        : Exiting method 

我总是希望每行都有 c.a.my.proxied.Class .这是我的方面:

I would like to always have c.a.my.proxied.Class for each line. Here's my aspect:

@Aspect
@Order(1)
@Component
@Slf4j
public class LoggingAspect {

  @Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
  private void controllerInvocation() {}

  @Around("controllerInvocation()")
  public Object logEntering(ProceedingJoinPoint joinPoint) throws Throwable{
     log.info("Entering method");
     Object res = joinPoint.proceed();
     log.info("Exiting method");
     return res;
  }
}

推荐答案

摘自注释文档

From the documentation for annotation @Slf4j

@Slf4j
public class LogExample {}

将生成:

public class LogExample {
    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
}

这意味着通过使用 log 引用,它将始终用于共享代码中的 LoggingAspect .

which means that by using the log reference , it would always be for LoggingAspect in the code shared.

一种解决方法可能如下

import org.slf4j.Logger;
import static org.slf4j.LoggerFactory.getLogger;

// ...

@Around("controllerInvocation()")
public Object logEntering(ProceedingJoinPoint joinPoint) throws Throwable {
    // Get the logger for the class of intercepted method
    Logger logz = getLogger(joinPoint.getTarget().getClass());
    logz.info("Entering method");
    Object res = joinPoint.proceed();
    logz.info("Exiting method");
    return res;
}

希望这会有所帮助

OP请求@kriegaex来举例说明他的建议.我可以自由地说明他的建议.

Edit : OP requested @kriegaex for an example of his suggestion. I take the liberty to illustrate his suggestions.

请参阅aop示例

Please refer the aop example section in documentation for more details

import org.slf4j.Logger;
import static org.slf4j.LoggerFactory.getLogger;

// ...

@Around("controllerInvocation() && target(myTarget)")
public Object logEntering(ProceedingJoinPoint joinPoint,Object myTarget) throws Throwable {
    // Get the logger for the class of intercepted method
    Logger logz = getLogger(myTarget.getClass());
    logz.info("Entering method");
    try{
        Object res = joinPoint.proceed();
    }finally{
        logz.info("Exiting method");
    }
    return res;
}

这篇关于是否有可能“假装"通过方面进行日志记录的课程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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