java 操作日志设计_AOP统一日志打印处理(系统操作日志通用设计)

本文介绍了如何在SpringBoot应用中使用AOP和Javassist来实现日志的统一管理和优雅输出。通过创建一个名为WebLogAop的切面组件,记录操作日志,包括方法的执行时间、传入参数和返回值。同时,文章展示了如何设计系统操作日志数据库表,并提供了获取方法参数名称的辅助方法。
摘要由CSDN通过智能技术生成

在日常开发工作中,我们免不了要打印很多log。而大部分需要输出的log又是重复的(例如传入参数,返回值)。

因此,通过AOP方式来进行日志管理可以减少很多代码量,也更加优雅。

Springboot通过AOP方式(@Aspect)和Javassist优雅地进行日志输出管理。

CREATE TABLE `sys_operation_log` (

`log_id` varchar(32) NOT NULL DEFAULT '',

`table_name` varchar(50) DEFAULT '' COMMENT '表名',

`biz_name` varchar(50) DEFAULT '' COMMENT '业务名称',

`biz_id` varchar(50) DEFAULT '' COMMENT '业务主键值',

`biz_type` smallint(1) DEFAULT '0' COMMENT '业务类型(1:添加 2:修改 3:删除)',

`create_time` datetime DEFAULT NULL COMMENT '操作时间',

`creator` varchar(50) DEFAULT '' COMMENT '操作人',

`remark` varchar(200) DEFAULT '' COMMENT '备注',

PRIMARY KEY (`log_id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='系统操作日志';

主要使用技术:Aspect,Javassist

package com.xinyartech.erp.system.aop;

import java.lang.reflect.Modifier;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;

import com.xinyartech.erp.core.util.Util;

import javassist.ClassClassPath;

import javassist.ClassPool;

import javassist.CtClass;

import javassist.CtMethod;

import javassist.NotFoundException;

import javassist.bytecode.CodeAttribute;

import javassist.bytecode.LocalVariableAttribute;

import javassist.bytecode.MethodInfo;

/**

* 通过spring aop实现service方法执行时间监控

*

* @author Lynch

*

*/

@Aspect

@Component

public class WebLogAop {

private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(WebLogAop.class);

private static ThreadLocal threadLocal = new ThreadLocal();

//public static final String POINT = "execution (* com.xinyartech.erp.*.web.*.*(..))";

@Pointcut("(execution (* com.xinyartech.erp.*.web.*.*(..)))")

public void webLog(){

}

/**

* 前置通知

* @param joinPoint 切点

* @throws Throwable 异常

*/

@Before("webLog()")

public void doBefore(JoinPoint joinPoint) throws Throwable {

String uuid = Util.getUUID();

threadLocal.set(uuid);

String classType = joinPoint.getTarget().getClass().getName();

Class> clazz = Class.forName(classType);

String clazzName = clazz.getName();

log.info(String.format("[%s] 类名:%s", uuid, clazzName));

String methodName = joinPoint.getSignature().getName();

log.info(String.format("[%s] 方法名:%s", uuid, methodName));

String[] paramNames = getFieldsName(this.getClass(), clazzName, methodName);

Object[] args = joinPoint.getArgs();

for(int k=0; k

log.info("[" + uuid + "] 参数名:" + paramNames[k] + ",参数值:" + JSON.toJSONString(args[k]));

}

}

/**

* 后置通知

* 打印返回值日志

* @param ret 返回值

* @throws Throwable 异常

*/

@AfterReturning(returning = "ret", pointcut = "webLog()")

public void doAfterReturning(JoinPoint joinPoint, Object ret) throws Throwable {

String uuid = threadLocal.get();

String classType = joinPoint.getTarget().getClass().getName();

Class> clazz = Class.forName(classType);

String clazzName = clazz.getName();

log.info(String.format("[%s] 类名:%s", uuid, clazzName));

String methodName = joinPoint.getSignature().getName();

log.info(String.format("[%s] 方法名:%s", uuid, methodName));

log.info(String.format("[%s] 返回值 : %s", uuid, JSON.toJSONString(ret)));

log.info("*****************************************");

}

/**

* 得到方法参数的名称

* @param cls 类

* @param clazzName 类名

* @param methodName 方法名

* @return 参数名数组

* @throws NotFoundException 异常

*/

private static String[] getFieldsName(Class> cls, String clazzName, String methodName) throws NotFoundException {

ClassPool pool = ClassPool.getDefault();

ClassClassPath classPath = new ClassClassPath(cls);

pool.insertClassPath(classPath);

CtClass cc = pool.get(clazzName);

CtMethod cm = cc.getDeclaredMethod(methodName);

MethodInfo methodInfo = cm.getMethodInfo();

CodeAttribute codeAttribute = methodInfo.getCodeAttribute();

LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);

String[] paramNames = new String[cm.getParameterTypes().length];

int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;

for (int i = 0; i < paramNames.length; i++){

paramNames[i] = attr.variableName(i + pos); //paramNames即参数名

}

return paramNames;

}

}

https://www.cnblogs.com/linjiqin/category/283832.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值