Spring boot配置日誌

Spring Boot 使用默認日誌系統首先添加dependency 依賴

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

新增一個配置類,在方法中打印日誌

package com.example.demo.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LogConfig {
    private static final Logger LOG = LoggerFactory.getLogger(LogConfig.class);

    @Bean
 public String logMethod() {
        LOG.info("==========print log==========");
        return "SayHi";
    }
}

application.properties 可以編寫日誌相關屬性
logging.level.root 日誌等級
logging.pattern.console 日誌輸出的格式
logging.path 屬性用來配置日誌文件的路徑

在方法中添加打印日誌

package com.example.demo.controller;

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

import com.example.demo.model.Student;

@RestController
public class HelloController {

	private static final Logger LOG = LoggerFactory.getLogger(HelloController.class);
	   
	   @RequestMapping("/sayHi")
	   public String sayHi(@RequestBody Student demo) {
		   LOG.info("OK");
		   
		   return demo.getName()+demo.getAge();
	   }
}

這樣運行Spring boot如下打印出日誌
在這裏插入圖片描述

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