栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Aop 实现日志功能

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Aop 实现日志功能

package com.netauth.api.aoplog;

import java.lang.annotation.*;


@Target(ElementType.METHOD) //注解放置的目标位置,METHOD是可注解在方法级别上
@Retention(RetentionPolicy.RUNTIME) //注解在哪个阶段执行
@Documented
public @interface OperLog {

    String operModul() default ""; // 操作模块

    String operType() default "";  // 操作类型

    String operDesc() default "";  // 操作说明
}


package com.netauth.api.aoplog;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSON;
import com.netauth.entity.user.UserLoginBean;
import com.netauth.utils.currentuser.LoginUserUtil;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;


@Aspect
@Component
public class OperLogAspect {

 
 @Pointcut("@annotation(com.netauth.api.aoplog.OperLog)")
 public void operLogPoinCut() {
 }

 
 @Pointcut("execution(* com.infosec.user..*.*(..))")
 public void operExceptionLogPoinCut() {
 }

 
 @AfterReturning(value = "operLogPoinCut()", returning = "keys")
 public void saveOperLog(JoinPoint joinPoint, Object keys) {
     // 获取RequestAttributes
     RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
     // 从获取RequestAttributes中获取HttpServletRequest的信息
     HttpServletRequest request = (HttpServletRequest) requestAttributes
             .resolveReference(RequestAttributes.REFERENCE_REQUEST);

     Map map = new HashMap<>();
     UserLoginBean localUser = LoginUserUtil.getLoginUserBean(request);
     try {
//         map.put("uuid", UUID.randomUUID().toString().replace("-",""));
         // 从切面织入点处通过反射机制获取织入点处的方法
         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
         // 获取切入点所在的方法
         Method method = signature.getMethod();
         // 获取操作
         OperLog opLog = method.getAnnotation(OperLog.class);
         if (opLog != null) {
             String operModul = opLog.operModul();
             String operType = opLog.operType();
             String operDesc = opLog.operDesc();

             map.put("operModul",operModul);// 操作模块
             map.put("operType",operType);// 操作类型
             map.put("operDesc",operDesc);// 操作描述
         }
         // 获取请求的类名
         String className = joinPoint.getTarget().getClass().getName();
         // 获取请求的方法名
         String methodName = method.getName();
         methodName = className + "." + methodName;
         String username = localUser.getLoginName();
         map.put("loginInfo", username);
         map.put("operMethod",methodName);// 请求方法
         // 请求的参数
//         Map rtnMap = converMap(request.getParameterMap());
         // 将参数所在的数组转换成json
//         String params = JSON.toJSONString(rtnMap);
//         map.put("params",params);// 请求参数
         map.put("result",JSON.toJSONString(keys));// 返回结果
         map.put("requestURI",request.getRequestURI());// 请求URI
         //打印结果
         System.out.println("打印操作日志:"+map.toString());
     } catch (Exception e) {
         e.printStackTrace();
     }
 }

 
 @AfterThrowing(pointcut = "operExceptionLogPoinCut()", throwing = "e")
 public void saveExceptionLog(JoinPoint joinPoint, Throwable e) {
     // 获取RequestAttributes
     RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
     // 从获取RequestAttributes中获取HttpServletRequest的信息
     HttpServletRequest request = (HttpServletRequest) requestAttributes
             .resolveReference(RequestAttributes.REFERENCE_REQUEST);

     Map map = new HashMap<>();
     try {
         // 从切面织入点处通过反射机制获取织入点处的方法
         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
         // 获取切入点所在的方法
         Method method = signature.getMethod();
//         map.put("uuid", UUID.randomUUID().toString().replace("-",""));
         // 获取请求的类名
         String className = joinPoint.getTarget().getClass().getName();
         // 获取请求的方法名
         String methodName = method.getName();
         methodName = className + "." + methodName;
         // 请求的参数
         Map rtnMap = converMap(request.getParameterMap());
         // 将参数所在的数组转换成json
         String params = JSON.toJSONString(rtnMap);

//         map.put("params",params);// 请求参数
         map.put("methodName",methodName);// 请求方法名
         map.put("excName",e.getClass().getName());
//         map.put("excMessage",stackTraceToString(e.getClass().getName(), e.getMessage(), e.getStackTrace()));// 异常信息
         map.put("requestURI",request.getRequestURI());// 操作URI
         map.put("excCreateTime",new Date());// 发生异常时间
         
         System.out.println("打印异常日志:"+map.toString());
     } catch (Exception e2) {
         e2.printStackTrace();
     }
 }

 
 public Map converMap(Map paramMap) {
     Map rtnMap = new HashMap();
     for (String key : paramMap.keySet()) {
         rtnMap.put(key, paramMap.get(key)[0]);
     }
     return rtnMap;
 }

 
 public String stackTraceToString(String exceptionName, String exceptionMessage, StackTraceElement[] elements) {
     StringBuffer strbuff = new StringBuffer();
     for (StackTraceElement stet : elements) {
         strbuff.append(stet + "n");
     }
     String message = exceptionName + ":" + exceptionMessage + "nt" + strbuff.toString();
     return message;
 }
}


 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/925054.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号