欢迎光临
我们一直在努力

@Slf4j是啥,@Slf4j的作用

@Slf4j是用作日志输出的,一般会在项目每个类的开头加入该注解,如果不写下面这段代码,并且想用log

private final Logger logger = LoggerFactory.getLogger(当前类名.class);

就可以用@Slf4来代替;这样就省去这段很长的代码。

import ...

@Controller
@RequestMapping("/abc")
@Slf4j
public class QueryBillController {
    @Autowired
    private TraceService traceService;
    @Autowired
    private SignatureAndVerification signatureAndVerification;

    /**
     * 账单查询接口
     */
    @RequestMapping(.........)
    @ResponseBody
    public String getBills(@RequestBody String queryRequest) {
        log.info("进入账单查询接口");

添加了该注释之后,就可以在代码中直接应用log.info( ) 打印日志了

@Slf4j的使用

首先,在pom.xml中导入依赖

    <dependencies>
        <!-- log -->
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
            <!-- <scope>test</scope> -->
        </dependency>
        <dependency>

            <groupId>org.projectlombok</groupId>

            <artifactId>lombok</artifactId>

            <version>1.18.6</version><!--版本号自己选一个就行-->

        </dependency>
    </dependencies>

然后,(这里是idea里面的操作)File -> Setting -> Plugins -> 搜索lombok并安装,

安装好了之后,就写日志的配置文件,路径为src/main/resources/log4j.properties

### 设置###
log4j.rootLogger = debug,stdout,D,E

### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS}ms, [%t]:%m%n

### 输出DEBUG 级别以上的日志到=E://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### 输出ERROR 级别以上的日志到=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E://logs/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

然后就可以用了:

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class threadCreateTest {
    public static void main(String[] args) {
        Thread t = new Thread(){
            @Override
            public void run() {
                log.debug("hello world");
            }
        };
        t.setName("t1");
        t.start();
        log.debug("hello world");
    }
}

以下是输出信息

[DEBUG] 2020-10-05 16:31:12,273ms, [main]:hello world
[DEBUG] 2020-10-05 16:31:12,273ms, [Thread-0]:hello world
赞(1)
版权归原作者所有,如有侵权请告知。达维营-前端网 » @Slf4j是啥,@Slf4j的作用

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址