首页 文章

禁用HttpClient的日志记录消息

提问于
浏览
0

我正在使用Selenium使用TestNG Framework自动化TestCases . 它调用WebDriver,它在内部使用HTTPClient lib . 在我的类路径中有多个日志记录 jar ,即 - slf4j和log4j .

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

所以我有两个问题 -

  • 如何强制HttpClient使用特定的日志jar(因为我没有添加任何关于日志记录jar的日志记录依赖关系 . 它们会自动下载,因为其他一些依赖项可能在内部使用它 . )

  • 如何禁用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]"

2 回答

  • 0

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

    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>
    
    • 首先要知道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第一个绑定是我们正在寻找的绑定 . 我的案例logback-classic-1.1.8.jar中的jar名称是我们需要查找隐藏调试消息的配置所需的包装器 .

    • 现在我们知道,我们正在使用带有Logback Logger 的slf4j

    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>
    

    评论是否有不明确的事情 .

  • 1

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

    mvn dependency:tree  > test.txt
    

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

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

相关问题