All Downloads are FREE. Search and download functionalities are using the official Maven repository.

leisure.springboot.web.aspect.ControlMonitor Maven / Gradle / Ivy

The newest version!
package leisure.springboot.web.aspect;

import leisure.springboot.core.BeanFactory;
import leisure.springboot.web.util.HostInfo;
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.Pointcut;
import org.aspectj.lang.reflect.CodeSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpUtils;
import java.util.Map;

@Aspect
@Component
public class ControlMonitor {
    private static final Logger log = LoggerFactory.getLogger(ControlMonitor.class);

    private final String line = System.getProperty("line.separator");

    private Map interceptors = BeanFactory.getApplicationContext().getBeansOfType(LeisureResultHandle.class);

    public ControlMonitor() {
    }

    @Pointcut(value = "@within(org.springframework.web.bind.annotation.RestController),@within(org.springframework.stereotype.Controller)")
    public void pointCut() {
    }


    @AfterThrowing(pointcut = "pointCut()", throwing = "ex")
    public void around(Exception ex) {
    }


    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        StringBuilder sb = new StringBuilder();
        String context = leisure.core.util.UUIDUtils.get32UUID();
        MDC.put("uuid", context);
        sb.append(String.format("日志开始:%s", context));
        Object[] paramValues = null;
        String[] paramNames = null;
        Object result = null;
        org.aspectj.lang.Signature signature = joinPoint.getSignature();
        sb.append(line);
        sb.append(context);
        sb.append(":");
        sb.append(String.format("方法信息:%s.%s", signature.getDeclaringTypeName(), signature.getName()));
        paramValues = joinPoint.getArgs();
        paramNames = ((CodeSignature) joinPoint.getSignature()).getParameterNames();
        sb = getHost(sb, context);
        sb = getParam(sb, paramNames, paramValues, context);
        try {
            result = joinPoint.proceed();
            sb = getResult(sb, result, context);
            if (interceptors != null && interceptors.size() > 0) {
                for (LeisureResultHandle leisureResultHandle : interceptors.values()) {
                    result = leisureResultHandle.ReturnValueHandler(result);
                }
            }
            sb = getEnd(sb, context);
            logInfo(sb);
            return result;
        } catch (Exception ex) {
            throw ex;
        } finally {
        }
    }

    private StringBuilder getParam(StringBuilder sb, String[] paramNames, Object[] paramValues, String context) {
        for (int i = 0; i < paramNames.length; i++) {
            sb.append(line);
            sb.append(context);
            sb.append(":");
            sb.append(String.format("参数 %s", paramNames[i]));
            sb.append(String.format(":%s", paramValues[i]));
        }
        return sb;
    }

    private StringBuilder getResult(StringBuilder sb, Object result, String context) {
        sb.append(line);
        sb.append(context);
        sb.append(":");
        sb.append(String.format("返回值:%s", result));
        return sb;
    }

    private StringBuilder getHost(StringBuilder sb, String context) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String url = HttpUtils.getRequestURL(request).toString();
        String ip = HostInfo.getIpAddr(request);
        String agent = HostInfo.getUserAgent(request);
        sb.append(line);
        sb.append(context);
        sb.append(":");
        sb.append(String.format("url:%s", url));
        sb.append(line);
        sb.append(context);
        sb.append(":");
        sb.append(String.format("ip:%s", ip));
        sb.append(line);
        sb.append(context);
        sb.append(":");
        sb.append(String.format("agent:%s", agent));
        return sb;
    }

    private StringBuilder getEnd(StringBuilder sb, String context) {
        sb.append(line);
        sb.append("结束:" + context);
        sb.append(line);
        sb.append("-----------------------------------------");
        return sb;
    }

    private void logInfo(StringBuilder sb) {
        if (log.isDebugEnabled()) {
            log.debug(sb.toString());
        }
        if (!log.isDebugEnabled() && log.isInfoEnabled()) {
            log.info(sb.toString());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy