博客主页:不会压弯的小飞侠
✨欢迎关注:点赞收藏⭐留言✒
✨系列专栏:SpringBoot专栏(每日更新)
✨如果觉得博主的文章还不错的话,请三连支持一下博主。
欢迎大佬指正,一起学习!一起加油!
Spring Boot默认情况下会用Logback来记录日志,并用INFO级别输出到控制台。在运行应用程序应该已经看到很多INFO级别的日志了。
创建Log对象的类
package com.jkj.log;
import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;
public class BaseClass {
//创建记录日志的对象
private Class clazz;
public static Logger log;
public BaseClass(){
clazz=this.getClass();
log= LoggerFactory.getLogger(clazz);
}
}
继承这个类
package com.jkj.controller;
import com.jkj.log.BaseClass;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/bookLog")
public class BookController extends BaseClass {
/* private static final Logger logger=LoggerFactory.getLogger(BookController.class);*/
@GetMapping
public String ById(){
System.out.println("springboot_02 is running...");
log.debug("debug...");
log.info("info...");
log.warn("warn...");
log.error("error...");
return "springboot_02 is running...";
}
}
package com.jkj.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("/bookLog")
public class BookController {
private static final Logger logger=LoggerFactory.getLogger(BookController.class);
@GetMapping
public String ById(){
System.out.println("springboot_02 is running...");
logger.debug("debug...");
logger.info("info...");
logger.warn("warn...");
logger.error("error...");
return "springboot_02 is running...";
}
}
**注解形式:**使用lombok提供的注解@Slf4j可以简化开发,减少日志对象的声明操作。
导入坐标
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
package com.jkj.controller;
import lombok.extern.slf4j.Slf4j;
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("/bookLog")
@Slf4j
public class BookController {
@GetMapping
public String ById(){
System.out.println("springboot_02 is running...");
//注解
log.debug("debug...");
log.info("info...");
log.warn("warn...");
log.error("error...");
return "springboot_02 is running...";
}
}
⭐⭐⭐注意:
以上三种方式运行出来的日志是没有debug的,需要在配置文件中设置日志输出级别:
#方式一
#开启debug模式,输出调试信息,常用于检查系统运行状况
#debug: true
#方式二(推荐使用)
#设置日志级别,root表示根节点,即整体应用日志级别
logging:
level:
root: debug
logging:
level:
root: info
logging:
level:
root: error
logging:
level:
root: warn
logging:
group:
ebank: com.jkj.controller
level:
root: info
ebank: debug
logging:
group:
ebank: com.jkj.controller
level:
root: info
ebank: debug
com.jkj.controller: debug
2022-07-14 15:12:31.580 INFO 11844 --- [ main] com.jkj.Springboot07LogApplication : Started Springboot07LogApplication in 2.754 seconds (JVM running for 4.14)
logging:
pattern:
console: "%d - %m%n"
#设置日志模板格式
pattern:
#console: "%d - %m %n "
console: "%d %c1r(%5p) --- [%16t] %clr(%-40.40c){cyan} : %m %n"
file:
name: server.log
logback:
rollingpolicy:
max-file-size: 4KB
file-name-pattern: server.%d{yyyy-MM-dd}. %i.log