首先在application.properties中配置以下配置

#项目的所有日志等级
logging.level.root=WARN
#指定包名下的日志等级
logging.level.com.example.springbootstudy=INFO
#项目同级目录下的日志文件
logging.file=app.log

测试:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
package com.example.springbootstudy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("helloSpringBoot")
public class HelloSpringBootController {

    private Logger logger = LoggerFactory.getLogger(HelloSpringBootController.class);

    @GetMapping("printLog")
    public String printLog() {
        logger.info("info -- 用户请求打印日志");
        logger.error("error -- 用户请求打印日志");
        return "print log success";
    }
}

结果:

 Spring Boot 5.日志管理 随笔

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄