自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

又被逼著優(yōu)化代碼,這次我干掉了出入?yún)?Log日志

系統(tǒng)
最近技術(shù)部突然刮起一陣 review 代碼的小風(fēng),挨個(gè)項(xiàng)目組過代碼,按理說這應(yīng)該是件挺好的事,讓別人指出自己代碼中的不足,查缺補(bǔ)漏,對(duì)提升自身編碼能力有很大幫助,畢竟自己審查很容易“陶醉”在自己寫的代碼里。

本文轉(zhuǎn)載自微信公眾號(hào)「程序員內(nèi)點(diǎn)事」,作者程序員內(nèi)點(diǎn)事。轉(zhuǎn)載本文請(qǐng)聯(lián)系程序員內(nèi)點(diǎn)事公眾號(hào)。

最近技術(shù)部突然刮起一陣 review 代碼的小風(fēng),挨個(gè)項(xiàng)目組過代碼,按理說這應(yīng)該是件挺好的事,讓別人指出自己代碼中的不足,查缺補(bǔ)漏,對(duì)提升自身編碼能力有很大幫助,畢竟自己審查很容易“陶醉”在自己寫的代碼里。

[[334348]]

 

不過,代碼 review 的詳細(xì)程度令人發(fā)指,一行一行的分析,簡(jiǎn)直就是個(gè)培訓(xùn)班啊。不夸張的說,如果我村里僅有縣重點(diǎn)小學(xué)學(xué)歷的四大爺,來(lái)聽上一個(gè)月后,保證能上手開發(fā),666~

既然組內(nèi)氣氛到這了,咱也得行動(dòng)起來(lái),要不哪天評(píng)審到我的代碼,讓人家指指點(diǎn)點(diǎn)的心里多少有點(diǎn)不舒服,與其被動(dòng)優(yōu)化代碼不如主動(dòng)出擊~

選優(yōu)化代碼的方向,方法入?yún)⒑头祷亟Y(jié)果日志首當(dāng)其沖,每個(gè)方法都會(huì)有這兩個(gè)日志,一大堆冗余的代碼,而且什么樣的打印格式都有,非常的雜亂。

  1. public OrderDTO getOrder(OrderVO orderVO, String name) { 
  2.  
  3.         log.info("訂單詳情入?yún)ⅲ簅rderVO={},name={}", JSON.toJSONString(orderVO), name); 
  4.  
  5.         OrderDTO orderInfo = orderService.getOrderInfo(orderVO); 
  6.  
  7.         log.info("訂單詳情結(jié)果:orderInfo={}", JSON.toJSONString(orderInfo)); 
  8.  
  9.         return orderInfo; 

下邊我們利用 AOP 實(shí)現(xiàn)請(qǐng)求方法的入?yún)?、返回結(jié)果日志統(tǒng)一打印,避免日志打印格式雜亂,同時(shí)減少業(yè)務(wù)代碼量。

一、自定義注解

自定義切面注解@PrintlnLog 用來(lái)輸出日志,注解權(quán)限 @Target({ElementType.METHOD}) 限制只在方法上使用,注解中只有一個(gè)參數(shù) description ,用來(lái)自定義方法輸出日志的描述。

  1. @Retention(RetentionPolicy.RUNTIME) 
  2. @Target({ElementType.METHOD}) 
  3. @Documented 
  4. public @interface PrintlnLog { 
  5.  
  6.     /** 
  7.      * 自定義日志描述信息文案 
  8.      * 
  9.      * @return 
  10.      */ 
  11.     String description() default ""

二、切面類

接下來(lái)編寫@PrintlnLog 注解對(duì)應(yīng)的切面實(shí)現(xiàn),doBefore()中輸出方法的自定義描述、入?yún)?、?qǐng)求方式、請(qǐng)求url、被調(diào)用方法的位置等信息,doAround() 中打印方法返回結(jié)果。

注意: 如何想指定切面在哪個(gè)環(huán)境執(zhí)行,可以用@Profile 注解,只打印某個(gè)環(huán)境的日志。

  1. @Slf4j 
  2. @Aspect 
  3. @Component 
  4. //@Profile({"dev"}) //只對(duì)某個(gè)環(huán)境打印日志 
  5. public class LogAspect { 
  6.  
  7.     private static final String LINE_SEPARATOR = System.lineSeparator(); 
  8.  
  9.     /** 
  10.      * 以自定義 @PrintlnLog 注解作為切面入口 
  11.      */ 
  12.     @Pointcut("@annotation(com.chengxy.unifiedlog.config.PrintlnLog)"
  13.     public void PrintlnLog() { 
  14.     } 
  15.  
  16.     /** 
  17.      * @param joinPoint 
  18.      * @author fu 
  19.      * @description 切面方法入?yún)⑷罩敬蛴?nbsp;
  20.      * @date 2020/7/15 10:30 
  21.      */ 
  22.     @Before("PrintlnLog()"
  23.     public void doBefore(JoinPoint joinPoint) throws Throwable { 
  24.  
  25.         ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); 
  26.         HttpServletRequest request = attributes.getRequest(); 
  27.  
  28.         String methodDetailDescription = this.getAspectMethodLogDescJP(joinPoint); 
  29.  
  30.         log.info("------------------------------- start --------------------------"); 
  31.         /** 
  32.          * 打印自定義方法描述 
  33.          */ 
  34.         log.info("Method detail Description: {}", methodDetailDescription); 
  35.         /** 
  36.          * 打印請(qǐng)求入?yún)?nbsp;
  37.          */ 
  38.         log.info("Request Args: {}", JSON.toJSONString(joinPoint.getArgs())); 
  39.         /** 
  40.          * 打印請(qǐng)求方式 
  41.          */ 
  42.         log.info("Request method: {}", request.getMethod()); 
  43.         /** 
  44.          * 打印請(qǐng)求 url 
  45.          */ 
  46.         log.info("Request URL: {}", request.getRequestURL().toString()); 
  47.  
  48.         /** 
  49.          * 打印調(diào)用方法全路徑以及執(zhí)行方法 
  50.          */ 
  51.         log.info("Request Class and Method: {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName()); 
  52.     } 
  53.  
  54.     /** 
  55.      * @param proceedingJoinPoint 
  56.      * @author xiaofu 
  57.      * @description 切面方法返回結(jié)果日志打印 
  58.      * @date 2020/7/15 10:32 
  59.      */ 
  60.     @Around("PrintlnLog()"
  61.     public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { 
  62.  
  63.         String aspectMethodLogDescPJ = getAspectMethodLogDescPJ(proceedingJoinPoint); 
  64.  
  65.         long startTime = System.currentTimeMillis(); 
  66.  
  67.         Object result = proceedingJoinPoint.proceed(); 
  68.         /** 
  69.          * 輸出結(jié)果 
  70.          */ 
  71.         log.info("{},Response result  : {}", aspectMethodLogDescPJ, JSON.toJSONString(result)); 
  72.  
  73.         /** 
  74.          * 方法執(zhí)行耗時(shí) 
  75.          */ 
  76.         log.info("Time Consuming: {} ms", System.currentTimeMillis() - startTime); 
  77.  
  78.         return result; 
  79.     } 
  80.  
  81.     /** 
  82.      * @author xiaofu 
  83.      * @description 切面方法執(zhí)行后執(zhí)行 
  84.      * @date 2020/7/15 10:31 
  85.      */ 
  86.     @After("PrintlnLog()"
  87.     public void doAfter(JoinPoint joinPoint) throws Throwable { 
  88.         log.info("------------------------------- End --------------------------" + LINE_SEPARATOR); 
  89.     } 
  90.  
  91.     /** 
  92.      * @param joinPoint 
  93.      * @author xiaofu 
  94.      * @description @PrintlnLog 注解作用的切面方法詳細(xì)細(xì)信息 
  95.      * @date 2020/7/15 10:34 
  96.      */ 
  97.     public String getAspectMethodLogDescJP(JoinPoint joinPoint) throws Exception { 
  98.         String targetName = joinPoint.getTarget().getClass().getName(); 
  99.         String methodName = joinPoint.getSignature().getName(); 
  100.         Object[] arguments = joinPoint.getArgs(); 
  101.         return getAspectMethodLogDesc(targetName, methodName, arguments); 
  102.     } 
  103.  
  104.     /** 
  105.      * @param proceedingJoinPoint 
  106.      * @author xiaofu 
  107.      * @description @PrintlnLog 注解作用的切面方法詳細(xì)細(xì)信息 
  108.      * @date 2020/7/15 10:34 
  109.      */ 
  110.     public String getAspectMethodLogDescPJ(ProceedingJoinPoint proceedingJoinPoint) throws Exception { 
  111.         String targetName = proceedingJoinPoint.getTarget().getClass().getName(); 
  112.         String methodName = proceedingJoinPoint.getSignature().getName(); 
  113.         Object[] arguments = proceedingJoinPoint.getArgs(); 
  114.         return getAspectMethodLogDesc(targetName, methodName, arguments); 
  115.     } 
  116.  
  117.     /** 
  118.      * @param targetName 
  119.      * @param methodName 
  120.      * @param arguments 
  121.      * @author xiaofu 
  122.      * @description 自定義注解參數(shù) 
  123.      * @date 2020/7/15 11:51 
  124.      */ 
  125.     public String getAspectMethodLogDesc(String targetName, String methodName, Object[] arguments) throws Exception { 
  126.         Class targetClass = Class.forName(targetName); 
  127.         Method[] methods = targetClass.getMethods(); 
  128.         StringBuilder description = new StringBuilder(""); 
  129.         for (Method method : methods) { 
  130.             if (method.getName().equals(methodName)) { 
  131.                 Class[] clazzs = method.getParameterTypes(); 
  132.                 if (clazzs.length == arguments.length) { 
  133.                     description.append(method.getAnnotation(PrintlnLog.class).description()); 
  134.                     break; 
  135.                 } 
  136.             } 
  137.         } 
  138.         return description.toString(); 
  139.     } 

三、應(yīng)用

我們?cè)谛枰蛴∪雲(yún)⒑头祷亟Y(jié)果日志的方法,加上@PrintlnLog注解,并添加自定義方法描述。

  1. @RestController 
  2. @RequestMapping 
  3. public class OrderController { 
  4.  
  5.     @Autowired 
  6.     private OrderService orderService; 
  7.  
  8.     @PrintlnLog(description = "訂單詳情Controller"
  9.     @RequestMapping("/order"
  10.     public OrderDTO getOrder(OrderVO orderVO, String name) { 
  11.  
  12.         OrderDTO orderInfo = orderService.getOrderInfo(orderVO); 
  13.  
  14.         return orderInfo; 
  15.     } 

代碼里去掉 log.info日志打印,加上 @PrintlnLog 看一下效果,清晰明了。

 

 

Demo GitHub地址:https://github.com/chengxy-nds/Springboot-Notebook/tree/master/springboot-aop-unifiedlog

 

責(zé)任編輯:武曉燕 來(lái)源: 程序員內(nèi)點(diǎn)事
相關(guān)推薦

2020-04-09 08:29:50

編程語(yǔ)言事件驅(qū)動(dòng)

2020-11-09 14:03:51

Spring BootMaven遷移

2020-10-10 09:09:21

CTOCRUD設(shè)計(jì)

2019-11-26 10:07:10

業(yè)務(wù)開發(fā)邏輯

2022-09-26 10:01:04

SpringAOP日志

2018-03-23 05:25:18

5GWiFi網(wǎng)絡(luò)

2019-12-02 10:34:19

Python虛擬機(jī)內(nèi)存

2021-04-20 08:02:08

業(yè)務(wù)數(shù)據(jù)用戶

2021-04-27 10:26:57

微軟Linux桌面

2020-12-28 13:43:03

MacWindowsSurface

2024-02-19 09:10:46

OpenAISora功能

2022-04-06 21:50:08

區(qū)塊鏈互聯(lián)網(wǎng)支付

2014-12-01 11:20:28

Win8.1微軟

2023-11-29 09:09:27

OceanBase底層

2020-01-21 19:21:44

WindowsWindows 10Windows XP

2021-09-09 18:12:22

內(nèi)存分段式網(wǎng)絡(luò)

2020-04-02 14:07:30

微信QQ轉(zhuǎn)賬

2018-10-06 15:38:12

2022-03-26 08:49:13

MySQL數(shù)據(jù)存儲(chǔ)

2021-02-23 09:06:00

MVCC版本并發(fā)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)