好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SpringBoot使用AOP统一日志管理的方法详解

前言

请问今天您便秘了吗?程序员坐久了真的会便秘哦,如果偶然点进了这篇小干货,就麻烦您喝杯水然后去趟厕所一边用左手托起对准嘘嘘,一边用右手滑动手机看完本篇吧。

实现

本篇AOP统一日志管理写法来源于国外知名开源框架JHipster的AOP日志管理方式

1、引入依赖

?

1

2

3

4

5

<!-- spring aop -->

< dependency >

     < groupId >org.springframework.boot</ groupId >

     < artifactId >spring-boot-starter-aop</ artifactId >

</ dependency >

2、定义logback配置

1)dev、test环境的spring-web包定义日志级别为INFO,项目包定义日志级别为DEBUG;

2)prod环境的spring-web包定义日志级别为ERROR,项目包定义日志级别为INFO;

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<? xml version = "1.0" encoding = "UTF-8" ?>

< configuration >

     < include resource = "org/springframework/boot/logging/logback/base.xml" />

     < logger name = "org.springframework.web" level = "INFO" />

     < logger name = "org.springboot.sample" level = "TRACE" />

 

     < springProfile name = "dev,test" >

         < logger name = "org.springframework.web" level = "INFO" />

         < logger name = "org.springboot.sample" level = "INFO" />

         < logger name = "com.example.aoplog" level = "DEBUG" />

     </ springProfile >

 

     < springProfile name = "prod" >

         < logger name = "org.springframework.web" level = "ERROR" />

         < logger name = "org.springboot.sample" level = "ERROR" />

         < logger name = "com.example.aoplog" level = "INFO" />

     </ springProfile >

 

</ configuration >

3、编写切面类

1)springBeanPointcut():单独定义的spring框架切入点;

2)applicationPackagePointcut():单独定义的项目包切入点;

3)logAfterThrowing():1和2定义的切入点抛出异常时日志格式及显示内容;

4)logAround():1和2定义的切入点方法进入和退出时日志格式及显示内容。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

package com.example.aoplog.logging;

 

import com.example.aoplog.constants.GloablConstants;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Pointcut;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.core.env.Environment;

import org.springframework.core.env.Profiles;

import org.springframework.stereotype.Component;

 

import java.util.Arrays;

 

/**

  * <p>

  * AOP统一日志管理 切面类

  * </p>

  *

  * @author 福隆苑居士,公众号:【Java分享客栈】

  * @since 2022/5/5 21:57

  */

@Aspect

@Component

public class LoggingAspect {

 

     private final Logger log = LoggerFactory.getLogger( this .getClass());

 

     private final Environment env;

 

     public LoggingAspect(Environment env) {

         this .env = env;

     }

 

     /**

     * 匹配spring框架的repositories、service、rest端点的切面

      */

     @Pointcut ( "within(@org.springframework.stereotype.Repository *)" +

         " || within(@org.springframework.stereotype.Service *)" +

         " || within(@org.springframework.web.bind.annotation.RestController *)" )

     public void springBeanPointcut() {

         // 方法为空,因为这只是一个切入点,实现在通知中。

     }

 

     /**

     * 匹配我们自己项目的repositories、service、rest端点的切面

      */

     @Pointcut ( "within(com.example.aoplog.repository..*)" +

         " || within(com.example.aoplog.service..*)" +

         " || within(com.example.aoplog.controller..*)" )

     public void applicationPackagePointcut() {

         // 方法为空,因为这只是一个切入点,实现在通知中。

     }

 

     /**

      * 记录方法抛出异常的通知

      *

      * @param joinPoint join point for advice

      * @param e exception

      */

     @AfterThrowing (pointcut = "applicationPackagePointcut() && springBeanPointcut()" , throwing = "e" )

     public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {

 

        // 判断环境,dev、test or prod

         if (env.acceptsProfiles(Profiles.of(GloablConstants.SPRING_PROFILE_DEVELOPMENT, GloablConstants.SPRING_PROFILE_TEST))) {

             log.error( "Exception in {}.{}() with cause = '{}' and exception = '{}'" , joinPoint.getSignature().getDeclaringTypeName(),

                 joinPoint.getSignature().getName(), e.getCause() != null ? e.getCause() : "NULL" , e.getMessage(), e);

 

         } else {

             log.error( "Exception in {}.{}() with cause = {}" , joinPoint.getSignature().getDeclaringTypeName(),

                 joinPoint.getSignature().getName(), e.getCause() != null ? e.getCause() : "NULL" );

         }

 

     }

 

     /**

      * 在方法进入和退出时记录日志的通知

      *

      * @param joinPoint join point for advice

      * @return result

      * @throws Throwable throws IllegalArgumentException

      */

     @Around ( "applicationPackagePointcut() && springBeanPointcut()" )

     public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {

 

         if (log.isDebugEnabled()) {

             log.debug( "Enter: {}.{}() with argument[s] = {}" , joinPoint.getSignature().getDeclaringTypeName(),

                 joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs()));

         }

         try {

             Object result = joinPoint.proceed();

             if (log.isDebugEnabled()) {

                 log.debug( "Exit: {}.{}() with result = {}" , joinPoint.getSignature().getDeclaringTypeName(),

                     joinPoint.getSignature().getName(), result);

             }

             return result;

         } catch (IllegalArgumentException e) {

             log.error( "Illegal argument: {} in {}.{}()" , Arrays.toString(joinPoint.getArgs()),

                 joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());

 

             throw e;

         }

 

     }

 

}

4、测试

1)写个service

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

package com.example.aoplog.service;

 

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

 

/**

  * <p>

  * AOP统一日志管理测试服务

  * </p>

  *

  * @author 福隆苑居士,公众号:【Java分享客栈】

  * @since 2022/5/5 21:57

  */

@Service

@Slf4j

public class AopLogService {

 

    public String test(Integer id) {

       return "传入的参数是:" + id;

    }

}

2)写个controller

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

package com.example.aoplog.controller;

 

import com.example.aoplog.service.AopLogService;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

 

/**

  * <p>

  * 测试接口

  * </p>

  *

  * @author 福隆苑居士,公众号:【Java分享客栈】

  * @since 2022/4/30 11:43

  */

@RestController

@RequestMapping ( "/api" )

@Slf4j

public class TestController {

 

    private final AopLogService aopLogService;

 

    public TestController(AopLogService aopLogService) {

       this .aopLogService = aopLogService;

    }

 

    @GetMapping ( "/test/{id}" )

    public ResponseEntity<String> test( @PathVariable ( "id" ) Integer id) {

       return ResponseEntity.ok().body(aopLogService.test(id));

    }

}

3)设置环境

这里我试试dev,prod自己试听见没?不服一拳打哭你哦!

?

1

2

3

4

5

6

7

server:

   port: 8888

 

# 环境:dev-开发 test-测试 prod-生产

spring:

   profiles:

     active: dev

4)效果

不解释了自己看

试试异常情况,手动加个异常。

?

1

2

3

4

5

6

7

8

9

@Service

@Slf4j

public class AopLogService {

 

    public String test(Integer id) {

       int i = 1 / 0 ;

       return "传入的参数是:" + id;

    }

}

效果

到此这篇关于SpringBoot使用AOP统一日志管理的方法详解的文章就介绍到这了,更多相关SpringBoot AOP日志管理内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://mp.weixin.qq测试数据/s/Agrj2pHwW2TK1Z_wr7ghPw

查看更多关于SpringBoot使用AOP统一日志管理的方法详解的详细内容...

  阅读:14次