
🍁博客主页:👉不会压弯的小飞侠
 ✨欢迎关注:👉点赞👍收藏⭐留言✒
 ✨系列专栏:👉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
 
🔥 debug测试 
 
🔥 info测试
logging:
  level:
    root: info
 

🔥 error测试
logging:
  level:
    root: error
 

🔥 warn测试
logging:
  level:
    root: warn
 

🔥日志级别
- TRACE:运行堆栈信息,使用率低
 - DEBUG:程序员调试代码使用
 - INFO:记录运维过程数据
 - WARN:记录运维过程报警数据
 - ERROR:记录错误堆栈信息
 - FATAL:灾难信息,合并计入ERROR
 
🔥设置分组,对某个组设置日志级别
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
 

 总结:
- 日志用于记录开发调试与运维过程消息
 - 日志的级别共6种,通常使用4种即可,分别是DEBUG,INFO,WARN,ERROR
 - 可以通过日志组或代码包的形式进行日志显示级别的控制
 
🍁日志输出格式控制
✨日志介绍:
2022-07-14 15:12:31.580  INFO 11844 --- [           main] com.jkj.Springboot07LogApplication : Started Springboot07LogApplication in 2.754 seconds (JVM running for 4.14)
 
- 时间 :2022-07-14 15:12:31.580
 - 级别 : INFO
 - PID :11844
 - 所属线程 : [ main]
 - 所属类/接口名 :com.jkj.Springboot07LogApplication
 - 日志信息 : : Started Springboot07LogApplication in 2.754 seconds (JVM running for 4.14)
 - PID:进程ID,用于表明当前操作所处的进程,当多服务同时记录日志时,该值可用于协助程序员调试程序
 - 所属类/接口名:当前显示信息为SpringBoot重写后的信息,名称过长时,简化包名书写为首字母,甚至直接删除
 
✨日志输出格式
logging:
  pattern:
    console: "%d - %m%n"
 
- %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
转载:https://blog.csdn.net/qq_43514330/article/details/125781525
查看评论
					
					