springboot 前后端处理日志

server/2024/12/22 1:49:53/

为了实现一个高效且合理的日志记录方案,我们需要在系统架构层面进行细致规划。在某些情况下,一个前端页面可能会调用多个辅助接口来完成整个业务流程,而并非所有这些接口的交互都需要被记录到日志中。为了避免不必要的日志开销,并确保日志系统的可读性和有效性,我们可以采用以下策略:

日志记录策略概述
前端控制:
前端在发起请求时,可以通过HTTP头(Header)传递一个标志,指示当前请求是否需要记录日志。例如,可以使用一个自定义的Header,如operation-log,其值可以是true或false。
后端响应:
后端服务接收到请求后,首先检查该Header的存在及值。如果该Header存在且其值为true,则后端将记录此次请求的相关信息;反之,则忽略日志记录。
下面贴出核心代码
注解

import java.lang.annotation.*;@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Loggable {// 接口信息String value() default "";// 是否记录响应结果,默认为trueboolean logResponse() default true;// 模块String module() default "";// 类型 insert 新增 delete 删除  select 查看 update 修改String type() default "";}

配置信息

import cn.com.nmd.base.constant.Constants;
import cn.hutool.core.util.IdUtil;
import org.slf4j.MDC;
import org.springframework.web.filter.OncePerRequestFilter;import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;public class TraceIdFilter extends OncePerRequestFilter {@Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)throws ServletException, IOException {String traceId = IdUtil.objectId();MDC.put(Constants.TRACE_ID, traceId);try {// 响应头中添加 traceId 参数,方便排查问题response.setHeader(Constants.TRACE_ID, traceId);filterChain.doFilter(request, response);}finally {MDC.remove(Constants.TRACE_ID);}}}
import cn.com.nmd.auth.log.mdc.TraceIdFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;@Configuration
@ConditionalOnWebApplication
public class TraceIdAutoConfiguration {@Beanpublic FilterRegistrationBean<TraceIdFilter> traceIdFilterRegistrationBean() {FilterRegistrationBean<TraceIdFilter> registrationBean = new FilterRegistrationBean<>(new TraceIdFilter());registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);return registrationBean;}}

工具类

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;public class WebUtils extends org.springframework.web.util.WebUtils {/*** 获取 ServletRequestAttributes* @return {ServletRequestAttributes}*/public static ServletRequestAttributes getServletRequestAttributes() {return (ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes());}/*** 获取 HttpServletRequest* @return {HttpServletRequest}*/public static HttpServletRequest getRequest() {return getServletRequestAttributes().getRequest();}/*** 获取 HttpServletResponse* @return {HttpServletResponse}*/public static HttpServletResponse getResponse() {return getServletRequestAttributes().getResponse();}}

拦截器

import cn.com.nmd.auth.annotation.Loggable;
import cn.com.nmd.auth.utils.SecurityUtils;
import cn.com.nmd.auth.utils.WebUtils;
import cn.com.nmd.base.constant.Constants;
import cn.com.nmd.base.model.SysOperationLog;
import cn.com.nmd.base.utils.IpUtils;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.URLUtil;
import com.alibaba.fastjson.JSON;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.*;@Aspect
@Component
public class LogAspect {private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);private final List<Class<?>> ignoredParamClasses = ListUtil.toList(ServletRequest.class, ServletResponse.class,MultipartFile.class);// 定义切入点,匹配带有 @Loggable 注解的方法@Pointcut("@annotation(cn.com.nmd.auth.annotation.Loggable)")public void loggableMethod() {}@Around("loggableMethod()")public Object aroundLog(ProceedingJoinPoint joinPoint) throws Throwable {// 获取 RequestHttpServletRequest request = WebUtils.getRequest();String operateLog = request.getHeader("operation-log");// 开始时间long startTime = System.currentTimeMillis();SysOperationLog sysOperationLog = null;boolean b = false;if ("true".equals(operateLog)) {// 获取目标方法MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();Loggable loggable = method.getAnnotation(Loggable.class);b = loggable.logResponse();sysOperationLog = this.buildLog(loggable, joinPoint);}Throwable throwable = null;Object result = null;try {result = joinPoint.proceed();return result;} catch (Throwable e) {throwable = e;throw e;} finally {if ("true".equals(operateLog)) {// 操作日志记录处理this.handleLog(startTime, sysOperationLog, throwable, b, result);}}}private void handleLog(long startTime, SysOperationLog sysOperationLog, Throwable throwable, boolean b, Object result) {try {// 结束时间long executionTime = System.currentTimeMillis() - startTime;// 执行时长sysOperationLog.setOperateTime(executionTime);// 执行状态String logStatus = throwable == null ? Constants.SUCCESS : Constants.FAIL;sysOperationLog.setStatus(logStatus);// 执行结果if (b) {Optional.ofNullable(result).ifPresent(x -> sysOperationLog.setResult(JSON.toJSONString(x)));}else {sysOperationLog.setResult(("{\"code\": \"200\",\"message\": \"\",\"data\": \"\"}"));}// 保存操作日志logger.info("保存的日志数据:{}", JSON.toJSONString(sysOperationLog));} catch (Exception e) {logger.error("记录操作日志异常:{}", JSON.toJSONString(sysOperationLog));}}private SysOperationLog buildLog(Loggable loggable, ProceedingJoinPoint joinPoint) {HttpServletRequest request = WebUtils.getRequest();SysOperationLog operationLog = new SysOperationLog();operationLog.setIp(IpUtils.getIpAddr(request));operationLog.setMethod(request.getMethod());operationLog.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT));operationLog.setUri(URLUtil.getPath(request.getRequestURI()));operationLog.setType(loggable.type());operationLog.setModule(loggable.module());operationLog.setTraceId(MDC.get(Constants.TRACE_ID));operationLog.setCreateTime(new Date());operationLog.setParams(getParams(joinPoint));operationLog.setCreateUserId(SecurityUtils.getUserId());return operationLog;}/*** 获取方法参数** @param joinPoint 切点* @return 当前方法入参的Json Str*/public String getParams(ProceedingJoinPoint joinPoint) {// 获取方法签名Signature signature = joinPoint.getSignature();String strClassName = joinPoint.getTarget().getClass().getName();String strMethodName = signature.getName();MethodSignature methodSignature = (MethodSignature) signature;logger.debug("[getParams],获取方法参数[类名]:{},[方法]:{}", strClassName, strMethodName);String[] parameterNames = methodSignature.getParameterNames();Object[] args = joinPoint.getArgs();Method method = ((MethodSignature) signature).getMethod();if (ArrayUtil.isEmpty(parameterNames)) {return null;}Map<String, Object> paramsMap = new HashMap<>();for (int i = 0; i < parameterNames.length; i++) {Object arg = args[i];if (arg == null) {paramsMap.put(parameterNames[i], null);continue;}Class<?> argClass = arg.getClass();// 忽略部分类型的参数记录for (Class<?> ignoredParamClass : ignoredParamClasses) {if (ignoredParamClass.isAssignableFrom(argClass)) {arg = "ignored param type: " + argClass;break;}}paramsMap.put(parameterNames[i], arg);}// 特别处理 @RequestBody 参数for (int i = 0; i < parameterNames.length; i++) {Object arg = args[i];if (arg != null && method.getParameterAnnotations()[i].length > 0) {RequestBody requestBodyAnnotation = method.getParameterAnnotations()[i][0].annotationType().getAnnotation(RequestBody.class);if (requestBodyAnnotation != null) {paramsMap.put(parameterNames[i], arg);}}}String params = "";try {// 入参类中的属性可以通过注解进行数据落库脱敏以及忽略等操作params = JSON.toJSONString(paramsMap);} catch (Exception e) {logger.error("[getParams],获取方法参数异常,[类名]:{},[方法]:{}", strClassName, strMethodName, e);}return params;}
}

示例

 	@Loggable(value = "保存采集信息", type = "insert", module = "业务管理-场站管理", logResponse = true)

http://www.ppmy.cn/server/129978.html

相关文章

【Docker】Docker基本操作

目录 一、了解云计算背景 1.1 云计算的三种服务模式 1.2 虚拟机的两种架构 二、Docker 概述 2.1 Docker简述 2.2 Docker 特点 2.3 Docker与虚拟机的区别 2.4 容器技术有哪些 2.4.1 namespace的六项隔离 2.5 Docker核心概念 2.5.1 镜像 2.5.2 容器 2.5.3 仓库 三、…

CSRF | CSRF 漏洞介绍

关注这个漏洞的其他相关笔记&#xff1a;CSRF 漏洞 - 学习手册-CSDN博客 0x01&#xff1a;CSRF 漏洞简介 CSRF&#xff08;Cross-Site request forgery&#xff0c;跨站请求伪造&#xff09;也被称为 One Click Attack 或者 Session Riding&#xff0c;通常缩写为 CSRF 或者 X…

【STM32】STM32CubeMX 之 GPIO配置 【笔记】

环境 硬件&#xff1a;通用PC 系统&#xff1a; Windows 10 软件 &#xff1a;STM32CubeMX 在STM32CubeMX的GPIO配置中&#xff0c;每个选项都有特定的含义。以下是逐一解释这些选项&#xff1a; 1.GPIO output level (仅适用于输出模式): Low: 初始输出状态为低电平High: …

开源全文搜索(搜索引擎)

吃水不忘挖井人&#xff0c;介绍Doug Cutting大牛是十分有必要的。 最早&#xff0c;接触到搜索引擎&#xff0c;知道有个Nutch&#xff08;开源搜索引擎&#xff09;&#xff0c;于是开始查看Nutch相关的资料&#xff0c;发现了Nutch的创始人Doug Cutting&#xff0c;随着项目…

数组与链表的特点、细节及其原理研究

目录 第一章 数组的基本概念与原理 1.1 数组的定义与特性 1.1.1 数组的连续性 1.1.2 数组的固定大小性 1.2 数组的存储方式 1.3 数组的访问与操作 第二章 链表的基本概念与原理 2.1 链表的定义与特性 2.2 链表的种类 2.2.1 单向链表 2.2.2 双向链表 2.2.3 循环链表…

Java面试题——第十篇

1. 什么是Java的PLAB PLAB是Java垃圾回收器中的一种优化机制&#xff0c;主要用于G1垃圾收集器&#xff0c;目的是提高对象晋升到老年代的效率。 在垃圾回收过程中&#xff0c;新生代中的某些对象由于生命周期较长&#xff0c;会被晋升到老年代。为了减少线程竞争和提升晋升效…

JAVA学习-练习试用Java实现“反转链表 II”

问题&#xff1a; 给定单链表的头指针 head 和两个整数 left 和 right &#xff0c;其中 left < right 。请你反转从位置 left 到位置 right 的链表节点&#xff0c;返回 反转后的链表 。 示例 1&#xff1a; 输入&#xff1a;head [1,2,3,4,5], left 2, right 4 输出…

目录工具类 - C#小函数类推荐

此文记录的是目录工具类。 /***目录工具类Austin Liu 刘恒辉Project Manager and Software DesignerE-Mail: lzhdim163.comBlog: http://lzhdim.cnblogs.comDate: 2024-01-15 15:18:00***/namespace Lzhdim.LPF.Utility {using System.IO;/// <summary>/// The Objec…