博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot 添加切面,记录日志
阅读量:4098 次
发布时间:2019-05-25

本文共 3158 字,大约阅读时间需要 10 分钟。

文章目录

前言

工作中,使用利用spring aop 切面的方式记录日志

一、引入依赖

org.springframework.boot
spring-boot-starter-aop

二、项目结构

在这里插入图片描述

三、注解类

@Target({
ElementType.PARAMETER, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface AuditLog {
String title() default ""; String desc() default "";}

四、切面类

@Aspect@Componentpublic class AuditLogAspect {
private static final Logger log = LoggerFactory.getLogger("audit_log"); public AuditLogAspect() {
} @Pointcut("@annotation(com.cch.auditlog.common.auditlog.AuditLog)") public void logPointCut() {
} @AfterReturning( pointcut = "logPointCut()" ) public void doAfterReturning(JoinPoint joinPoint) {
this.handleAfterLog(joinPoint); } @Before(value = "logPointCut()") public void doBefore(JoinPoint joinPoint) {
this.handleBeforeLog(joinPoint); } @AfterThrowing( value = "logPointCut()", throwing = "e" ) public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
this.handleAfterThrowingLog(joinPoint, e); } private AuditLog getAnnotationLog(JoinPoint joinPoint) throws Exception {
Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); return method != null ? (AuditLog) method.getAnnotation(AuditLog.class) : null; } private void handleBeforeLog(JoinPoint joinPoint) {
try {
AuditLog controllerLog = this.getAnnotationLog(joinPoint); if (controllerLog == null) {
return; } log.info("before ----> title:{}, desc:{}", controllerLog.title(), controllerLog.desc()); } catch (Exception var10) {
log.error("operlog exception:{}", var10); } } protected void handleAfterLog(JoinPoint joinPoint) {
try {
AuditLog controllerLog = this.getAnnotationLog(joinPoint); if (controllerLog == null) {
return; } log.info("after ----> title:{}, desc:{}", controllerLog.title(), controllerLog.desc()); return; } catch (Exception var10) {
log.error("operlog exception:{}", var10); return; } } protected void handleAfterThrowingLog(JoinPoint joinPoint, Exception e) {
try {
AuditLog controllerLog = this.getAnnotationLog(joinPoint); if (controllerLog == null) {
return; } log.info("发生异常:{}", e.getMessage()); } catch (Exception var10) {
log.error("operlog exception:{}", var10); } }}

五、启动springboot 测试

AopTestController

@RestController@RequestMapping(value = "/aoptest")public class AopTestController {
@AuditLog(title = "标题",desc = "牛逼的方法") @RequestMapping(value = "",method = RequestMethod.GET) public void aopTestMethod(){
System.out.println("哈哈哈"); }}

结果如下:

在这里插入图片描述

总结

本篇只是写个简单的demo示例,大家可以根据该工程结合自身公司业务场景进行扩展

转载地址:http://ekrii.baihongyu.com/

你可能感兴趣的文章
人工神经网络——神经元模型介绍
查看>>
人工神经网络——感知器介绍
查看>>
人工神经网络——反向传播算法(BackPropagation)
查看>>
进程的地址空间概述
查看>>
Windows 窗口底层原理
查看>>
一种函数指针的运用
查看>>
Win32程序之进程的原理
查看>>
C++虚函数原理
查看>>
MySQL的索引
查看>>
今天,Python信息量很大!
查看>>
Flash 已死,Deno 当立?
查看>>
编程差的程序员,90%都是吃了数学的亏!骨灰级开发:方法不对,努力也白费...
查看>>
编程差的程序员,90%都是吃了数学的亏!骨灰级开发:方法不对,努力也白费...
查看>>
都无代码了,还要程序员吗?
查看>>
程序员:凭自己能力吃饭,有什么理由瞧不起?
查看>>
面试想拿 10K,HR 说我只配7k?
查看>>
副业过万的程序员都知道的网站有哪些
查看>>
那些人生“开挂”的程序员,都在干什么?
查看>>
影响科学圈的那些计算机代码
查看>>
乐视视频 App 图标改为“欠 122 亿”,网友:我在别家分红包,却在你家随份子!...
查看>>