webmagic 日志使用及maven项目中排除日志依赖

我用的Spring Boot maven构建的工程,默认引入了
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
再引入webmagic相关的包之后,运行出现大量的debug日志。
此时没有加入任何的log配置文件
代码初始化执行时报以下警告

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/hongbo/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/hongbo/.m2/repository/org/slf4j/slf4j-log4j12/1.7.25/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

说明有其他dependency引入了logback
根据作者的说明:
WebMagic 使用slf4j-log4j12作为slf4j的实现.如果你自己定制了slf4j的实现,请在项目中去掉此依赖。
<exclusions>
    <exclusion>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
    </exclusion>
</exclusions>
查看maven tree
发现

 

该包是在spring-boot-starter被引入的,所以需要做排除。
但是不能直接排除spring-boot-starter-logging,这样则所有依赖于slf4j的也将失效。
所以排除logback-classic即可
 
如下:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </exclusion>
    </exclusions>
</dependency>

关于有多个log的实现及其实际绑定的log配置文件。
通常,我们会使用以下三种log
log4j,对应配置文件log4j.xml或log4j.properties
logback, 对应配置文件logback.xml
log4j2, 对应配置文件log4j2.xml或log4j2.json
 
但是在引用log4j2时有个陷阱或者小细节
当直接依赖log4j2时,其对应的配置文件为log4j2.xml或log4j2.json
其依赖如下
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.7</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.7</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.7</version>
</dependency>
当使用slf4j-log4j12时,其对应的配置文件为log4j.xml或log4j.properties
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
</dependency>
当项目中存在多个log依赖,且没有排除相应的依赖的时候,会选择哪个配置文件呢?
想maven加载冲突jar有没有加载顺序(偷懒),在官网查了下:点击打开链接
The way SLF4J picks a binding is determined by the JVM and for all practical purposes should be considered random.
说明slf4j具体选哪个binding是jvm随机决定的。那么偷懒(调整先后顺序)不成,再继续。
虽然SLF4J随机binding了一个log,但是SLF4J会告诉我们它到底binding了谁。
通过项目启动的时候,查看SLF4J: Actual binding is of type
例如SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
说明依赖的是Log4j对应的配置文件
 
当实际绑定的为logback且logback.xml不存在时,springboot默认会输出debug级别的日志。
参考:
spring boot log4j2配置不生效
 
Maven全局排除某引用的一种方式

转载于:https://www.cnblogs.com/huanghongbo/p/9089311.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值