首页 > 其他 > 详细

日志相关

时间:2019-02-22 16:01:37      阅读:172      评论:0      收藏:0      [点我收藏+]
package com.commons.aop;

import com.haiercash.commons.exception.BusinessException;
import com.haiercash.commons.util.RestUtil;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.json.JSONObject;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Aspect
@Component
@Order(100)
public class RestActionInterceptor {
    private Log logger = LogFactory.getLog(this.getClass());

    public RestActionInterceptor() {
    }

    @Pointcut("execution(* com..*.*(..)) && ( @annotation(org.springframework.web.bind.annotation.RequestMapping) || @annotation(org.springframework.web.bind.annotation.GetMapping)   || @annotation(org.springframework.web.bind.annotation.PostMapping) || @annotation(org.springframework.web.bind.annotation.PutMapping)  || @annotation(org.springframework.web.bind.annotation.DeleteMapping) || @annotation(org.springframework.web.bind.annotation.PatchMapping))")
    private void anyMethod() {
    }

    @Around("anyMethod()")
    public Object doBasicProfiling(ProceedingJoinPoint joinPoint) throws Throwable {
        String action = joinPoint.getSignature().toLongString();
        Object[] args = joinPoint.getArgs();
        StringBuilder argsBuilder = new StringBuilder();
        Exception exception = null;
        StringBuilder logBuilder = new StringBuilder();
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        String appVersion = request.getHeader("APPVersion");
        String deviceModel = request.getHeader("DeviceModel");
        String deviceResolution = request.getHeader("DeviceResolution");
        String sysVersion = request.getHeader("SysVersion");
        String channel = request.getHeader("channel");
        String channelNo = request.getHeader("channel_no");
        logBuilder.append("--APPVersion:").append(appVersion).append("--DeviceModel:").append(deviceModel).append("--DeviceResolution:").append(deviceResolution).append("--SysVersion:").append(sysVersion).append("--channel:").append(channel).append("--channel_no:").append(channelNo).append("--servletPath:").append(request.getServletPath()).append("--methodInfo:").append(action);
        long startTime = System.currentTimeMillis();
        String retStr;
        if (args.length > 0) {
            if (args[0] instanceof Map) {
                retStr = this.getServletPath(request);
                Map argsMap = this.skipServletPathKeys(retStr, "faceCheckByDataPackage", (Map)args[0], Arrays.asList("dataPackage"));
                argsMap = this.skipServletPathKeys(retStr, "faceCheckByDataType", argsMap, Arrays.asList("fileList", "dataPackage", "delta"));
                argsMap = this.skipServletPathKeys(retStr, "merchFaceCheckByDataType", argsMap, Arrays.asList("fileList", "delta"));
                argsMap = this.skipServletPathKeys(retStr, "getOCRCertInfo", argsMap, Arrays.asList("data"));
                argsBuilder.append(new JSONObject(argsMap));
            } else {
                argsBuilder.append(args[0]);
            }
        }

        for(int i = 1; i < args.length; ++i) {
            argsBuilder.append(", ").append(args[i]);
        }

        Object result;
        try {
            result = joinPoint.proceed();
        } catch (BusinessException var19) {
            exception = var19;
            result = RestUtil.fail(var19.getRetFlag(), var19.getRetMsg(), var19.getBody());
        } catch (Exception var20) {
            exception = var20;
            result = RestUtil.fail(RestUtil.ERROR_INTERNAL_CODE, RestUtil.ERROR_INTERNAL_MSG);
        }

        retStr = JSONObject.valueToString(result);
        logBuilder.append(" args(").append(argsBuilder).append(")").append(" return(").append(retStr).append(")");
        if (exception != null) {
            logBuilder.append(" EXCEPTION:").append(ThrowableUtils.getString((Throwable)exception)).append("\n");
        }

        Long timeSpan = System.currentTimeMillis() - startTime;
        logBuilder.append(" TOOK:").append(timeSpan).append(" ms.");
        if (exception == null && timeSpan <= 1000L) {
            this.logger.info(logBuilder.toString());
        } else {
            this.logger.warn(logBuilder.toString());
        }

        return result;
    }

    public String getServletPath(HttpServletRequest request) {
        String[] servletPaths = request.getServletPath().split("/");
        return servletPaths[servletPaths.length - 1];
    }

    public Map skipServletPathKeys(String curServletPath, String skipServletPath, Map map, List<String> skipKeys) {
        if (!StringUtils.isEmpty(skipServletPath) && curServletPath.equals(skipServletPath)) {
            Map<String, Object> resultMap = new HashMap();
            resultMap.putAll(map);
            resultMap.forEach((key, value) -> {
                if (skipKeys.contains(key)) {
                    resultMap.put(key, "**");
                }

            });
            return resultMap;
        } else {
            return map;
        }
    }
}

 

日志相关

原文:https://www.cnblogs.com/lu51211314/p/10418713.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!