有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用在Log4j中启用调试。属性文件和maven?

我正在尝试为maven项目启用打印调试信息。我将log4j作为依赖项添加到pom.xml,并将log4j.properties以及log4j2.propertiesrootloger=DEBUG, sdout一起添加到src/main/resources文件夹。然后在所需的类中,我在所需的类“org”中启动一个记录器。包装1。ClassA'和addlogger.debug()行,但在我的领事中没有显示任何内容。当我选中logger.isDebugEnabled()时,它返回false

  • pom。xml

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

  • log4j。属性和类似的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文件中声明时运行良好

有什么东西不见了吗?如何检查是否存在冲突


共 (2) 个答案

  1. # 1 楼答案

    如果您想使用log4j appender打印slf4j记录器的日志,请确保您有slf4j-log4j12 jar并移除logback jar,否则无论使用log4j还是logback,您都无法控制

  2. # 2 楼答案

    added log4j.properties as well as log4j2.properties

    第一个用于log4j 1,第二个用于log4j 2。 声明两者都不是解决问题的方法
    仅使用与实际log4j版本匹配的版本

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

    事实上,问题相当清楚:您希望使用log4J实现作为SLF4J绑定,但在运行时类路径中至少有一个SLF4J绑定。这里提到了另一个绑定Logback中的ContextSelectorStaticBinder
    在运行时,必须在类路径中为SLF4J提供单个实现/绑定

    要解决你的问题,很简单
    在使用Maven时,我建议您从应用程序的agreegator pom执行mvn dependency:tree命令,如果您没有agreegator pom,则从打包应用程序的pom执行mvn dependency:tree命令
    此命令将作为输出写入依赖项(包括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
    

    您只需确定从何处提取回登录依赖项
    一旦识别了它,如果它是从我们自己的pom显式声明的依赖项,那么就删除它
    如果它是可传递依赖项,use the exclusion dependency mechanism of Maven