Spring AOP 实现日志记录

/**
 * 通过Aop记录日志
 */
@Aspect
@Component
public class LogRecordAop {

    @Autowired
    private ISysLogService logService;

    private long startTimeMillis = 0;
    private long endTimeMillis = 0;

    //函数执行前
    @Before(value = "( execution(* com.spring.admin.controller.*.*.*.*(..))" +
            "|| execution(* com.spring.admin.controller.*.*.*(..)) )")
    public void before(JoinPoint joinPoint) {
        startTimeMillis = System.currentTimeMillis();
    }

    //函数返回数据后
    @AfterReturning(returning = "retVal", pointcut = "( execution(* com.spring.admin.controller.*.*.*.*(..))" +
            "|| execution(* com.spring.admin.controller.*.*.*(..)) )")
    public void afterReturning(JoinPoint joinPoint, Object retVal) {

        try {
            String className = joinPoint.getTarget().getClass().getName();
            String methodName = joinPoint.getSignature().getName();

            if (StringUtils.isNotBlank(className) || StringUtils.isNotBlank(methodName)) {
                className = className.substring(className.lastIndexOf(".") + 1);

                String operaType = "SELECT";
                if (methodName.toLowerCase().indexOf("select") != -1) {
                    operaType = "SELECT";
                } else if (methodName.toLowerCase().indexOf("insert") != -1) {
                    operaType = "INSERT";
                } else if (methodName.toLowerCase().indexOf("update") != -1) {
                    operaType = "UPDATE";
                } else if (methodName.toLowerCase().indexOf("delete") != -1) {
                    operaType = "DELETE";
                } else if (methodName.toLowerCase().indexOf("login") != -1) {
                    operaType = "LOGIN";
                } else if (methodName.toLowerCase().indexOf("send") != -1) {
                    operaType = "SEND";
                }

                HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                        .getRequestAttributes()).getRequest();
                String requestURL = request.getRequestURL().toString();
                String requestType = request.getMethod();
                String queryString = "";
                Enumeration enu = request.getParameterNames();
                while (enu.hasMoreElements()) {
                    String paraName = (String) enu.nextElement();
                    String paramValue = request.getParameter(paraName);
                    queryString = queryString + paraName + "=" + paramValue + "&";
                }
                if (StringUtils.isNotBlank(queryString)) {
                    queryString = queryString.substring(0, queryString.length() - 1);
                }

                String token = "NULL";
                String userType = "WEB";
                if (requestURL.toLowerCase().indexOf("/admin/") != -1) {
                    token = request.getHeader("Authorization");
                    userType = "WEB";
                } else if (requestURL.toLowerCase().indexOf("/api/") != -1) {
                    token = "API";
                    userType = "API";
                } else if (requestURL.toLowerCase().indexOf("/app/") != -1) {
                    token = request.getParameter("token");
                    userType = "APP";
                } else if (requestURL.toLowerCase().indexOf("/mp/") != -1) {
                    token = request.getParameter("openId");
                    userType = "WEIXIN";
                }

                TipDto td = (TipDto) retVal;
                String message = null;
                String devMessage = null;
                String status = "0";
                String result = null;
                if (td != null) {
                    message = td.getMessage();
                    devMessage = td.getDevMessage();
                    status = String.valueOf(td.getStatus());
                    if (!"SELECT".equals(operaType)) {
                        result = JSON.toJSON(td).toString();
                    } else {
                        try {
                            Map<String, Object> dataMap = (Map<String, Object>) ((JSONObject) JSON.toJSON(td)).get("data");
                            if (dataMap == null || dataMap.size() == 0) {
                                result = "NULL";
                            } else {
                                List<Map<String, Object>> list = (List<Map<String, Object>>) dataMap.get("list");
                                if (list == null || list.size() == 0) {
                                    result = JSON.toJSON(td).toString();
                                } else {
                                    result = "NULL";
                                }
                            }
                        } catch (Exception e) {

                        }
                    }
                }

                endTimeMillis = System.currentTimeMillis();
                Long runTime = endTimeMillis - startTimeMillis;
                Map<String, Object> logMap = new HashMap<>();
                logMap.put("token", StringUtils.isNotBlank(token) ? token : "NULL");
                logMap.put("userType", StringUtils.isNotBlank(userType) ? userType : "NULL");
                logMap.put("requestType", StringUtils.isNotBlank(requestType) ? requestType : "NULL");
                logMap.put("requestURL", StringUtils.isNotBlank(requestURL) ? requestURL : "NULL");
                logMap.put("className", StringUtils.isNotBlank(className) ? className : "NULL");
                logMap.put("methodName", StringUtils.isNotBlank(methodName) ? methodName : "NULL");
                logMap.put("operaType", StringUtils.isNotBlank(operaType) ? operaType : "NULL");
                logMap.put("param", StringUtils.isNotBlank(queryString) ? queryString : "NULL");
                logMap.put("runTime", StringUtils.isNotBlank(runTime.toString()) ? runTime : "NULL");
                logMap.put("status", StringUtils.isNotBlank(status) ? status : "NULL");
                logMap.put("message", StringUtils.isNotBlank(message) ? message : "NULL");
                logMap.put("devMessage", StringUtils.isNotBlank(devMessage) ? devMessage : "NULL");
                logMap.put("result", StringUtils.isNotBlank(result) ? result : "NULL");
                logMap.put("logDate", DateUtil.getTime());
                logMap.put("remark", "NULL");
                this.logService.insertLog(logMap);
            }

        } catch (Exception e) {
        }

        return;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值