Spring Aop 日志记录行号不正确 [英] Spring Aop logging line number incorrect

查看:48
本文介绍了Spring Aop 日志记录行号不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using spring aop to do logging for my application : I have before after and afterthrowing advice configured but the line numbers that I see is not of the target class but that of the class used for logging How can I solve this Below is my configuration

弹簧 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;
    }
}

是我遗漏了什么还是应该是这样的

Am I missing something or is it suppose to be like this

谢谢

尼拉夫

推荐答案

我认为这不是 Spring AOP 特有的问题,而只是 Log4j 的工作方式,请参阅 Javadoc 以了解 PatternLayout:

I think this is not specifically a Spring AOP problem but just the way Log4j works, see Javadoc for PatternLayout:

L

用于输出发出日志请求的行号

Used to output the line number from where the logging request was issued.

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

WARNING Generating caller location information is extremely slow and should be avoided unless execution speed is not an issue.

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

So my recommendation is to use a pattern layout without line number and use Spring AOP's capability of determining line numbers, roughly like this:

joinPoint.getSourceLocation().getLine()

这篇关于Spring Aop 日志记录行号不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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