【问题标题】:Spring Aop logging line number incorrectSpring Aop 日志记录行号不正确
【发布时间】:2015-06-09 05:38:13
【问题描述】:

我正在使用 spring aop 为我的应用程序进行日志记录: 我配置了之前和之后的建议,但我看到的行号不是目标类,而是用于记录的类的行号 我该如何解决这个问题 下面是我的配置

春天的xml:

<aop:aspectj-autoproxy proxy-target-class="false" />

用于记录的类:

package com.digilegal.services.ahc.logging;

import java.lang.reflect.Modifier;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

@Aspect
public class AHCLogging {

    @Before("execution(* com.digilegal.services..*.*(..))")
    public void logBefore(JoinPoint joinPoint) {

        Logger log = Logger.getLogger(joinPoint.getTarget().getClass());
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();

        if (!Modifier.isPrivate(signature.getModifiers())
                && !signature.getName().startsWith("get")
                && !signature.getName().startsWith("set")
                && !signature.getName().startsWith("is")) {
            log.trace("ENTER METHOD ::"
                    + signature.getReturnType().getSimpleName() + " "
                    + signature.getName() + "("
                    + paramterType(signature.getParameterTypes()) + ")");
        }

    }

    @After("execution(* com.digilegal.services..*.*(..))")
    public void logAfter(JoinPoint joinPoint) {
        Logger log = Logger.getLogger(joinPoint.getTarget().getClass());
        MethodSignature signature = (MethodSignature)     joinPoint.getSignature();

        if (!Modifier.isPrivate(signature.getModifiers())
                && !signature.getName().startsWith("get")
                && !signature.getName().startsWith("set")
                && !signature.getName().startsWith("is")) {
            log.trace("EXIT METHOD ::"
                    + signature.getReturnType().getSimpleName() + " "
                    + signature.getName() + "("
                    + paramterType(signature.getParameterTypes()) + ")");
        }
    }

    @AfterThrowing(pointcut = "execution(* com.digilegal.services..*.*        (..))",throwing= "error")
    public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
        Logger log = Logger.getLogger(joinPoint.getTarget().getClass());
        MethodSignature signature = (MethodSignature)     joinPoint.getSignature();

        if (!Modifier.isPrivate(signature.getModifiers())
                && !signature.getName().startsWith("get")
                && !signature.getName().startsWith("set")
                && !signature.getName().startsWith("is")) {
            log.error("EXCEPTION IN METHOD ::"
                    + signature.getReturnType().getSimpleName() + " "
                    + signature.getName() + "("
                    + paramterType(signature.getParameterTypes()) + ")");
            log.error("Exception",error);
        }
    }

    private String paramterType(Class<?>[] classes) {
        StringBuffer buffer = new StringBuffer();
        String returnValue = "";

        for (Class<?> string : classes) {
            buffer.append(Modifier.toString(string.getModifiers()));
            buffer.append(" ");
            buffer.append(string.getSimpleName());
            buffer.append(",");
        }

        returnValue = buffer.toString();

        if (returnValue.trim().length() > 0) {
            returnValue = returnValue.substring(0, returnValue.length() -         1);
        }

        return returnValue;
    }
}

我错过了什么还是应该是这样的

谢谢

尼拉夫

【问题讨论】:

  • 那里没有提到行号,所以我不确定你在问什么。你也知道 spring 已经有一个类可以为你记录这些东西(而不是添加你自己的?)。检查CustomizableTraceInterceptor
  • 很酷,感谢您指出 Spring 课程,这将有很大帮助,但我要说的是:2015 年 4 月 4 日 15:25:08:775 TRACE LoginBean:30 - ENTER METHOD ::void doLogin(public ActionEvent) 其中 30 是日志记录类的行号,而不是 LoginBean

标签: spring logging aop


【解决方案1】:

我认为这不是 Spring AOP 的具体问题,而只是 Log4j 的工作方式,请参阅 Javadoc for PatternLayout

L

用于输出发出记录请求的行号

警告生成调用方位置信息非常缓慢,除非执行速度不是问题,否则应避免。

所以我的建议是使用没有行号的模式布局,并使用Spring AOP的确定行号的能力,大致是这样的:

joinPoint.getSourceLocation().getLine()

【讨论】:

  • 您好 Kriegaex,谢谢您的帮助
  • 好吧,如果是这样,请接受并支持我的回答。
  • 这种方法 joinPoint.getSourceLocation().getLine() 不支持 Spring-AOP 到 4.2.2。 Exception in thread "main" java.lang.UnsupportedOperationException
  • 我真的不是Spring用户,但是你见过this吗?我不知道它是否有帮助。不管怎样,这个问题的问题是 Log4J 使用错误。
  • 遗憾,在 Spring 4.2.4 上也不起作用……该死!
猜你喜欢
  • 2018-04-13
  • 2012-11-04
  • 2012-09-30
  • 1970-01-01
  • 1970-01-01
  • 2011-04-05
  • 1970-01-01
  • 2011-08-12
  • 2015-08-18
相关资源
最近更新 更多