java AOP 로 그 를 인쇄 합 니 다

34804 단어 코드 세 션
1. 사용자 정의 설명 만 들 기:
package com.yan.demo.core.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({
     ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Logging {
     
    String value() default "";
}


2. 절단면 류 만 들 기
package com.yan.demo.core.aspect;

import com.alibaba.fastjson.JSONObject;
import com.wmeimob.fastboot.util.web.IpAddressHelper;
import java.math.BigDecimal;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Aspect
@Component
public class LoggingAspect {
     
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public LoggingAspect() {
     
    }

	//  
    @Pointcut("@within(com.yan.demo.core.annotation.Logging)")
    public void loggingAspect() {
     
    }

    @Before("loggingAspect()")
    public void before(JoinPoint joinPoint) {
     
        if (this.logger.isDebugEnabled()) {
     
            HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
            StringBuilder params = new StringBuilder();
            if (joinPoint.getArgs() != null && joinPoint.getArgs().length > 0) {
     
                for(int i = 0; i < joinPoint.getArgs().length; ++i) {
     
                    params.append(joinPoint.getArgs()[i]).append(";");
                }
            }

            StringBuffer sb = new StringBuffer();
            sb.append("
IP:"
).append(IpAddressHelper.getRemoteHost(request)).append("
"
); sb.append(" : ").append(joinPoint.getTarget().getClass().getName()).append(".").append(joinPoint.getSignature().getName()).append("()
"
); sb.append(" : ").append("".equals(params.toString()) ? " " : params.toString()).append("
"
); this.logger.info(sb.toString()); this.logger.info("execute method ==>>{}()", joinPoint.getSignature().getName()); } } @Around("loggingAspect()") public Object around(ProceedingJoinPoint pjp) throws Throwable { HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); this.logger.debug("******************* START*********************"); long start = System.currentTimeMillis(); Object obj = pjp.proceed(pjp.getArgs()); long end = System.currentTimeMillis(); BigDecimal second = (new BigDecimal(end)).subtract(new BigDecimal(start)).divide(new BigDecimal("1000")).setScale(2, 4); String time = second + " "; this.logger.debug("------------------- :" + time + "----------------------"); if (second.doubleValue() > 1.0D) { this.logger.warn("execute method timeout!!!"); } this.logger.debug("******************* END***********************
"
); return obj; } @AfterThrowing( pointcut = "loggingAspect()", throwing = "e" ) public void doAfterThrowing(JoinPoint joinPoint, Throwable e) { HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); String ip = IpAddressHelper.getRemoteHost(request); String params = "
"
; if (joinPoint.getArgs() != null && joinPoint.getArgs().length > 0) { for(int i = 0; i < joinPoint.getArgs().length; ++i) { if (!(joinPoint.getArgs()[i] instanceof HttpServletRequest) && !(joinPoint.getArgs()[i] instanceof HttpServletResponse)) { params = params + JSONObject.toJSONString(joinPoint.getArgs()[i]) + "
"
; } } } StringBuffer sb = new StringBuffer(); sb.append("
=================== START==================
"
); sb.append(" :" + joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()
"
); sb.append(" IP:" + ip + "
"
); sb.append(" :" + params + "
"
); sb.append(" :" + e.getClass().getName() + "
"
); sb.append(" :" + e.getMessage() + "
"
); sb.append("=================== END====================
"
); this.logger.error(sb.toString()); } }

사용
로 그 를 인쇄 할 방법 에 @ Logging 주 해 를 추가 합 니 다.
AOP 사용 해 주세요. 여기.

좋은 웹페이지 즐겨찾기