Log4j2无法使用Sprint Boot找到日志记录实现

我试图在弹簧启动时使用log4j2时遇到此错误。

ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console... 

我已经遵循了这个指南: http : //docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html#howto-configure-log4j-for-logging – 并且还添加了来自https://logging.apache.org/log4j/2.x/maven-artifacts.html的两个log4j2依赖项

我的依赖:树看起来像这样:

 [INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ musikkjulekalender --- [INFO] no.saiboten:musikkjulekalender:war:1.0-SNAPSHOT [INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.4.0.RELEASE:compile [INFO] | +- org.hibernate:hibernate-validator:jar:5.2.4.Final:compile [INFO] | | +- javax.validation:validation-api:jar:1.1.0.Final:compile [INFO] | | +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile [INFO] | | \- com.fasterxml:classmate:jar:1.3.1:compile [INFO] | +- org.springframework:spring-web:jar:4.3.2.RELEASE:compile [INFO] | \- org.springframework:spring-webmvc:jar:4.3.2.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter-jetty:jar:1.4.0.RELEASE:compile [INFO] | +- org.eclipse.jetty:jetty-servlets:jar:9.3.11.v20160721:compile [INFO] | | +- org.eclipse.jetty:jetty-continuation:jar:9.3.11.v20160721:compile [INFO] | | +- org.eclipse.jetty:jetty-http:jar:9.3.11.v20160721:compile [INFO] | | \- org.eclipse.jetty:jetty-io:jar:9.3.11.v20160721:compile [INFO] | +- org.eclipse.jetty:jetty-webapp:jar:9.3.11.v20160721:compile [INFO] | | +- org.eclipse.jetty:jetty-xml:jar:9.3.11.v20160721:compile [INFO] | | \- org.eclipse.jetty:jetty-servlet:jar:9.3.11.v20160721:compile [INFO] | | \- org.eclipse.jetty:jetty-security:jar:9.3.11.v20160721:compile [INFO] | +- org.eclipse.jetty.websocket:websocket-server:jar:9.3.11.v20160721:compile [INFO] | | +- org.eclipse.jetty.websocket:websocket-common:jar:9.3.11.v20160721:compile [INFO] | | | \- org.eclipse.jetty.websocket:websocket-api:jar:9.3.11.v20160721:compile [INFO] | | +- org.eclipse.jetty.websocket:websocket-client:jar:9.3.11.v20160721:compile [INFO] | | \- org.eclipse.jetty.websocket:websocket-servlet:jar:9.3.11.v20160721:compile [INFO] | \- org.eclipse.jetty.websocket:javax-websocket-server-impl:jar:9.3.11.v20160721:compile [INFO] | +- org.eclipse.jetty:jetty-annotations:jar:9.3.11.v20160721:compile [INFO] | | +- org.eclipse.jetty:jetty-plus:jar:9.3.11.v20160721:compile [INFO] | | +- javax.annotation:javax.annotation-api:jar:1.2:compile [INFO] | | +- org.ow2.asm:asm:jar:5.0.1:compile [INFO] | | \- org.ow2.asm:asm-commons:jar:5.0.1:compile [INFO] | | \- org.ow2.asm:asm-tree:jar:5.0.1:compile [INFO] | +- org.eclipse.jetty.websocket:javax-websocket-client-impl:jar:9.3.11.v20160721:compile [INFO] | \- javax.websocket:javax.websocket-api:jar:1.0:compile [INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.4.0.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-aop:jar:1.4.0.RELEASE:compile [INFO] | | \- org.aspectj:aspectjweaver:jar:1.8.9:compile [INFO] | +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.4.0.RELEASE:compile [INFO] | | +- org.apache.tomcat:tomcat-jdbc:jar:8.5.4:compile [INFO] | | | \- org.apache.tomcat:tomcat-juli:jar:8.5.4:compile [INFO] | | \- org.springframework:spring-jdbc:jar:4.3.2.RELEASE:compile [INFO] | +- org.hibernate:hibernate-core:jar:5.0.9.Final:compile [INFO] | | +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile [INFO] | | +- org.javassist:javassist:jar:3.20.0-GA:compile [INFO] | | +- antlr:antlr:jar:2.7.7:compile [INFO] | | +- org.jboss:jandex:jar:2.0.0.Final:compile [INFO] | | +- dom4j:dom4j:jar:1.6.1:compile [INFO] | | | \- xml-apis:xml-apis:jar:1.4.01:compile [INFO] | | \- org.hibernate.common:hibernate-commons-annotations:jar:5.0.1.Final:compile [INFO] | +- org.hibernate:hibernate-entitymanager:jar:5.0.9.Final:compile [INFO] | +- javax.transaction:javax.transaction-api:jar:1.2:compile [INFO] | \- org.springframework:spring-aspects:jar:4.3.2.RELEASE:compile [INFO] +- com.h2database:h2:jar:1.4.192:compile [INFO] +- org.springframework.boot:spring-boot-starter-test:jar:1.4.0.RELEASE:test [INFO] | +- org.springframework.boot:spring-boot-test:jar:1.4.0.RELEASE:test [INFO] | +- org.springframework.boot:spring-boot-test-autoconfigure:jar:1.4.0.RELEASE:test [INFO] | +- com.jayway.jsonpath:json-path:jar:2.2.0:test [INFO] | | \- net.minidev:json-smart:jar:2.2.1:test [INFO] | | \- net.minidev:accessors-smart:jar:1.1:test [INFO] | +- org.assertj:assertj-core:jar:2.5.0:test [INFO] | +- org.mockito:mockito-core:jar:1.10.19:test [INFO] | | \- org.objenesis:objenesis:jar:2.1:test [INFO] | +- org.hamcrest:hamcrest-core:jar:1.3:compile [INFO] | +- org.hamcrest:hamcrest-library:jar:1.3:test [INFO] | +- org.skyscreamer:jsonassert:jar:1.3.0:test [INFO] | | \- org.json:json:jar:20140107:test [INFO] | +- org.springframework:spring-core:jar:4.3.2.RELEASE:compile [INFO] | \- org.springframework:spring-test:jar:4.3.2.RELEASE:test [INFO] +- org.springframework.boot:spring-boot-starter:jar:1.4.0.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot:jar:1.4.0.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:1.4.0.RELEASE:compile [INFO] | \- org.yaml:snakeyaml:jar:1.17:runtime [INFO] +- org.springframework.boot:spring-boot-starter-log4j2:jar:1.4.0.RELEASE:compile [INFO] | +- org.apache.logging.log4j:log4j-slf4j-impl:jar:2.6.2:compile [INFO] | +- org.slf4j:jcl-over-slf4j:jar:1.7.21:compile [INFO] | \- org.slf4j:jul-to-slf4j:jar:1.7.21:compile [INFO] +- org.apache.logging.log4j:log4j-api:jar:2.6.2:compile [INFO] +- org.apache.logging.log4j:log4j-core:jar:2.6.2:compile [INFO] +- org.apache.commons:commons-lang3:jar:3.3.2:compile [INFO] +- org.mongodb:mongo-java-driver:jar:2.12.4:compile [INFO] +- org.springframework.data:spring-data-mongodb:jar:1.9.2.RELEASE:compile [INFO] | +- org.springframework:spring-tx:jar:4.3.2.RELEASE:compile [INFO] | +- org.springframework:spring-context:jar:4.3.2.RELEASE:compile [INFO] | +- org.springframework:spring-beans:jar:4.3.2.RELEASE:compile [INFO] | +- org.springframework:spring-expression:jar:4.3.2.RELEASE:compile [INFO] | +- org.springframework.data:spring-data-commons:jar:1.12.2.RELEASE:compile [INFO] | \- org.slf4j:slf4j-api:jar:1.7.21:compile [INFO] +- com.google.api-client:google-api-client:jar:1.18.0-rc:compile [INFO] +- com.google.http-client:google-http-client-jackson2:jar:1.18.0-rc:compile [INFO] | \- com.google.http-client:google-http-client:jar:1.18.0-rc:compile [INFO] +- com.google.oauth-client:google-oauth-client:jar:1.18.0-rc:compile [INFO] | \- com.google.code.findbugs:jsr305:jar:1.3.9:compile [INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.2:compile [INFO] | \- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.1:compile [INFO] +- com.google.apis:google-api-services-oauth2:jar:v2-rev81-1.19.0:compile [INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.8.2:compile [INFO] +- org.mongodb.morphia:morphia:jar:0.107:compile [INFO] +- org.springframework.security:spring-security-web:jar:4.1.1.RELEASE:compile [INFO] | +- aopalliance:aopalliance:jar:1.0:compile [INFO] | \- org.springframework.security:spring-security-core:jar:4.1.1.RELEASE:compile [INFO] +- org.springframework.security:spring-security-config:jar:4.1.1.RELEASE:compile [INFO] | \- org.springframework:spring-aop:jar:4.3.2.RELEASE:compile [INFO] +- org.springframework.social:spring-social-config:jar:1.1.4.RELEASE:compile [INFO] | \- org.springframework.social:spring-social-web:jar:1.1.4.RELEASE:compile [INFO] | \- javax.inject:javax.inject:jar:1:compile [INFO] +- org.springframework.social:spring-social-core:jar:1.1.4.RELEASE:compile [INFO] +- org.springframework.social:spring-social-facebook:jar:2.0.3.RELEASE:compile [INFO] +- org.apache.httpcomponents:httpclient:jar:4.3.6:compile [INFO] | +- org.apache.httpcomponents:httpcore:jar:4.4.5:compile [INFO] | \- commons-codec:commons-codec:jar:1.10:compile [INFO] +- org.mockito:mockito-all:jar:1.10.8:compile [INFO] +- junit:junit:jar:4.11:compile [INFO] +- org.eclipse.jetty:apache-jsp:jar:9.3.11.v20160721:provided [INFO] | +- org.eclipse.jetty:jetty-util:jar:9.3.11.v20160721:compile [INFO] | +- org.eclipse.jetty:jetty-server:jar:9.3.11.v20160721:compile [INFO] | +- org.eclipse.jetty.toolchain:jetty-schemas:jar:3.1:provided [INFO] | +- javax.servlet:javax.servlet-api:jar:3.1.0:compile [INFO] | +- org.mortbay.jasper:apache-jsp:jar:8.0.33:provided [INFO] | | \- org.mortbay.jasper:apache-el:jar:8.0.33:provided [INFO] | \- org.eclipse.jdt.core.compiler:ecj:jar:4.4.2:provided [INFO] +- javax.servlet:jstl:jar:1.2:compile [INFO] +- org.springframework.data:spring-data-jpa:jar:1.10.2.RELEASE:compile [INFO] | +- org.springframework:spring-orm:jar:4.3.2.RELEASE:compile [INFO] | \- org.aspectj:aspectjrt:jar:1.8.9:compile [INFO] +- postgresql:postgresql:jar:9.1-901-1.jdbc4:compile [INFO] \- joda-time:joda-time:jar:2.9.4:compile 

我已使用默认配置将标准log4j2.xml文件添加到src / main / resources / config:

               

任何猜测? 我试过从Spring Tool Suite和maven spring-boot插件中运行。

根据流行的请求,这是完整的pom.xml:

   org.springframework.boot spring-boot-starter-parent 1.4.0.RELEASE  4.0.0 no.saiboten musikkjulekalender 1.0-SNAPSHOT war Musikkjulekalender   org.springframework.boot spring-boot-starter-web   org.springframework.boot spring-boot-starter-tomcat     org.springframework.boot spring-boot-starter-jetty   org.springframework.boot spring-boot-starter-data-jpa   com.h2database h2   org.springframework.boot spring-boot-starter-test test   org.springframework.boot spring-boot-starter   org.springframework.boot spring-boot-starter-logging     org.springframework.boot spring-boot-starter-log4j2   org.apache.logging.log4j log4j-api 2.6.2   org.apache.logging.log4j log4j-core 2.6.2   org.apache.commons commons-lang3 3.3.2   org.mongodb mongo-java-driver 2.12.4   org.springframework.data spring-data-mongodb   com.google.api-client google-api-client 1.18.0-rc   com.google.http-client google-http-client-jackson2 1.18.0-rc   com.google.oauth-client google-oauth-client 1.18.0-rc   com.fasterxml.jackson.core jackson-databind 2.8.2   com.google.apis google-api-services-oauth2 v2-rev81-1.19.0   com.fasterxml.jackson.core jackson-core 2.8.2   org.mongodb.morphia morphia 0.107   org.springframework.security spring-security-web   org.springframework.security spring-security-config   org.springframework.social spring-social-config   org.springframework.social spring-social-core   org.springframework.social spring-social-facebook   org.apache.httpcomponents httpclient 4.3.6   org.mockito mockito-all 1.10.8   junit junit 4.11   org.eclipse.jetty apache-jsp provided   javax.servlet jstl   org.springframework.data spring-data-jpa   postgresql postgresql 9.1-901-1.jdbc4   joda-time joda-time 2.9.4      src/main/resources     org.springframework.boot spring-boot-maven-plugin   org.apache.maven.plugins maven-surefire-plugin  false       spring-releases Spring Releases https://repo.spring.io/libs-release   org.jboss.repository.releases JBoss Maven Release Repository https://repository.jboss.org/nexus/content/repositories/releases     spring-releases Spring Releases https://repo.spring.io/libs-release    

当log4j-api模块无法找到或加载其接口的实现时,会生成该错误消息。 通常这意味着类路径中缺少log4j-core模块,但查看依赖图表似乎并非如此。

log4j-slf4j-impl和log4j-jul模块有一些传递依赖,但我认为这不会导致错误消息。

进一步调查的一种方法是尝试使用此系统属性集再次启动应用程序: -Dorg.apache.logging.log4j.simplelog.StatusLogger.level=TRACE 。 这将打印log4j内部调试日志记录到控制台。

(加载配置文件后,可以通过在log4j2.xml配置文件的开头设置来控制StatusLogger输出级别。但是,配置文件由log4j-core模块加载,并且我们还没有…)

我查找了LogManager.class的源代码,发现原因是在log4j-provider.properties中找不到LoggerContextFactory ,它应该在log4j-core.jar / META-INF中找到
因此,请检查log4j-core.jar / META-INF / log4j-provider.properties文件,或者可以删除本地存储库中的log4j-core.jar

它看起来你的依赖是正确的。 这是使用log4j2作为日志框架的工作spring-boot应用程序的pom:

    org.springframework.boot spring-boot-starter   org.springframework.boot spring-boot-starter-logging     org.springframework.boot spring-boot-starter-log4j2  

将log4j2.xml放入资源文件夹,以便从eclipse运行; 如果您更喜欢使用不同的目录 – resource / conf – 使用JVM参数提供log4j2配置的路径,如下所示:

-Dlog4j.configurationFile =” CONF / log4j2.xml”

不要放弃并从logback切换回log4j2 ……