pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId>
</dependency>

application.yaml配置

logging:
  level:
    root: INFO
  file:
    name: log/sys.log # 根目录下创建log目录,保存为sys.log文件
  logback:
    rollingpolicy:
      # gz
      file-name-pattern: ${LOG_FILE}.%d{yyyy-MM-dd}.%i.zip
      max-file-size: 1MB
  pattern:
    dateformat: yyyy-MM-dd HH:mm:ss

示例

package com.xcg.webapp.Controller;

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("/Test")
public class TestController {
    private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    @GetMapping("/writeLog")
    public String writeLog() {
        logger.info("this is a test message!");
        System.out.println(logger.getClass().toString());
        //class ch.qos.logback.classic.Logger
        return "welcome to spring boot3";
    }
}

 

posted on 2024-04-23 11:08  邢帅杰  阅读(239)  评论(0编辑  收藏  举报