【问题标题】:Disable Logging Messages for HttpClient禁用 HttpClient 的日志记录消息
【发布时间】:2015-05-21 07:41:04
【问题描述】:

我正在使用 Selenium 使用 TestNG 框架自动化测试用例。它调用内部使用 HTTPClient 库的 WebDriver。在我的类路径中有多个日志记录 jar,即 - slf4j 和 log4j。

我已经尝试了这里写的几乎所有东西 - Disable HttpClient logging 比如创建一个log4j.properties文件,commons-logging.properties文件然后添加到classpath-src/java/main

所以我有 2 个问题 -

  1. 如何强制 HttpClient 使用特定的日志记录 jar(因为我没有添加任何有关日志记录 jar 的日志记录依赖项。它们会自动下载,因为其他一些依赖项可能在内部使用它。)
  2. 如何禁用 HttpClient 的不需要的调试消息。

    SLF4J:类路径包含多个 SLF4J 绑定。 SLF4J:在 [jar:file:/C:/Users/~/.m2/repository/ch/qos/logback/logback-classic/1.0.7/logback-classic-1.0.7.jar!/org/ 中找到绑定slf4j/impl/StaticLoggerBinder.class] SLF4J:在 [jar:file:/C:/Users/~/.m2/repository/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar!/org/slf4j/ 中找到绑定impl/StaticLoggerBinder.class] SLF4J:在 [jar:file:/C:/Users/~/.m2/repository/org/apache/activemq/activemq-all/5.6.0/activemq-all-5.6.0.jar!/org/ 中找到绑定slf4j/impl/StaticLoggerBinder.class] SLF4J:请参阅http://www.slf4j.org/codes.html#multiple_bindings 以获得解释。 SLF4J:实际绑定的类型为 [ch.qos.logback.classic.util.ContextSelectorStaticBinder] 在端口 3052 上启动 ChromeDriver (v2.9.248315)

    18:27:00.726 [Forwarding newSession on session null to remote] DEBUG o.a.h.c.protocol.RequestAddCookies - CookieSpec selected: best-match
    18:27:00.727 [Forwarding newSession on session null to remote] DEBUG o.a.h.c.protocol.RequestAuthCache - Auth cache not set in the context
    18:27:00.727 [Forwarding newSession on session null to remote] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection request: [route: {}->http://localhost:3052][total kept alive: 0; route allocated: 0 of 2000; total allocated: 0 of 2000]
    18:27:00.758 [Forwarding newSession on session null to remote] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {}->http://localhost:3052][total kept alive: 0; route allocated: 1 of 2000; total allocated: 1 of 2000]
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG o.a.h.impl.execchain.MainClientExec - Opening connection {}->http://localhost:3052
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG o.a.h.i.c.HttpClientConnectionOperator - Connecting to localhost/127.0.0.1:3052
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG o.a.h.i.c.HttpClientConnectionOperator - Connection established 127.0.0.1:50744<->127.0.0.1:3052
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG o.a.h.impl.execchain.MainClientExec - Executing request POST /session HTTP/1.1
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG o.a.h.impl.execchain.MainClientExec - Target auth state: UNCHALLENGED
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG o.a.h.impl.execchain.MainClientExec - Proxy auth state: UNCHALLENGED
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG org.apache.http.headers - http-outgoing-0 >> POST /session HTTP/1.1
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Type: application/json; charset=utf-8
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Length: 169
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG org.apache.http.headers - http-outgoing-0 >> Host: localhost:3052
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG org.apache.http.headers - http-outgoing-0 >> Connection: Keep-Alive
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG org.apache.http.headers - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.3.4 (java 1.5)
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept-Encoding: gzip,deflate
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG org.apache.http.wire - http-outgoing-0 >> "POST /session HTTP/1.1[\r][\n]"
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Type: application/json; charset=utf-8[\r][\n]"
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Length: 169[\r][\n]"
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG org.apache.http.wire - http-outgoing-0 >> "Host: localhost:3052[\r][\n]"
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG org.apache.http.wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG org.apache.http.wire - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.3.4 (java 1.5)[\r][\n]"
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG org.apache.http.wire - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
    18:27:00.774 [Forwarding newSession on session null to remote] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
    

【问题讨论】:

    标签: logging apache-httpclient-4.x java.util.logging apache-commons-logging


    【解决方案1】:

    我有同样的问题,想隐藏这个日志:

    13:00:17.577 [main] DEBUG org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: default
    13:00:17.594 [main] DEBUG org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context
    

    我花了一些时间来寻找解决方案,我会尽量使其更通用。 httpclient 也可以使用自定义的日志框架。我在 spring rest api 项目中使用 httpclient。

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>
    
    1. 首先知道 httpclient 使用什么包装器进行日志记录。

      1.1 让我成功的方法是添加 slf4j-nop 作为我项目的依赖项。通过添加依赖项 SLF4J 由于 multiple_bindings 引发了异常。

      SLF4J: Class path contains multiple SLF4J bindings.
      SLF4J: Found binding in [jar:file:/Users/francisc/.m2/repository/ch/qos/logback/logback-classic/1.1.8/logback-classic-1.1.8.jar!/org/slf4j/impl/StaticLoggerBinder.class]
      SLF4J: Found binding in [jar:file:/Users/francisc/.m2/repository/org/slf4j/slf4j-nop/1.7.22/slf4j-nop-1.7.22.jar!/org/slf4j/impl/StaticLoggerBinder.class]
      SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
      

      1.2 第一个绑定是我们正在寻找的绑定。在我的例子中,jar 名称 logback-classic-1.1.8.jar 是我们需要查找配置以隐藏调试消息的包装器。

    2. 现在我们知道,我们正在使用 slf4j 和 Logback 记录器

      2.1 将 logback.xml 添加到您的项目 (src/main/resources/logback.xml)。

      2.2 配置示例,以启动并在以后根据需要进行配置。

      <configuration>
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
          <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
          </encoder>
        </appender>
      
        <logger name="deng" level="DEBUG"/>
      
        <root level="INFO">
          <appender-ref ref="STDOUT" />
        </root>
      </configuration>
      

    如果有不清楚的地方请评论。

    【讨论】:

      【解决方案2】:

      我能找到的解决这个问题的方法是首先检查日志 jar 的来源。为此,您可以通过在 cmd 上运行以下命令来制作 Maven 依赖树 -

      mvn dependency:tree  > test.txt
      

      现在打开这个 text.txt 文件并检查正在下载的 Logger.jar。识别后,您可以使用 pom.xml 中的排除标记排除此 jar

          <exclusions>
                  <exclusion>
                      <groupId>org.slf4j</groupId>
                      <artifactId>log4j-over-slf4j</artifactId>
                  </exclusion>
              </exclusions>
      

      【讨论】:

        猜你喜欢
        • 2011-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-24
        • 2012-02-08
        • 2016-02-20
        • 2011-10-07
        相关资源
        最近更新 更多