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如下打印出日志
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_23140197/article/details/102581247