SpringBoot3使用自帶日誌組件Logback

參考:https://blog.csdn.net/AIJXB/article/details/128602818
pom.xml

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

application.yaml配置

logging:
  level:
    root: INFO
  file:
    name: 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";
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章