java

log4j2 exception when using elas

2018-11-14  本文已影响4人  Java分布式架构实战

错误描述

<html >
<div style="color:red">
RROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
</div>
</html>

错误分析过程

elasticsearch依赖定义

<properties>
   <elasticsearch.version>5.5.1</elasticsearch.version>
<properties>

<!--elasticsearch-->
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
</dependency>
log4j2-error.png

从这些信息可以看出,组件org.elasticsearch:elasticsearch:5.5.1用到了log4j

log4j-core是可选依赖,通过mvn dependency:tree分析,发现确实没有这个依赖,所以先尝试把log4j-core加上去。加上去后,再次运行,就不会再输出这个error了。

org.apache.logging.log4j:log4j-1.2-api这个构件是Apache Logging提供的log4j到log4j2的桥接库。所以从这里可以看出elasticsearch构件用的应该是log4j,同时它又引用的可选的桥接库,将log4j桥接到log4j2。根据mvn dependency:tree的输出可以得知spring-boot-starter-logging已经引入了以下桥接库org.slf4j:jul-to-slf4j和org.slf4j:log4j-over-slf4j。

[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.5.10.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:1.5.10.RELEASE:compile
[INFO] | | - org.springframework.boot:spring-boot-starter-logging:jar:1.5.10.RELEASE:compile
[INFO] | | +- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] | | - org.slf4j:log4j-over-slf4j:jar:1.7.25:compile

同时发现项目所有依赖中并没有发现有log4j2的身影。所以不需要相应的桥接库,即
org.apache.logging.log4j:log4j-to-slf4j

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-to-slf4j -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-to-slf4j</artifactId>
    <version>2.11.1</version>
</dependency>

同时还发现项目所有依赖中也没有出现commons-logging的身影,所以不需要jcl相关桥接库,即

JCL 1.2 implemented over SLF4J
<!-- https://mvnrepository.com/artifact/org.slf4j/jcl-over-slf4j -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.7.25</version>
</dependency>

spring-boot-starter-logging已排除对commons-logging的依赖:

             <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

spring-boot-starter-logging的pom完整定义:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starters</artifactId>
        <version>1.5.16.RELEASE</version>
    </parent>
    <artifactId>spring-boot-starter</artifactId>
    <name>Spring Boot Starter</name>
    <description>Core starter, including auto-configuration support, logging and YAML</description>
    <url>http://projects.spring.io/spring-boot/</url>
    <organization>
        <name>Pivotal Software, Inc.</name>
        <url>http://www.spring.io</url>
    </organization>
    <properties>
        <main.basedir>${basedir}/../..</main.basedir>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>

最终解决方案

在pom中增加依赖

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.1</version>
</dependency>

总结

通过这个解决问题的过程学习了java loging框架相关的知识及解决maven依赖相关的分析思路。涉及的知识点:

  1. log4j2,logback相关日志框架的选择。从springboot框架的倾向来看,比较钟情于logback, 会默认排除commons-logging。
  2. maven可选依赖的作用,主要是打破依赖的传递性
  3. maven如何排除依赖
  4. maven中scrope的作用
  5. 在项目中应该定义parent模块,并在其中使用dependencyManagement统一声明各构件的版本及排除相关依赖。
  6. maven模块构建的问题:在写demo验证问题的过程中遇到了一个构建问题,原因是没有从根开始构建。
    常用的几个命令
    mvn clean package -DskipTests
    mvn clean package -DskipTests -pl module-0,moduel-1 -am
    mvn clean deploy -DskipTests
    mvn clean deploy -DskipTests -pl module-0,moduel-1 -am
上一篇下一篇

猜你喜欢

热点阅读