使用
@RestController
public class LogController {
public static final Logger log = LoggerFactory.getLogger(LogController.class);
@GetMapping("/index")
public String index(){
log.info("请求info 信息");
log.debug("请求debug 信息");
log.warn("请求warn 信息");
log.error("请求错误信息");
return "OK";
}
}
一般的使用
设置日志的级别
logging:
level:
root: info
注意:打印的内容越多,级别也就越低
设置组
logging:
# 设置组
group:
controller: cn.sycoder.springbootlog.controller
level:
root: info
# cn.sycoder.springbootlog.controller: warn
controller: warn
导入依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
使用
@Slf4j
@RestController
public class LogController {
@GetMapping("/index")
public String index(){
log.info("请求info 信息");
log.debug("请求debug 信息");
log.warn("请求warn 信息");
log.error("请求错误信息");
return "OK";
}
}
底层反编译
导入依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
使用
@Slf4j
@RestController
public class LogController {
@GetMapping("/index")
public String index(){
log.info("请求info 信息");
log.debug("请求debug 信息");
log.warn("请求warn 信息");
log.error("请求错误信息");
return "OK";
}
}
底层反编译
图示
模仿springboot 写一下日志文件
logging:
pattern:
console: "%d %clr(%-5p){yellow} %clr(${pid}){magenta} ---[%20t] %-40.40c : %m %n"
背景:项目持续跑,日志文件会越来越大。
指定路径
logging:
file:
path: F:\03-Spring\SpringBoot\homework\springboot-log
指定名称
logging:
file:
name: F:\03-Spring\SpringBoot\homework\springboot-log\sy.log
注意:指定名称输出的优先级会更高
注意版本信息,如果版本太低的时候不支持 logback
按照日期的格式拆分
拆分日志文件
logdir: F:\03-Spring\SpringBoot\homework\springboot-log\
logging:
logback:
rollingpolicy:
max-file-size: 2KB
file-name-pattern: ${logdir}sy-%d{yyyy-MM-dd}-%i.log
file:
name: ${logdir}sy.log