2017-04-05 89 views
1

我正在尝试为Maven项目启用打印调试信息。我说的log4j作为依赖于pom.xml并添加log4j.properties以及log4j2.propertiesrootloger=DEBUG, sdoutsrc/main/resources文件夹。然后,在所需的班级中,我启动所需班级“org.pakage1.ClassA”中的记录程序并添加logger.debug()行,但是我的领事没有显示任何内容。当我检查logger.isDebugEnabled()返回false如何使用.properties文件和maven在Log4j中启用调试?

  • 的pom.xml

    <dependencies> 
    ..... 
    <dependency> 
        <groupId>org.slf4j</groupId> 
        <artifactId>slf4j-log4j12</artifactId> 
        <version>1.7.25</version> 
    </dependency> 
    

  • log4j.properties,同样log4j2

    log4j.debug=true 
    log4j.rootLogger=DEBUG, stdout 
    
    log4j.appender.Stdout.threshold=debug 
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
    log4j.appender.stdout.layout.ConversionPattern=%-5p %d [%t] %c{1} - %m%n 
    

    并作为额外的步骤,我尝试添加

    log4j.logger.extendedsldnf.ExtendedSLDNFEvaluator=DEBUG 
    

    ,但它也没有工作。

    package package1; 
    ...... 
    class ClassA{ 
        private Logger logger = LoggerFactory.getLogger(getClass()); 
        ...... 
        public static void main(String []args){ 
          logger.debug("message"); 
        } 
    
    
    } 
    

知道我是在收到警告

SLF4J: Class path contains multiple SLF4J bindings. 
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder] 

而且该项目其他子模块也有启用日志记录类似的,当在log4j.properties文件中所述调试消息行之有效

有什么遗漏吗?如何检查是否有冲突?

回答

1

加入log4j.properties以及log4j2.properties

第一个是为log4j的1,第二个是用于log4j的2 声明既不是解决问题的方式。
只使用哪一个匹配到您的实际log4j版本。

SLF4J:类路径包含多个SLF4J绑定。 SLF4J:实际 绑定类型的 [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

事实上,这个问题是相当清楚的:你想使用Log4j实现作为SLF4J结合你在类路径中至少有一个另外的SLF4J绑定。这里提到的是来自Logback的ContextSelectorStaticBinder,这是另一种绑定。
您必须在运行时在类路径中为SLF4J提供单个实现/绑定。

解决您的问题,这是相当简单的。
当您使用Maven时,我建议您从您的应用程序的agreggator pom执行mvn dependency:tree命令,或者如果您没有agreggator pom,请从打包应用程序的pom执行命令。
该命令将写入输出依赖关系(包括由您的pom拉动的传递依赖关系)。

它会输出这样的事情:

[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ Test-Spring-Boot --- 
[INFO] Test-Spring-Boot:Test-Spring-Boot:jar:0.0.1-SNAPSHOT 
[INFO] \- org.springframework.boot:spring-boot-starter-web:jar:1.4.4.RELEASE:compile 
[INFO] +- org.springframework.boot:spring-boot-starter:jar:1.4.4.RELEASE:compile 
[INFO] | +- org.springframework.boot:spring-boot:jar:1.4.4.RELEASE:compile 
[INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:1.4.4.RELEASE:compile 
[INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:1.4.4.RELEASE:compile 
[INFO] | | +- ch.qos.logback:logback-classic:jar:1.1.9:compile 
[INFO] | | | +- ch.qos.logback:logback-core:jar:1.1.9:compile 
[INFO] | | | \- org.slf4j:slf4j-api:jar:1.7.22:compile 
[INFO] | | +- org.slf4j:jcl-over-slf4j:jar:1.7.22:compile 
[INFO] | | +- org.slf4j:jul-to-slf4j:jar:1.7.22:compile 
[INFO] | | \- org.slf4j:log4j-over-slf4j:jar:1.7.22:compile 
[INFO] | +- org.springframework:spring-core:jar:4.3.6.RELEASE:compile 
[INFO] | \- org.yaml:snakeyaml:jar:1.17:runtime 
[INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.4.4.RELEASE:compile 
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.11:compile 
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.11:compile 
[INFO] | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.11: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.3:compile 
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.6:compile 
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.6:compile 
[INFO] | \- com.fasterxml.jackson.core:jackson-core:jar:2.8.6:compile 
[INFO] +- org.springframework:spring-web:jar:4.3.6.RELEASE:compile 
[INFO] | +- org.springframework:spring-aop:jar:4.3.6.RELEASE:compile 
[INFO] | +- org.springframework:spring-beans:jar:4.3.6.RELEASE:compile 
[INFO] | \- org.springframework:spring-context:jar:4.3.6.RELEASE:compile 
[INFO] \- org.springframework:spring-webmvc:jar:4.3.6.RELEASE:compile 
[INFO]  \- org.springframework:spring-expression:jar:4.3.6.RELEASE:compile 

你刚才从其中的logback依赖被拉识别。
一旦确定它,如果它是从我们自己的pom中显式声明的依赖项,就移除它的依赖项。
如果它是传递依赖项,use the exclusion dependency mechanism of Maven

+0

谢谢你对我的答案似乎是合理的,但有一点..为什么可能'logger.isDbugEnabled()'返回false,而配置文件是显式说明它是真的? –

+1

不用客气。因为它是在运行时使用的Logback配置,而不是Log4J配置。 – davidxxx

+0

我刚刚已经想通了,我发现,如果项目或其依赖项中有多个log4j.properties文件,只有其中一个被加载 –

1

如果您想使用log4j appender打印slf4j记录器的日志,请确保您有slf4j-log4j12 jar并删除logback jar,否则无法使用log4j或logback进行控制。

相关问题