spring-boot-route(十七)使用aop記錄操作日志


在上一章內容中——使用logback管理日志,我們詳細講述了如何將日志生成文件進行存儲。但是在實際開發中,使用文件存儲日志用來快速查詢問題並不是最方便的,一個優秀系統除了日志文件還需要將操作日志進行持久化,來監控平台的操作記錄。今天我們一起來學習一下如何通過apo來記錄日志。

為了讓記錄日志更加靈活,我們將使用自定義的注解來實現重要操作的日志記錄功能。

一 日志記錄表

日志記錄表主要包含幾個字段,業務模塊,操作類型,接口地址,處理狀態,錯誤信息以及操作時間。數據庫設計如下:

CREATE TABLE `sys_oper_log` (
   `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '日志主鍵',
   `title` varchar(50) CHARACTER SET utf8 DEFAULT '' COMMENT '模塊標題',
   `business_type` int(2) DEFAULT '0' COMMENT '業務類型(0其它 1新增 2修改 3刪除)',
   `method` varchar(255) CHARACTER SET utf8 DEFAULT '' COMMENT '方法名稱',
   `status` int(1) DEFAULT '0' COMMENT '操作狀態(0正常 1異常)',
   `error_msg` varchar(2000) CHARACTER SET utf8 DEFAULT '' COMMENT '錯誤消息',
   `oper_time` datetime DEFAULT NULL COMMENT '操作時間',
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB CHARSET=utf8mb4 CHECKSUM=1 COMMENT='操作日志記錄'

對應的實體類如下:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class SysOperLog implements Serializable {
    private static final long serialVersionUID = 1L;

    /** 日志主鍵 */
    private Long id;

    /** 操作模塊 */
    private String title;

    /** 業務類型(0其它 1新增 2修改 3刪除) */
    private Integer businessType;

    /** 請求方法 */
    private String method;

    /** 錯誤消息 */
    private String errorMsg;

    private Integer status;

    /** 操作時間 */
    private Date operTime;
}

二 自定義注解及處理

自定義注解包含兩個屬性,一個是業務模塊title,另一個是操作類型businessType

@Target({ ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {
    /**
     * 模塊
     */
    String title() default "";

    /**
     * 功能
     */
    BusinessType businessType() default BusinessType.OTHER;
}

使用aop對自定義的注解進行處理

@Aspect
@Component
@Slf4j
public class LogAspect {

    @Autowired
    private AsyncLogService asyncLogService;

    // 配置織入點
    @Pointcut("@annotation(com.javatrip.aop.annotation.Log)")
    public void logPointCut() {}

    /**
     * 處理完請求后執行
     *
     * @param joinPoint 切點
     */
    @AfterReturning(pointcut = "logPointCut()", returning = "jsonResult")
    public void doAfterReturning(JoinPoint joinPoint, Object jsonResult) {
        handleLog(joinPoint, null, jsonResult);
    }

    /**
     * 攔截異常操作
     * 
     * @param joinPoint 切點
     * @param e 異常
     */
    @AfterThrowing(value = "logPointCut()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
        handleLog(joinPoint, e, null);
    }

    protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult) {
        try {
            // 獲得注解
            Log controllerLog = getAnnotationLog(joinPoint);
            if (controllerLog == null) {
                return;
            }

            SysOperLog operLog = new SysOperLog();
            operLog.setStatus(0);
            if (e != null) {
                operLog.setStatus(1);
                operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
            }
            // 設置方法名稱
            String className = joinPoint.getTarget().getClass().getName();
            String methodName = joinPoint.getSignature().getName();
            operLog.setMethod(className + "." + methodName + "()");
            // 處理設置注解上的參數
            getControllerMethodDescription(joinPoint, controllerLog, operLog);
            // 保存數據庫
            asyncLogService.saveSysLog(operLog);
        } catch (Exception exp) {
            log.error("==前置通知異常==");
            log.error("日志異常信息 {}", exp);
        }
    }

    /**
     * 獲取注解中對方法的描述信息 用於Controller層注解
     * 
     * @param log 日志
     * @param operLog 操作日志
     * @throws Exception
     */
    public void getControllerMethodDescription(JoinPoint joinPoint, Log log, SysOperLog operLog) {
        // 設置action動作
        operLog.setBusinessType(log.businessType().ordinal());
        // 設置標題
        operLog.setTitle(log.title());
    }

    /**
     * 是否存在注解,如果存在就獲取
     */
    private Log getAnnotationLog(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method != null) {
            return method.getAnnotation(Log.class);
        }
        return null;
    }
}

操作類型的枚舉類:

public enum BusinessType {
    /**
     * 其它
     */
    OTHER,

    /**
     * 新增
     */
    INSERT,

    /**
     * 修改
     */
    UPDATE,

    /**
     * 刪除
     */
    DELETE,
}

使用異步方法將操作日志存庫,為了方便我直接使用jdbcTemplate在service中進行存庫操作。

@Service
public class AsyncLogService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 保存系統日志記錄
     */
    @Async
    public void saveSysLog(SysOperLog log) {

        String sql = "INSERT INTO sys_oper_log(title,business_type,method,STATUS,error_msg,oper_time) VALUES(?,?,?,?,?,?)";
        jdbcTemplate.update(sql,new Object[]{log.getTitle(),log.getBusinessType(),log.getMethod(),log.getStatus(),log.getErrorMsg(),new Date()});
    }
}

三 編寫接口測試

將自定義注解寫在業務方法上,測試效果

@RestController
@RequestMapping("person")
public class PersonController {
    @GetMapping("/{name}")
    @Log(title = "system",businessType = BusinessType.OTHER)
    public Person getPerson(@PathVariable("name") String name, @RequestParam int age){
        return new Person(name,age);
    }

    @PostMapping("add")
    @Log(title = "system",businessType = BusinessType.INSERT)
    public int addPerson(@RequestBody Person person){
        if(StringUtils.isEmpty(person)){
            return -1;
        }
        return 1;
    }

    @PutMapping("update")
    @Log(title = "system",businessType = BusinessType.UPDATE)
    public int updatePerson(@RequestBody Person person){
        if(StringUtils.isEmpty(person)){
            return -1;
        }
        return 1;
    }

    @DeleteMapping("/{name}")
    @Log(title = "system",businessType = BusinessType.DELETE)
    public int deletePerson(@PathVariable(name = "name") String name){
        if(StringUtils.isEmpty(name)){
            return -1;
        }
        return 1;
    }
}

當然,還可以在數據庫中將請求參數和響應結果也進行存儲,這樣就能看出具體接口的操作記錄了。


本文示例代碼已上傳至github,點個star支持一下!

Spring Boot系列教程目錄

spring-boot-route(一)Controller接收參數的幾種方式

spring-boot-route(二)讀取配置文件的幾種方式

spring-boot-route(三)實現多文件上傳

spring-boot-route(四)全局異常處理

spring-boot-route(五)整合swagger生成接口文檔

spring-boot-route(六)整合JApiDocs生成接口文檔

spring-boot-route(七)整合jdbcTemplate操作數據庫

spring-boot-route(八)整合mybatis操作數據庫

spring-boot-route(九)整合JPA操作數據庫

spring-boot-route(十)多數據源切換

spring-boot-route(十一)數據庫配置信息加密

spring-boot-route(十二)整合redis做為緩存

spring-boot-route(十三)整合RabbitMQ

spring-boot-route(十四)整合Kafka

spring-boot-route(十五)整合RocketMQ

spring-boot-route(十六)使用logback生產日志文件

spring-boot-route(十七)使用aop記錄操作日志

spring-boot-route(十八)spring-boot-adtuator監控應用

spring-boot-route(十九)spring-boot-admin監控服務

spring-boot-route(二十)Spring Task實現簡單定時任務

spring-boot-route(二十一)quartz實現動態定時任務

spring-boot-route(二十二)實現郵件發送功能

spring-boot-route(二十三)開發微信公眾號

spring-boot-route(二十四)分布式session的一致性處理

spring-boot-route(二十五)兩行代碼實現國際化

spring-boot-route(二十六)整合webSocket

這個系列的文章都是工作中頻繁用到的知識,學完這個系列,應付日常開發綽綽有余。如果還想了解其他內容,掃面下方二維碼告訴我,我會進一步完善這個系列的文章!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM