Log4J的2版本无法与RabbitMQ的&春天开机工作(Log4J 2 version not w

2019-09-30 00:28发布

我有以下的RabbitMQ项目的pom.xml。 附加示例代码了。 任何人可以帮助我的错误我面对。 我试图实现净,但没有成功的各种解决方案。 我试着用Log4J的XML过。

我读了很多网站,如果我使用log4j的话,我必须排除一些依赖关系也。 在我的POM XML我已经排除它们。 但仍然没有成功。

<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>
    <groupId>TestProject</groupId>
    <artifactId>TestProject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <description>TestProject</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
        </dependency>

        <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> 
            <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> 
            </exclusion> </exclusions> </dependency> -->

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>2.6.0</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.40</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.4.0</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.4.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.2</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>

        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.9</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.5</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我LoggerUtil.java

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import com.en.imps.constant.LoggerConstants;

@Component
public class LoggerUtil{

    private static Logger logger = LogManager.getLogger(LoggerUtil.class);
    private static LoggerUtil instance = null;

    public static LoggerUtil getInstance() {
        if (instance == null) {
            instance = new LoggerUtil();
        }
        return instance;
    }

    public void doLog(int type, String className, String methodName, String description) {  
        StringBuffer strMessage = new StringBuffer();
        strMessage.append(" IMPS PORTAL LOG  : ");
        strMessage.append("Class :");
        strMessage.append(className).append("|| ");
        strMessage.append("Method :");
        strMessage.append(methodName).append(" || ");

        if (type == LoggerConstants.LTI) 
            logger.info(strMessage.toString());
        else if (type == LoggerConstants.LTW) 
            logger.warn(strMessage.toString());
        else if (type == LoggerConstants.LTE) 
            logger.error(strMessage.toString());
        else if (type == LoggerConstants.LTD) 
            logger.debug(strMessage.toString());

    }

    public static String getExStackTrace(Exception e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        return sw.toString();
    }


}

我下面让例外。

[DEBUG] 2018-03-23 15:20:28.438 [main] ClasspathLoggingApplicationListener - Application failed to start with classpath: [file:/D:/Test_WorkSpace%202/TestProject/target/classes/, file:/C:/Users/USERS/.m2/repository/org/springframework/boot/spring-boot-starter-amqp/1.5.3.RELEASE/spring-boot-starter-amqp-1.5.3.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/boot/spring-boot-starter/1.5.3.RELEASE/spring-boot-starter-1.5.3.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/boot/spring-boot/1.5.3.RELEASE/spring-boot-1.5.3.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/1.5.3.RELEASE/spring-boot-autoconfigure-1.5.3.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/spring-core/4.3.8.RELEASE/spring-core-4.3.8.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/org/yaml/snakeyaml/1.17/snakeyaml-1.17.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/spring-messaging/4.3.8.RELEASE/spring-messaging-4.3.8.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/spring-beans/4.3.8.RELEASE/spring-beans-4.3.8.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/spring-context/4.3.8.RELEASE/spring-context-4.3.8.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/spring-expression/4.3.8.RELEASE/spring-expression-4.3.8.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/amqp/spring-rabbit/1.7.2.RELEASE/spring-rabbit-1.7.2.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/spring-web/4.3.8.RELEASE/spring-web-4.3.8.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/com/rabbitmq/http-client/1.1.1.RELEASE/http-client-1.1.1.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/org/apache/httpcomponents/httpclient/4.5.3/httpclient-4.5.3.jar, file:/C:/Users/USERS/.m2/repository/org/apache/httpcomponents/httpcore/4.4.6/httpcore-4.4.6.jar, file:/C:/Users/USERS/.m2/repository/commons-codec/commons-codec/1.10/commons-codec-1.10.jar, file:/C:/Users/USERS/.m2/repository/com/rabbitmq/amqp-client/4.0.2/amqp-client-4.0.2.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/spring-tx/4.3.8.RELEASE/spring-tx-4.3.8.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/retry/spring-retry/1.2.0.RELEASE/spring-retry-1.2.0.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/amqp/spring-amqp/1.7.2.RELEASE/spring-amqp-1.7.2.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/boot/spring-boot-starter-log4j2/1.5.3.RELEASE/spring-boot-starter-log4j2-1.5.3.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.7/log4j-slf4j-impl-2.7.jar, file:/C:/Users/USERS/.m2/repository/org/slf4j/jcl-over-slf4j/1.7.25/jcl-over-slf4j-1.7.25.jar, file:/C:/Users/USERS/.m2/repository/org/slf4j/jul-to-slf4j/1.7.25/jul-to-slf4j-1.7.25.jar, file:/C:/Users/USERS/.m2/repository/org/json/json/20140107/json-20140107.jar, file:/C:/Users/USERS/.m2/repository/com/fasterxml/jackson/dataformat/jackson-dataformat-xml/2.8.8/jackson-dataformat-xml-2.8.8.jar, file:/C:/Users/USERS/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.8.8/jackson-core-2.8.8.jar, file:/C:/Users/USERS/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.8.0/jackson-annotations-2.8.0.jar, file:/C:/Users/USERS/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.8.8/jackson-databind-2.8.8.jar, file:/C:/Users/USERS/.m2/repository/com/fasterxml/jackson/module/jackson-module-jaxb-annotations/2.8.8/jackson-module-jaxb-annotations-2.8.8.jar, file:/C:/Users/USERS/.m2/repository/org/codehaus/woodstox/stax2-api/3.1.4/stax2-api-3.1.4.jar, file:/C:/Users/USERS/.m2/repository/com/fasterxml/woodstox/woodstox-core/5.0.3/woodstox-core-5.0.3.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/boot/spring-boot-starter-jdbc/1.5.3.RELEASE/spring-boot-starter-jdbc-1.5.3.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/spring-jdbc/4.3.8.RELEASE/spring-jdbc-4.3.8.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/com/zaxxer/HikariCP/2.6.0/HikariCP-2.6.0.jar, file:/C:/Users/USERS/.m2/repository/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar, file:/C:/Users/USERS/.m2/repository/mysql/mysql-connector-java/5.1.40/mysql-connector-java-5.1.40.jar, file:/C:/Users/USERS/.m2/repository/io/springfox/springfox-swagger2/2.4.0/springfox-swagger2-2.4.0.jar, file:/C:/Users/USERS/.m2/repository/io/swagger/swagger-annotations/1.5.6/swagger-annotations-1.5.6.jar, file:/C:/Users/USERS/.m2/repository/io/swagger/swagger-models/1.5.6/swagger-models-1.5.6.jar, file:/C:/Users/USERS/.m2/repository/io/springfox/springfox-spi/2.4.0/springfox-spi-2.4.0.jar, file:/C:/Users/USERS/.m2/repository/io/springfox/springfox-core/2.4.0/springfox-core-2.4.0.jar, file:/C:/Users/USERS/.m2/repository/io/springfox/springfox-schema/2.4.0/springfox-schema-2.4.0.jar, file:/C:/Users/USERS/.m2/repository/io/springfox/springfox-swagger-common/2.4.0/springfox-swagger-common-2.4.0.jar, file:/C:/Users/USERS/.m2/repository/io/springfox/springfox-spring-web/2.4.0/springfox-spring-web-2.4.0.jar, file:/C:/Users/USERS/.m2/repository/com/google/guava/guava/18.0/guava-18.0.jar, file:/C:/Users/USERS/.m2/repository/com/fasterxml/classmate/1.3.3/classmate-1.3.3.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/plugin/spring-plugin-core/1.2.0.RELEASE/spring-plugin-core-1.2.0.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/spring-aop/4.3.8.RELEASE/spring-aop-4.3.8.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/org/springframework/plugin/spring-plugin-metadata/1.2.0.RELEASE/spring-plugin-metadata-1.2.0.RELEASE.jar, file:/C:/Users/USERS/.m2/repository/io/springfox/springfox-swagger-ui/2.4.0/springfox-swagger-ui-2.4.0.jar, file:/C:/Users/USERS/.m2/repository/com/google/code/gson/gson/2.8.2/gson-2.8.2.jar, file:/C:/Users/USERS/.m2/repository/com/thoughtworks/xstream/xstream/1.4.9/xstream-1.4.9.jar, file:/C:/Users/USERS/.m2/repository/xmlpull/xmlpull/1.1.3.1/xmlpull-1.1.3.1.jar, file:/C:/Users/USERS/.m2/repository/xpp3/xpp3_min/1.1.4c/xpp3_min-1.1.4c.jar, file:/C:/Users/USERS/.m2/repository/org/apache/logging/log4j/log4j-api/2.5/log4j-api-2.5.jar, file:/C:/Users/USERS/.m2/repository/org/apache/logging/log4j/log4j-core/2.5/log4j-core-2.5.jar]
[ERROR] 2018-03-23 15:20:28.440 [main] SpringApplication - Application startup failed
java.lang.NoSuchMethodError: org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(Lorg/apache/logging/log4j/core/LoggerContext;Lorg/apache/logging/log4j/core/config/ConfigurationSource;)Lorg/apache/logging/log4j/core/config/Configuration;
    at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.loadConfiguration(Log4J2LoggingSystem.java:176) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.loadDefaults(Log4J2LoggingSystem.java:159) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:84) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:59) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.initialize(Log4J2LoggingSystem.java:148) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:303) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:276) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:212) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:73) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:336) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at com.en.test.TestProject.main(TestProject.java:10) [classes/:?]

Answer 1:

错误的原因是log4j-core version 2.5没有下面的方法-

org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(org.apache.logging.log4j.core.LoggerContext loggerContext, org.apache.logging.log4j.core.config.ConfigurationSource configurationSource)

要解决此错误,只需删除下面的依赖关系pom.xml -

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.5</version>
    </dependency>

Spring boot管理log4j依赖及其版本。 有关信息, Spring boot version 1.5.3.RELEASE使用log4j version 2.7 ,该版本具有上述方法。



文章来源: Log4J 2 version not working with RabbitMQ & Spring Boot