Spring Aop logging line number incorrect

断了今生、忘了曾经 提交于 2019-12-29 09:18:12

问题


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

Spring xml :

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

Class used for logging :

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

Thanks

Nirav


回答1:


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.

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()


来源:https://stackoverflow.com/questions/29443899/spring-aop-logging-line-number-incorrect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!