0

I have used spring aop (aspect oriented programing) in my web application. Now I want to add logs to the places where Join-points occur. How can I configure my log4j xml for this task.

1 Answer 1

3

For this requirement i have created one Aspect logger. this works on different layers like controller, service DAO with different level of logging.

@Aspect
public class LoggerAspect {

    private final static Logger LOGGER = LoggerFactory
            .getLogger(LoggerAspect.class);

    @Autowired
    private RequestInfo requestInfo;

    @Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public Object logWebRequest(ProceedingJoinPoint pjp) throws Throwable {
        Object obj;
        Long startTime = System.currentTimeMillis();
        if (SecurityContextHolder.getContext().getAuthentication() != null) {
            Object user = SecurityContextHolder.getContext()
                    .getAuthentication().getPrincipal();
            LOGGER.info("User: " + user);
        }
        LOGGER.info("Controller UUID: " + requestInfo.getRequestUUID());
        LOGGER.info("Controller URL: " + requestInfo.getRequestMethod());
        LOGGER.info("Controller Method: " + pjp.getSignature());
        logArguments(pjp);
        try {
            obj = pjp.proceed();
        } catch (Exception e) {
            LOGGER.error("Controller:" + e.getMessage());
            throw e;
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Returns: " + obj);
            LOGGER.debug("Controller Response Time: "
                    + (System.currentTimeMillis() - startTime) + " ms");
        }
        return obj;
    }

    @Around("execution(public * com.common.service..*.*(..))")
    public Object logCommonServiceRequest(ProceedingJoinPoint pjp)
            throws Throwable {
        return logServiceRequest(pjp);
    }

    @Around("execution(public * com.modules.*.service..*.*(..))")
    public Object logModulesServiceRequest(ProceedingJoinPoint pjp)
            throws Throwable {
        return logServiceRequest(pjp);
    }

    private Object logServiceRequest(ProceedingJoinPoint pjp) throws Throwable,
            Exception {
        Object obj;
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Service: " + pjp.getTarget().getClass() + " -- "
                    + pjp.getSignature());
            logArguments(pjp);
        }
        try {
            obj = pjp.proceed();
        } catch (Exception e) {
            LOGGER.error("Service Exception:" + e.getMessage());
            throw e;
        }
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Service Returns: " + obj);
        return obj;
    }

    @Around("execution(public * com.common.dao..*.*(..))")
    public Object logCommonDAORequest(ProceedingJoinPoint pjp) throws Throwable {
        return logDAORequest(pjp);
    }

    @Around("execution(public * com.modules.*.dao..*.*(..))")
    public Object logModulesDAORequest(ProceedingJoinPoint pjp)
            throws Throwable {
        return logDAORequest(pjp);
    }

    private Object logDAORequest(ProceedingJoinPoint pjp) throws Throwable,
            Exception {
        Object obj;
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("DAO: " + pjp.getTarget().getClass() + " -- "
                    + pjp.getSignature());
            logArguments(pjp);
        }
        try {
            obj = pjp.proceed();
        } catch (Exception e) {
            LOGGER.error("DAO Exception:" + e.getMessage());
            throw e;
        }
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("DAO Returns: " + obj);
        return obj;
    }

    private void logArguments(ProceedingJoinPoint pjp) {
        StringBuffer argLog = new StringBuffer();
        for (Object arg : pjp.getArgs()) {
            argLog.append(arg);
            argLog.append(",");
        }
        LOGGER.info("Args: " + argLog);
    }

}


<aop:aspectj-autoproxy proxy-target-class="true">
    <aop:include name="loggerAspect" />
</aop:aspectj-autoproxy>

<bean id="loggerAspect" class="com.service.LoggerAspect">
</bean>
Sign up to request clarification or add additional context in comments.

1 Comment

What you have mention here is another aspect called LoggingAspect, what I want is to find how it is called by the spring framework whent the join points are met. For that purpose I need find out a way to add logs for org.springframework.aop with a logging level. Thats what my problem is about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.