禁用HttpClient的日志记录消息

时间:2015-03-18 13:32:08

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

我使用Selenium使用TestNG Framework自动化TestCases。它调用WebDriver,它在内部使用HTTPClient lib。在我的classpath中有多个日志记录罐,即 - slf4j& log4j的。

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

所以我有两个问题 -

  1. 如何强制HttpClient使用特定的日志记录jar(因为我没有添加任何关于记录Jars的日志记录依赖关系。它们会自动下载,因为其他一些依赖项可能在内部使用它。)
  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]"
    

2 个答案:

答案 0 :(得分:1)

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

//user
function doSomething(error, result) {
  if (error) {
    return console.error(error);
  }
  // it's a given that "result" exists at this point
}

//caller (both lines should be valid)
doSomething(new Error('something happened'));
doSomething(null, 'resulting string');

我找到了一些寻找解决方案的好时机,我会尽力让它变得更加通用。 httpclient还可以使用自定义日志记录框架。我在spring rest api项目中使用httpclient。

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
  1. 首先要知道httpclient用于记录的包装器。

    1.1给我成功的方法是添加slf4j-nop作为我项目的依赖项。通过添加依赖项SLF4J因为multiple_bindings而抛出异常。

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>
    

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

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

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

    2.2配置示例,以便开始工作并在以后随时配置。

    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.
    
  3. 评论是否有不明确的事情。

答案 1 :(得分:0)

我能找到的解决这个问题的方法是先检查伐木罐的来源。为此,您可以通过在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>