前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot aop 自定义注解方式实现一套完善的日志记录

springboot aop 自定义注解方式实现一套完善的日志记录

作者头像
java架构师
发布2019-03-01 15:44:46
8390
发布2019-03-01 15:44:46
举报
文章被收录于专栏:Java架构师进阶Java架构师进阶

一:功能简介

本文主要记录如何使用aop切面的方式来实现日志记录功能。

主要记录的信息有: 操作人,方法名,参数,运行时间,操作类型(增删改查),详细描述,返回值。

二:项目结构图

三:代码实现

1.配置文件

这里只有两个配置:1)server.port=11000,设置项目启动的端口号,防止被其他服务占用;2)spring.aop.auto=true,开启spring的aop配置,简单明了,不需要多配置其他的配置或注解。application.yml文件server: port:11000spring: aop: auto:true#启动aop配置

2.AOP切点类

这个是最主要的类,可以使用自定义注解或针对包名实现AOP增强。

1)这里实现了对自定义注解的环绕增强切点,对使用了自定义注解的方法进行AOP切面处理;

2)对方法运行时间进行监控;

3)对方法名,参数名,参数值,对日志描述的优化处理;

在方法上增加@Aspect 注解声明切面,使用@Pointcut 注解定义切点,标记方法。

使用切点增强的时机注解:@Before,@Around,@AfterReturning,@AfterThrowing,@After

packagecom.wwj.springboot.aop;importcom.alibaba.fastjson.JSON;importcom.wwj.springboot.annotation.OperationLogDetail;importcom.wwj.springboot.model.OperationLog;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.*;importorg.aspectj.lang.reflect.MethodSignature;importorg.springframework.stereotype.Component;importjava.util.Date;importjava.util.HashMap;importjava.util.Map;importjava.util.UUID;/**
 * Created by IntelliJ IDEA
 *
 * @author weiwenjun
 * @date 2018/9/12
 */@Aspect@Componentpublicclass LogAspect {/**
 * 此处的切点是注解的方式,也可以用包名的方式达到相同的效果
 * '@Pointcut("execution(* com.wwj.springboot.service.impl.*.*(..))")'
 */@Pointcut("@annotation(com.wwj.springboot.annotation.OperationLogDetail)")publicvoidoperationLog(){}/**
 * 环绕增强,相当于MethodInterceptor
 */@Around("operationLog()")publicObjectdoAround(ProceedingJoinPoint joinPoint)throwsThrowable {Objectres =null;longtime = System.currentTimeMillis();try{ res = joinPoint.proceed(); time = System.currentTimeMillis() - time;returnres; }finally{try{//方法执行完成后增加日志addOperationLog(joinPoint,res,time); }catch(Exception e){ System.out.println("LogAspect 操作失败:"+ e.getMessage()); e.printStackTrace(); } } }privatevoidaddOperationLog(JoinPoint joinPoint,Objectres,longtime){ MethodSignature signature = (MethodSignature)joinPoint.getSignature(); OperationLog operationLog =newOperationLog(); operationLog.setRunTime(time); operationLog.setReturnValue(JSON.toJSONString(res)); operationLog.setId(UUID.randomUUID().toString()); operationLog.setArgs(JSON.toJSONString(joinPoint.getArgs())); operationLog.setCreateTime(newDate()); operationLog.setMethod(signature.getDeclaringTypeName() +"."+ signature.getName()); operationLog.setUserId("#{currentUserId}"); operationLog.setUserName("#{currentUserName}"); OperationLogDetail annotation = signature.getMethod().getAnnotation(OperationLogDetail.class);if(annotation !=null){ operationLog.setLevel(annotation.level()); operationLog.setDescribe(getDetail(((MethodSignature)joinPoint.getSignature()).getParameterNames(),joinPoint.getArgs(),annotation)); operationLog.setOperationType(annotation.operationType().getValue()); operationLog.setOperationUnit(annotation.operationUnit().getValue()); }//TODO 这里保存日志System.out.println("记录日志:"+ operationLog.toString());// operationLogService.insert(operationLog);}/**
 * 对当前登录用户和占位符处理
 * @param argNames 方法参数名称数组
 * @param args 方法参数数组
 * @param annotation 注解信息
 * @return 返回处理后的描述
 */privateStringgetDetail(String[] argNames,Object[] args, OperationLogDetail annotation){ Mapmap=newHashMap<>(4);for(inti =0;i < argNames.length;i++){map.put(argNames[i],args[i]); }Stringdetail = annotation.detail();try{ detail ="'"+"#{currentUserName}"+"'=》"+ annotation.detail();for(Map.Entry entry :map.entrySet()) {Objectk = entry.getKey();Objectv = entry.getValue(); detail = detail.replace("{{"+ k +"}}", JSON.toJSONString(v)); } }catch(Exception e){ e.printStackTrace(); }returndetail; } @Before("operationLog()")publicvoiddoBeforeAdvice(JoinPoint joinPoint){ System.out.println("进入方法前执行....."); }/**
 * 处理完请求,返回内容
 * @param ret
 */@AfterReturning(returning ="ret", pointcut ="operationLog()")publicvoiddoAfterReturning(Objectret) { System.out.println("方法的返回值 : "+ ret); }/**
 * 后置异常通知
 */@AfterThrowing("operationLog()")publicvoidthrowss(JoinPoint jp){ System.out.println("方法异常时执行....."); }/**
 * 后置最终通知,final增强,不管是抛出异常或者正常退出都会执行
 */@After("operationLog()")publicvoidafter(JoinPoint jp){ System.out.println("方法最后执行....."); }}

3.自定义注解

package com.wwj.springboot.annotation;import com.wwj.springboot.enums.OperationType;import com.wwj.springboot.enums.OperationUnit;import java.lang.annotation.*;/**
 * Created by IntelliJ IDEA
 *
 * @author weiwenjun
 * @date 2018/9/12*/
//@OperationLogDetail(detail = "通过手机号[{{tel}}]获取用户名",level = 3,operationUnit = OperationUnit.USER,operationType = OperationType.SELECT)
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface OperationLogDetail {
 /** * 方法描述,可使用占位符获取参数:{{tel}}*/
 String detail() default "";
 /** * 日志等级:自己定,此处分为1-9*/
 int level() default 0;
 /** * 操作类型(enum):主要是select,insert,update,delete*/
 OperationType operationType() default OperationType.UNKNOWN;
 /** * 被操作的对象(此处使用enum):可以是任何对象,如表名(user),或者是工具(redis)*/
 OperationUnit operationUnit() default OperationUnit.UNKNOWN;
}

4.注解用到的枚举类型

package com.wwj.springboot.enums;/**
 * Created by IntelliJ IDEA
 *
 * @author weiwenjun
 * @date 2018/9/12
 */publicenumOperationType {/**
 * 操作类型
 */UNKNOWN("unknown"), DELETE("delete"), SELECT("select"), UPDATE("update"), INSERT("insert");privateStringvalue;publicStringgetValue(){returnvalue; }publicvoidsetValue(Stringvalue){this.value=value; } OperationType(String s) {this.value= s; }}package com.wwj.springboot.enums;/**
 * Created by IntelliJ IDEA
 * 被操作的单元
 * @author weiwenjun
 * @date 2018/9/12
 */publicenumOperationUnit {/**
 * 被操作的单元
 */UNKNOWN("unknown"), USER("user"), EMPLOYEE("employee"), Redis("redis");privateStringvalue; OperationUnit(Stringvalue) {this.value=value; }publicStringgetValue(){returnvalue; }publicvoidsetValue(Stringvalue){this.value=value; }}

5.日志记录对象

package com.wwj.springboot.model;importjava.util.Date;/**
 * Created by IntelliJ IDEA
 *
 * @author weiwenjun
 * @date 2018/9/12
 */publicclassOperationLog {privateStringid;privateDatecreateTime;/**
 * 日志等级
 */privateInteger level;/**
 * 被操作的对象
 */privateStringoperationUnit;/**
 * 方法名
 */privateStringmethod;/**
 * 参数
 */privateStringargs;/**
 * 操作人id
 */privateStringuserId;/**
 * 操作人
 */privateStringuserName;/**
 * 日志描述
 */privateStringdescribe;/**
 * 操作类型
 */privateStringoperationType;/**
 * 方法运行时间
 */privateLong runTime;/**
 * 方法返回值
 */privateStringreturnValue;@OverridepublicStringtoString() {return"OperationLog{"+"id='"+ id +'\''+", createTime="+ createTime +", level="+ level +", operationUnit='"+ operationUnit +'\''+", method='"+ method +'\''+", args='"+ args +'\''+", userId='"+ userId +'\''+", userName='"+ userName +'\''+", describe='"+ describe +'\''+", operationType='"+ operationType +'\''+", runTime="+ runTime +", returnValue='"+ returnValue +'\''+'}'; }publicLong getRunTime() {returnrunTime; }publicvoidsetRunTime(Long runTime) {this.runTime = runTime; }publicStringgetReturnValue() {returnreturnValue; }publicvoidsetReturnValue(StringreturnValue) {this.returnValue = returnValue; }publicStringgetId() {returnid; }publicvoidsetId(Stringid) {this.id = id; }publicDategetCreateTime() {returncreateTime; }publicvoidsetCreateTime(DatecreateTime) {this.createTime = createTime; }publicInteger getLevel() {returnlevel; }publicvoidsetLevel(Integer level) {this.level = level; }publicStringgetOperationUnit() {returnoperationUnit; }publicvoidsetOperationUnit(StringoperationUnit) {this.operationUnit = operationUnit; }publicStringgetMethod() {returnmethod; }publicvoidsetMethod(Stringmethod) {this.method = method; }publicStringgetArgs() {returnargs; }publicvoidsetArgs(Stringargs) {this.args = args; }publicStringgetUserId() {returnuserId; }publicvoidsetUserId(StringuserId) {this.userId = userId; }publicStringgetUserName() {returnuserName; }publicvoidsetUserName(StringuserName) {this.userName = userName; }publicStringgetDescribe() {returndescribe; }publicvoidsetDescribe(Stringdescribe) {this.describe = describe; }publicStringgetOperationType() {returnoperationType; }publicvoidsetOperationType(StringoperationType) {this.operationType = operationType; }}

6.springboot启动类

packagecom.wwj.springboot;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;/** * Created by IntelliJ IDEA * *@authorweiwenjun *@date2018/9/12 */@SpringBootApplicationpublicclassAopApplication{publicstaticvoidmain(String[] args) { SpringApplication.run(AopApplication.class); }}

7.controller类

packagecom.wwj.springboot.controller;importcom.wwj.springboot.service.UserService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.ResponseBody;/**
 * Created by IntelliJ IDEA
 *
 * @author weiwenjun
 * @date 2018/9/12
 */@Controller@RequestMapping("user")public class UserController { @Autowiredprivate UserService userService;/**
 * 访问路径 http://localhost:11000/user/findUserNameByTel?tel=1234567
 * @param tel 手机号
 * @return userName
 */@ResponseBody@RequestMapping("/findUserNameByTel") public String findUserNameByTel(@RequestParam("tel") String tel){returnuserService.findUserName(tel); }}

8.Service类

packagecom.wwj.springboot.service;/** * Created by IntelliJ IDEA * *@authorweiwenjun *@date2018/9/13 */publicinterfaceUserService{/** * 获取用户信息 *@return*@paramtel */StringfindUserName(String tel);}

9.ServiceImpl类

packagecom.wwj.springboot.service.impl;importcom.wwj.springboot.annotation.OperationLogDetail;importcom.wwj.springboot.enums.OperationType;importcom.wwj.springboot.enums.OperationUnit;importcom.wwj.springboot.service.UserService;importorg.springframework.stereotype.Service;/**
 * Created by IntelliJ IDEA
 *
 * @author weiwenjun
 * @date 2018/9/13
 */@Servicepublic class UserServiceImpl implements UserService { @OperationLogDetail(detail="通过手机号[{{tel}}]获取用户名",level =3,operationUnit = OperationUnit.USER,operationType = OperationType.SELECT) @Override public String findUserName(String tel) {System.out.println("tel:"+tel);return"zhangsan"; }}

四:MAVEM依赖

本项目有两个pom文件,父类的pom文件主要作用是对子类pom文件依赖的版本号进行统一管理。

1.最外层的pom文件配置如下

<?xml version="1.0"encoding="UTF-8"?>4.0.0com.wwj.springbootspringbootpom1.0-SNAPSHOTspringboot-aop<!-- Inherit defaults from Spring Boot -->org.springframework.bootspring-boot-starter-parent2.0.4.RELEASE1.2.49<!-- Import dependency management from Spring Boot -->org.springframework.bootspring-boot-dependencies2.0.4.RELEASEpomimportcom.alibabafastjson${fastjson.version}

2.子pom文件配置如下

<?xml version="1.0"encoding="UTF-8"?>springbootcom.wwj.springboot1.0-SNAPSHOT4.0.0springboot-aoporg.springframework.bootspring-boot-starter-web<!-- spring-boot aop依赖配置引入 -->org.springframework.bootspring-boot-starter-aopcom.alibabafastjson

五:运行结果

进入方法前执行.....tel:1234567记录日志:OperationLog{id='cd4b5ba7-7580-4989-a75e-51703f0dfbfc', createTime=Fri Sep1408:54:55CST2018, level=3, operationUnit='user', method='com.wwj.springboot.service.impl.UserServiceImpl.findUserName', args='["1234567"]', userId='#{currentUserId}', userName='#{currentUserName}', describe=''#{currentUserName}'=》通过手机号["1234567"]获取用户名', operationType='select', runTime=4, returnValue='"zhangsan"'}方法最后执行.....方法的返回值 : zhangsan
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.01.30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档