• springboot记录操做日志


    1.拦截
    import java.util.Date; import java.util.Enumeration; import javax.servlet.FilterChain; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.multipart.MultipartFile; import com.
    *.reconciliation.common.ThreadUtil; import com.*.SystemOperationLogInfo; import com.*.reconciliation.service.SystemOperationLogService; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @Aspect @Component public class DBLoggerAspect { private ObjectMapper mapper; @Autowired public DBLoggerAspect(ObjectMapper mapper) { this.mapper = mapper; } @Autowired private SystemOperationLogService systemOperationLogService; @Around("@annotation(dbLogger)") public Object exec(ProceedingJoinPoint invocation, DBLogger dbLogger) throws Throwable { ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); HttpServletRequest request = servletRequestAttributes.getRequest(); Object result = null; Date startTime = new Date(); try { result = invocation.proceed(); } catch (Exception e) { throw e; } finally { Object obj = result; Date endTime = new Date(); ThreadUtil.logPool.execute(()->{ HttpSession session = request.getSession(); String userName = (String) session.getAttribute("userName"); StringBuffer str = new StringBuffer(); SystemOperationLogInfo systemOperationLogInfo = new SystemOperationLogInfo(); systemOperationLogInfo.setOperationUser(userName); systemOperationLogInfo.setOperationName(dbLogger.operationName()); systemOperationLogInfo.setRequestUrl(request.getRequestURI()); if(invocation.getArgs() != null) { for (Object object : invocation.getArgs()) { if (object instanceof MultipartFile || object instanceof ServletRequest || object instanceof ServletResponse || object instanceof FilterChain ) { continue; } try { str.append(mapper.writeValueAsString(object)); } catch (JsonProcessingException e) { e.printStackTrace(); } } systemOperationLogInfo.setRequestData(str.toString()); } systemOperationLogInfo.setRequestStartTime(startTime); systemOperationLogInfo.setRequestEndTime(endTime); systemOperationLogInfo.setUsedTime(Integer.parseInt(""+(endTime.getTime() - startTime.getTime()))); JSONObject headerJson = new JSONObject(); Enumeration<String> headers = request.getHeaderNames(); while(headers.hasMoreElements()) { String key = headers.nextElement(); String value = request.getHeader(key); headerJson.put(key, value); } systemOperationLogInfo.setRequestHeader(headerJson.toString()); systemOperationLogInfo.setRequestMethod(request.getMethod()); try { systemOperationLogInfo.setResponseData(mapper.writeValueAsString(obj)); } catch (JsonProcessingException e) { e.printStackTrace(); } systemOperationLogInfo.setSystemName("accountcenter"); systemOperationLogService.save(systemOperationLogInfo); }); } return result; }
    2.
    @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented
    public @interface DBLogger { String operationName(); }
    
    
    3.使用
    @DBLogger(operationName = "*列表查询")
    
    }
    人这辈子没法做太多事情,所以每做一件事都要做到精彩绝伦。 因为,这就是我的宿命。人生苦短,你明白吗? 所以这是我为人生做出的选择
  • 相关阅读:
    L1-045 宇宙无敌大招呼 (5分)
    L1-044 稳赢 (15分)
    L1-043 阅览室 (20分)
    L1-042 日期格式化 (5分)
    L1-041 寻找250 (10分)
    L1-040 最佳情侣身高差 (10分)
    L1-039 古风排版 (20分)
    L1-038 新世界 (5分)
    L1-037 A除以B (10分)
    利用C一种最有效的文件存储方式——16bit有符号位2进制存储
  • 原文地址:https://www.cnblogs.com/junjun1578/p/13602649.html
Copyright © 2020-2023  润新知