AOP应用之日志

一、声明式方式
切面


package com.abc.aop;

import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Enumeration;

public class WebLogAspect {
    private Logger logger = Logger.getLogger(WebLogAspect.class);
    private ThreadLocal sessionId = new ThreadLocal();
    public void doBefore(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            if ("application/json".equals(request.getContentType())) {
                sessionId.set(request.getSession().getId());
                logger.info("\n>>[    SessionId]" + sessionId.get() +
                            "\n>>[   RemoteAddr]" + request.getRemoteAddr() +
                            "\n>>[   RequestURL]" + request.getRequestURL().toString() +
                            "\n>>[ Content-type]" + request.getContentType() +
                            "\n>>[RequestParams]" + Arrays.toString(joinPoint.getArgs()));
            } else {
                sessionId.set(request.getSession().getId());
                JSONObject params = new JSONObject();
                Enumeration keys = request.getParameterNames();
                String key = null;
                while (keys.hasMoreElements()) {
                    key = (String) keys.nextElement();
                    params.put(key, request.getParameter(key));
                }
                logger.info("\n>>[    SessionId]" + sessionId.get() +
                            "\n>>[   RemoteAddr]" + request.getRemoteAddr() +
                            "\n>>[   RequestURL]" + request.getRequestURL().toString() +
                            "\n>>[ Content-type]" + request.getContentType() +
                            "\n>>[RequestParams]" + params.toJSONString());
            }
        }
    }
    public void doAfterReturning(Object resp) {
        logger.info("\n<<[SessionId]" + sessionId.get() +
                    "\n<<[ Response]" + resp);
    }
}

切入配置

	
	
		
		
			
			
		
	

二、注解方式

package com.abc.aop;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Enumeration;
@Aspect
public class WebLogAspect {
    private Logger logger = Logger.getLogger(WebLogAspect.class);
    private ThreadLocal sessionId = new ThreadLocal();
    @Pointcut("execution(public  * com.abc.controller..*.*(..))")
    public void webLog() {
    }
    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) {
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            // 记录下请求内容
            HttpServletRequest request = attributes.getRequest();
            if ("application/json".equals(request.getContentType())) {
                sessionId.set(request.getSession().getId());
                logger.info("\n>>[    SessionId]" + sessionId.get() +
                            "\n>>[   RemoteAddr]" + request.getRemoteAddr() +
                            "\n>>[   RequestURL]" + request.getRequestURL().toString() +
                            "\n>>[ Content-type]" + request.getContentType() +
                            "\n>>[RequestParams]" + Arrays.toString(joinPoint.getArgs()));
            } else {
                sessionId.set(request.getSession().getId());
                JSONObject params = new JSONObject();
                Enumeration keys = request.getParameterNames();
                String key = null;
                while (keys.hasMoreElements()) {
                    key = (String) keys.nextElement();
                    params.put(key, request.getParameter(key));
                }
                logger.info("\n>>[    SessionId]" + sessionId.get() +
                            "\n>>[   RemoteAddr]" + request.getRemoteAddr() +
                            "\n>>[   RequestURL]" + request.getRequestURL().toString() +
                            "\n>>[ Content-type]" + request.getContentType() +
                            "\n>>[RequestParams]" + params.toJSONString());
            }
        }
    }   
    @AfterReturning(returning = "resp", pointcut = "webLog()")
    public void doAfterReturning(Object resp) {
        logger.info("\n<<[SessionId]" + sessionId.get() +
                    "\n<<[ Response]" + resp);
    }
}

你可能感兴趣的:(JAVA,SSM)