三、SpringBoot的日志--bilibili雷丰阳笔记

三、springboot与日志

抽象层 SLF4j 实现类logback

日志记录方法

给系统导入slf4j的jar和logback的jar

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    logger.info("Hello World");
  }

在这里插入图片描述

统一日志记录,即使别的框架

在这里插入图片描述

1 将系统中其他日志框架先排除

2 用中间包来替换原有的日志框架

3 我们导入slf4j其他的实现

idea中

在这里插入图片描述

springboot的底层方式 slf4j+log4j的方式
在这里插入图片描述

中间的转换包

在这里插入图片描述

我们要引入其他框架,一定要把这个框架的默认日志依赖移除掉

在这里插入图片描述

适配器

/**
 * 在编码的时候进行容器注入
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootApplicationTest {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Test
    public void contextLoads(){
        //日志级别
        logger.trace("这是trace日志");
        logger.debug("这是debug日志");
        logger.info("info日志");
        logger.warn("warn日志");
        logger.error("error日志");

    }
}

springboot默认日志级别是info

logging.level.com=trace
#logging.file=springboot.log
logging.file=d:/springboot.log
logging.path=/spring/log   #在e:/根目录下

logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n

logging.pattern.file=

logging.file和logging.path取其一,一般logging.file为主

2、指定配置

给类路径下放上每个日志框架自己的配置文件:SpringBoot就不使用他默认配置

记录系统 定制
Logback logback-spring.xml, logback-spring.groovy, logback.xml, or logback.groovy
Log4j2 log4j2-spring.xml or log4j2.xml
JDK (Java Util Logging) logging.properties

logback-spring.xml

<springProfile name="staging">
	<!-- configuration to be enabled when the "staging" profile is active -->
</springProfile>

<springProfile name="dev | staging">
	<!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</springProfile>

<springProfile name="!production">
	<!-- configuration to be enabled when the "production" profile is not active -->
</springProfile>

在这里插入图片描述

spring.profiles.active=dev

文件名没有logback-spring.xml spring就报错

排除依赖

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值