Using Lombok @ slf4j annotation to simplify the introduction of log function
出于代码复用和简洁的考量,我们建议使用lombok 的注解@Slf4j
读者可以对比下文的传统写法。
In the development process , Printing the necessary logs is an essential part , And print the log , In the related class, it is necessary to introduce log related attributes , Traditionally , Usually by LoggerFactory.getLogger To achieve the :
package net.xiaogd.demo.mybatis.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.RestController; @RestController public class LogDemoController { // Self declaration private static final Logger log = LoggerFactory.getLogger((LogDemoController.class)); //... }
Although it's already very simple , But if there are a large number of classes to do similar operations , It's a little repetitive , from The angle of Repetitive management , How can we minimize this duplication of boilerplate [ˈbɔɪlərˌpleɪt]n.(可供模仿的)样板文件 code ?
The answer is to use lombok Of @Slf4j annotation .
Use lombok @Slf4j The premise of annotation
natural , You need to introduce... In your project first lombok, maven Add related dependence in , IDE It also needs to be adjusted , Install related plug-ins.
Besides , Related log dependency also needs to have , If you have enabled logging in the old way , Dependent dependencies usually contain.
lombok @Slf4j The specific usage of annotations
With lombok after , It is very simple to introduce the logging function into the class , Just add... To the class @Slf4j Annotations can be :
package com.demo.controller; import org.springframework.web.bind.annotation.RestController; import lombok.extern.slf4j.Slf4j; @Slf4j @RestController public class DemoController { // ... }
After this , lombok It will inject us with a name by log Log properties of , Use it directly in the code :
package net.xiaogd.demo.mybatis.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import lombok.extern.slf4j.Slf4j; @Slf4j @RestController public class DemoController { @GetMapping("/hello") public String hello() { log.info("enter hello..."); return "hello rest"; } }
Be careful : The default variable name is injection log.
Of course , The name can also be used by lombok.log.fieldName Configuration to adjust , Reference resources lombok About the official website : https://projectlombok.org/features/configuration as well as https://projectlombok.org/features/log
It is generally not recommended to adjust , After all log This name is the most concise , It's easy to knock .
If your existing code uses the traditional way , And it's not for use log The name , Now I want to adjust to use @Slf4j The way , So you can use it first restructure (refactor)-- rename (rename) How to change the name , And then introduce .
It's like saying this to yourself Logger log like that :
@RestController public class LogDemoController { // Self declaration private static final Logger log = LoggerFactory.getLogger((LogDemoController.class)); @GetMapping("/hello1") public String hello() { log.info("enter hello..."); return "hello rest"; } }
If you declare yourself , There are some problems :
- The statement is cumbersome and lengthy , It may be hard for you to remember .
- Different classes need to adjust the name of the current class Value , If you copy carelessly and forget to change, there may be some potential problems ;
- The mistake introduces
LoggerandLoggerFactoryInterfaces can also lead to some potential problems .
It's much easier to use annotations , You don't have to worry about the above problems at all .
Constantly eliminating all kinds of duplication , Improving efficiency is a pursuit in our coding process.
本文来自博客园,作者:z_s_s,转载请注明原文链接:https://www.cnblogs.com/zhoushusheng/p/15887048.html
浙公网安备 33010602011771号