封装系统全局操作日志aop拦截且可打包给其他项目依赖
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,封装系统全局操作日志aop拦截且可打包给其他项目依赖,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。在开发过程中,为了更快地排错,更好
千家信息网最后更新 2025年12月03日封装系统全局操作日志aop拦截且可打包给其他项目依赖
封装系统全局操作日志aop拦截且可打包给其他项目依赖,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
在开发过程中,为了更快地排错,更好得了解接口访问量,可以选择用aop做全局的操作拦截,在项目不止一个的时候,我们可以选择独立出来,新项目可以很快的加上全局操作日志,具体代码及数据库表设计如下:
1.数据库MySQL表结构设计,如下图(可根据需要加减字段):
2.可以根据表结构逆向生成相关实体类及Mapper,此步骤相对简单(略)
3.增加全局日志切面类
** * @author songlonghui * @ClassName SystemLogAspect * @Description 日志切面记录 * @date 2019/7/24 16:38 * @Version 1.0 */@Component@Aspectpublic class SystemLogAspect { private Logger logger = LoggerFactory.getLogger(SystemLogAspect.class); @Autowired private SystemLogDao systemLogDao; /*@Value("${LOG_POINT_URL}") private String logPointUrl;*/ /** 以 controller 包下定义的所有请求为切入点 */ @Pointcut("execution(public * com.machinsight.*.*.controller..*.*(..)) && !@annotation(com.machinsight.system_log.core.annotation.NoAspectAnnotation)") public void webLog() { //logger.warn("切点路径---------->" + logPointUrl); } //private SystemLogWithBLOBs systemLogWithBLOBs; /** * 在切点之前织入 * @param joinPoint * @throws Throwable */ @Before("webLog()") public void doBefore(JoinPoint joinPoint) throws Throwable { // 开始打印请求日志 //logger.info("=========================================== Start ==========================================="); } /** * 在切点之后织入 * @throws Throwable */ @After("webLog()") public void doAfter() throws Throwable { logger.info("=========================================== End ==========================================="); // 每个请求之间空一行 logger.info(""); } /** * 环绕 * @param proceedingJoinPoint * @return * @throws Throwable */ @Around("webLog()") public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { // 开始时间 long startTime = System.currentTimeMillis(); SystemLogWithBLOBs systemLogWithBLOBs = new SystemLogWithBLOBs(); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if (null != attributes){ HttpServletRequest request = attributes.getRequest(); logger.error("当前线程号:--doAround--" + Thread.currentThread()); String url = request.getRequestURL().toString(); String description = proceedingJoinPoint.getSignature().getDeclaringTypeName(); String requestArgs = FastJsonUtils.toJSONNoFeatures(proceedingJoinPoint.getArgs()).replaceAll("\\\\",""); String ip = UuidUtil.getIpAddr(request); // 打印请求相关参数 logger.info("========================================== Start =========================================="); // 打印请求 url logger.info("请求URL : {}", url); // 打印 Http method logger.info("HTTP请求方法 Method : {}", request.getMethod()); // 打印调用 controller 的全路径以及执行方法 logger.info("Class--Controller 全路径以及执行方法 Method : {}.{}", description); // 打印请求的 IP logger.info("请求IP : {}", ip); // 打印请求入参 logger.info("请求参数Request Args : {}", requestArgs); // 记录日志入库 String value = request.getHeader("user-agent"); // 用户代理信息 // systemLogWithBLOBs.setCreateTime(new Date()); // 请求参数 systemLogWithBLOBs.setMethod(url); // 接口地址 systemLogWithBLOBs.setRequestIp(ip); //请求ip systemLogWithBLOBs.setRequestArgs(requestArgs); // 请求参数 systemLogWithBLOBs.setUserAgent(value); // 用户代理信息 systemLogWithBLOBs.setDescription(description); } Object result = null; try { result = proceedingJoinPoint.proceed(); String responseArgs = FastJsonUtils.toJSONNoFeatures(result).replaceAll("\\\\",""); long useTime = System.currentTimeMillis() - startTime; // 打印出参 logger.info("具体返回参数 Response Args : {}", responseArgs); // 执行耗时 logger.info("整体执行耗时 Time-Consuming : {} ms", useTime); systemLogWithBLOBs.setResponseArgs(responseArgs); systemLogWithBLOBs.setTimeConsuming(Integer.valueOf(String.valueOf(useTime))); } catch (Throwable throwable) { // 设置异常信息 systemLogWithBLOBs.setIsException(1); // 异常信息 systemLogWithBLOBs.setExceptionDetail(throwable.getMessage()); // 耗时 long exceptionTime = System.currentTimeMillis() - startTime; systemLogWithBLOBs.setTimeConsuming(Integer.valueOf(String.valueOf(exceptionTime))); // 异常返回参数 JsonResult jsonResult = new JsonResult(); jsonResult.setMsg(CrmConstant.OPS_FAILED_MSG); jsonResult.setSuccess(CrmConstant.OPS_FAILED_CODE); jsonResult.setData(throwable.getMessage()); String responseArgsForException = FastJsonUtils.toJSONNoFeatures(jsonResult); systemLogWithBLOBs.setResponseArgs(responseArgsForException); // 抛出异常 throw new Throwable(throwable.getMessage()); } finally { systemLogDao.insertSelective(systemLogWithBLOBs); } return result; }4.可根据公司项目的整体包结构配置切点,还可以增加不需要拦截的配置,为了更灵活细化,这里增加了相应注解类,如下:
/** * 切面排除注解类 */@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface NoAspectAnnotation {}5.只要在不需要的拦截的controller方法加上注解@NoAspectAnnotation即可,切点会自动排除:
/** * @author songlonghui * @ClassName CommonController * @Description TODO * @date 2019/8/23 16:09 * @Version 1.0 */@Controller@RequestMapping("common")public class CommonController { @ResponseBody @RequestMapping("/test") @NoAspectAnnotation public String testLog(@RequestBody String inputJson) throws Exception{ String msg = "操作成功"; return msg; }}6.只要在别的项目中增加该项目依赖即可:
com.machinsight system_log 0.0.1-SNAPSHOT * *
有需要整体项目源码的朋友可以联系我,现在还没有想好源码放在什么地方供别人下载!!
邮箱地址:lance911215@outlook.com
关于封装系统全局操作日志aop拦截且可打包给其他项目依赖问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。
日志
项目
参数
全局
切点
方法
信息
切面
整体
注解
结构
路径
问题
系统
封装
地址
接口
数据
数据库
更多
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技公司是怎样估值
网络安全微课策划案
qq数据库 txt文件
django 远程数据库
云南服务器回收
平谷旧服务器回收报价
自己能做数据库吗
数据库维度怎么算
网络安全审计硬件生产厂家
中国数据库检索结果包括哪些内容
wdcp数据库地址
企业软件开发软件工具
东城区综合软件开发大概费用
软件开发人员组合
我们的dna是如何录入数据库的
嵌入式软件开发评估
深圳摩科时代软件开发有限公司
tp link虚拟服务器
阎良软件开发
数据库高级入门教程
sql数据库的外键是什么
服务器x3550m5开机不了
华为服务器和戴尔服务器哪个好
数据库后台调整
网络安全小卫士手抄报初中
大话西游2昆仑天池服务器合服
网络安全科技公司的经营范围
图片视频数据库怎么下载
劳动局计算机网络技术人员
数据库使用排行