Java HttpClient - 减少调试输出
Java HttpClient - reducing debug output
我需要对我们一直在开发的应用程序进行负载测试,但我们的控制台日志不断地充斥着输出。根据之前的经验,我了解到 System.out.println( )
是一个相对昂贵的操作,所以我想将打印减少到最低限度。
打印仅在应用程序尝试 Http-post 时发生,但我们每秒执行 100 次。我们使用 Apache 的 HttpClient 4.5.2 和 HttpCore 4.4.5。该应用程序运行于 Tomcat 7.
我尝试了很多不同的解决方案:删除 Tomcat 阀、设置 Tomcat 日志记录级别、设置 -Djavax.net.debug=none,但没有任何效果似乎删除了这个。有谁知道是什么导致了这些输出,我该如何关闭它们?
调试输出如下所示:
2016-10-11 15:24:42,942 DEBUG [http-bio-8080-exec-3] Connection request: [route: {}->http://localhost:8080][total kept alive: 0; route allocated: 0 of 200; total allocated: 0 of 200]
2016-10-11 15:24:42,956 DEBUG [http-bio-8080-exec-3] Connection leased: [id: 0][route: {}->http://localhost:8080][total kept alive: 0; route allocated: 1 of 200; total allocated: 1 of 200]
2016-10-11 15:24:42,958 DEBUG [http-bio-8080-exec-3] Connecting to localhost:8080
2016-10-11 15:24:42,978 DEBUG [http-bio-8080-exec-3] CookieSpec selected: best-match
2016-10-11 15:24:42,994 DEBUG [http-bio-8080-exec-3] Auth cache not set in the context
2016-10-11 15:24:42,995 DEBUG [http-bio-8080-exec-3] Target auth state: UNCHALLENGED
2016-10-11 15:24:42,996 DEBUG [http-bio-8080-exec-3] Proxy auth state: UNCHALLENGED
2016-10-11 15:24:42,996 DEBUG [http-bio-8080-exec-3] Attempt 1 to execute request
2016-10-11 15:24:42,997 DEBUG [http-bio-8080-exec-3] Sending request: POST /XtService/account/getbalance HTTP/1.1
2016-10-11 15:24:42,997 DEBUG [http-bio-8080-exec-3] >> "POST /XtService/account/getbalance HTTP/1.1[\r][\n]"
2016-10-11 15:24:42,999 DEBUG [http-bio-8080-exec-3] >> "Content-Length: 48[\r][\n]"
2016-10-11 15:24:42,999 DEBUG [http-bio-8080-exec-3] >> "Content-Type: application/x-www-form-urlencoded; charset=UTF-8[\r][\n]"
2016-10-11 15:24:43,000 DEBUG [http-bio-8080-exec-3] >> "Host: localhost:8080[\r][\n]"
2016-10-11 15:24:43,000 DEBUG [http-bio-8080-exec-3] >> "Connection: Keep-Alive[\r][\n]"
2016-10-11 15:24:43,000 DEBUG [http-bio-8080-exec-3] >> "User-Agent: Apache-HttpClient/4.2 (java 1.5)[\r][\n]"
2016-10-11 15:24:43,000 DEBUG [http-bio-8080-exec-3] >> "[\r][\n]"
2016-10-11 15:24:43,001 DEBUG [http-bio-8080-exec-3] >> POST /XtService/account/getbalance HTTP/1.1
2016-10-11 15:24:43,001 DEBUG [http-bio-8080-exec-3] >> Content-Length: 48
2016-10-11 15:24:43,001 DEBUG [http-bio-8080-exec-3] >> Content-Type: application/x-www-form-urlencoded; charset=UTF-8
2016-10-11 15:24:43,001 DEBUG [http-bio-8080-exec-3] >> Host: localhost:8080
2016-10-11 15:24:43,001 DEBUG [http-bio-8080-exec-3] >> Connection: Keep-Alive
2016-10-11 15:24:43,001 DEBUG [http-bio-8080-exec-3] >> User-Agent: Apache-HttpClient/4.2 (java 1.5)
2016-10-11 15:24:43,001 DEBUG [http-bio-8080-exec-3] >> "categoryid=2"
2016-10-11 15:24:43,256 DEBUG [http-bio-8080-exec-3] << "HTTP/1.1 200 OK[\r][\n]"
2016-10-11 15:24:43,259 DEBUG [http-bio-8080-exec-3] << "Server: Apache-Coyote/1.1[\r][\n]"
2016-10-11 15:24:43,260 DEBUG [http-bio-8080-exec-3] << "Pragma: no-cache[\r][\n]"
2016-10-11 15:24:43,260 DEBUG [http-bio-8080-exec-3] << "Cache-Control: no-cache[\r][\n]"
2016-10-11 15:24:43,260 DEBUG [http-bio-8080-exec-3] << "Expires: Thu, 01 Jan 1970 00:00:00 GMT[\r][\n]"
2016-10-11 15:24:43,261 DEBUG [http-bio-8080-exec-3] << "Content-Type: application/json;charset=utf-8[\r][\n]"
2016-10-11 15:24:43,261 DEBUG [http-bio-8080-exec-3] << "Content-Length: 83[\r][\n]"
2016-10-11 15:24:43,261 DEBUG [http-bio-8080-exec-3] << "Date: Tue, 11 Oct 2016 13:24:43 GMT[\r][\n]"
2016-10-11 15:24:43,261 DEBUG [http-bio-8080-exec-3] << "[\r][\n]"
2016-10-11 15:24:43,262 DEBUG [http-bio-8080-exec-3] Receiving response: HTTP/1.1 200 OK
2016-10-11 15:24:43,262 DEBUG [http-bio-8080-exec-3] << HTTP/1.1 200 OK
2016-10-11 15:24:43,263 DEBUG [http-bio-8080-exec-3] << Server: Apache-Coyote/1.1
2016-10-11 15:24:43,263 DEBUG [http-bio-8080-exec-3] << Pragma: no-cache
2016-10-11 15:24:43,263 DEBUG [http-bio-8080-exec-3] << Cache-Control: no-cache
2016-10-11 15:24:43,263 DEBUG [http-bio-8080-exec-3] << Expires: Thu, 01 Jan 1970 00:00:00 GMT
2016-10-11 15:24:43,263 DEBUG [http-bio-8080-exec-3] << Content-Type: application/json;charset=utf-8
2016-10-11 15:24:43,263 DEBUG [http-bio-8080-exec-3] << Content-Length: 83
2016-10-11 15:24:43,263 DEBUG [http-bio-8080-exec-3] << Date: Tue, 11 Oct 2016 13:24:43 GMT
2016-10-11 15:24:43,265 DEBUG [http-bio-8080-exec-3] Connection can be kept alive for 10000 MILLISECONDS
2016-10-11 15:24:43,274 DEBUG [http-bio-8080-exec-3] << "{"status":1,"data":{"RESERVED":569.81}}"
2016-10-11 15:24:43,275 DEBUG [http-bio-8080-exec-3] Connection [id: 0][route: {}->http://localhost:8080] can be kept alive for 10000 MILLISECONDS
2016-10-11 15:24:43,275 DEBUG [http-bio-8080-exec-3] Connection released: [id: 0][route: {}->http://localhost:8080][total kept alive: 1; route allocated: 1 of 200; total allocated: 1 of 200]
你可以看看:http://hc.apache.org/httpcomponents-client-ga/logging.html。
Therefore HttpClient utilizes the logging interface provided by the Commons Logging package. Commons Logging provides a simple and generalized log interface to various logging packages. By using Commons Logging, HttpClient can be configured for a variety of different logging behaviours. That means the user will have to make a choice which logging framework to use. By default Commons Logging supports the following logging frameworks:
- Log4J
- java.util.logging
- SimpleLog (internal to Commons Logging)
您观察到的绝大多数输出实际上是 "wire" 日志记录(即实际的 HTTP 流量):
The wire log is used to log all data transmitted to and from servers when executing HTTP requests. The wire log uses the org.apache.http.wire
logging category. This log should only be enabled to debug problems, as it will produce an extremely large amount of log data.
简而言之:找到使用了哪个日志记录实现(例如 log4J?),找到它的配置文件,并减少 org.apache.http(.wire)
类别
我需要对我们一直在开发的应用程序进行负载测试,但我们的控制台日志不断地充斥着输出。根据之前的经验,我了解到 System.out.println( )
是一个相对昂贵的操作,所以我想将打印减少到最低限度。
打印仅在应用程序尝试 Http-post 时发生,但我们每秒执行 100 次。我们使用 Apache 的 HttpClient 4.5.2 和 HttpCore 4.4.5。该应用程序运行于 Tomcat 7.
我尝试了很多不同的解决方案:删除 Tomcat 阀、设置 Tomcat 日志记录级别、设置 -Djavax.net.debug=none,但没有任何效果似乎删除了这个。有谁知道是什么导致了这些输出,我该如何关闭它们?
调试输出如下所示:
2016-10-11 15:24:42,942 DEBUG [http-bio-8080-exec-3] Connection request: [route: {}->http://localhost:8080][total kept alive: 0; route allocated: 0 of 200; total allocated: 0 of 200]
2016-10-11 15:24:42,956 DEBUG [http-bio-8080-exec-3] Connection leased: [id: 0][route: {}->http://localhost:8080][total kept alive: 0; route allocated: 1 of 200; total allocated: 1 of 200]
2016-10-11 15:24:42,958 DEBUG [http-bio-8080-exec-3] Connecting to localhost:8080
2016-10-11 15:24:42,978 DEBUG [http-bio-8080-exec-3] CookieSpec selected: best-match
2016-10-11 15:24:42,994 DEBUG [http-bio-8080-exec-3] Auth cache not set in the context
2016-10-11 15:24:42,995 DEBUG [http-bio-8080-exec-3] Target auth state: UNCHALLENGED
2016-10-11 15:24:42,996 DEBUG [http-bio-8080-exec-3] Proxy auth state: UNCHALLENGED
2016-10-11 15:24:42,996 DEBUG [http-bio-8080-exec-3] Attempt 1 to execute request
2016-10-11 15:24:42,997 DEBUG [http-bio-8080-exec-3] Sending request: POST /XtService/account/getbalance HTTP/1.1
2016-10-11 15:24:42,997 DEBUG [http-bio-8080-exec-3] >> "POST /XtService/account/getbalance HTTP/1.1[\r][\n]"
2016-10-11 15:24:42,999 DEBUG [http-bio-8080-exec-3] >> "Content-Length: 48[\r][\n]"
2016-10-11 15:24:42,999 DEBUG [http-bio-8080-exec-3] >> "Content-Type: application/x-www-form-urlencoded; charset=UTF-8[\r][\n]"
2016-10-11 15:24:43,000 DEBUG [http-bio-8080-exec-3] >> "Host: localhost:8080[\r][\n]"
2016-10-11 15:24:43,000 DEBUG [http-bio-8080-exec-3] >> "Connection: Keep-Alive[\r][\n]"
2016-10-11 15:24:43,000 DEBUG [http-bio-8080-exec-3] >> "User-Agent: Apache-HttpClient/4.2 (java 1.5)[\r][\n]"
2016-10-11 15:24:43,000 DEBUG [http-bio-8080-exec-3] >> "[\r][\n]"
2016-10-11 15:24:43,001 DEBUG [http-bio-8080-exec-3] >> POST /XtService/account/getbalance HTTP/1.1
2016-10-11 15:24:43,001 DEBUG [http-bio-8080-exec-3] >> Content-Length: 48
2016-10-11 15:24:43,001 DEBUG [http-bio-8080-exec-3] >> Content-Type: application/x-www-form-urlencoded; charset=UTF-8
2016-10-11 15:24:43,001 DEBUG [http-bio-8080-exec-3] >> Host: localhost:8080
2016-10-11 15:24:43,001 DEBUG [http-bio-8080-exec-3] >> Connection: Keep-Alive
2016-10-11 15:24:43,001 DEBUG [http-bio-8080-exec-3] >> User-Agent: Apache-HttpClient/4.2 (java 1.5)
2016-10-11 15:24:43,001 DEBUG [http-bio-8080-exec-3] >> "categoryid=2"
2016-10-11 15:24:43,256 DEBUG [http-bio-8080-exec-3] << "HTTP/1.1 200 OK[\r][\n]"
2016-10-11 15:24:43,259 DEBUG [http-bio-8080-exec-3] << "Server: Apache-Coyote/1.1[\r][\n]"
2016-10-11 15:24:43,260 DEBUG [http-bio-8080-exec-3] << "Pragma: no-cache[\r][\n]"
2016-10-11 15:24:43,260 DEBUG [http-bio-8080-exec-3] << "Cache-Control: no-cache[\r][\n]"
2016-10-11 15:24:43,260 DEBUG [http-bio-8080-exec-3] << "Expires: Thu, 01 Jan 1970 00:00:00 GMT[\r][\n]"
2016-10-11 15:24:43,261 DEBUG [http-bio-8080-exec-3] << "Content-Type: application/json;charset=utf-8[\r][\n]"
2016-10-11 15:24:43,261 DEBUG [http-bio-8080-exec-3] << "Content-Length: 83[\r][\n]"
2016-10-11 15:24:43,261 DEBUG [http-bio-8080-exec-3] << "Date: Tue, 11 Oct 2016 13:24:43 GMT[\r][\n]"
2016-10-11 15:24:43,261 DEBUG [http-bio-8080-exec-3] << "[\r][\n]"
2016-10-11 15:24:43,262 DEBUG [http-bio-8080-exec-3] Receiving response: HTTP/1.1 200 OK
2016-10-11 15:24:43,262 DEBUG [http-bio-8080-exec-3] << HTTP/1.1 200 OK
2016-10-11 15:24:43,263 DEBUG [http-bio-8080-exec-3] << Server: Apache-Coyote/1.1
2016-10-11 15:24:43,263 DEBUG [http-bio-8080-exec-3] << Pragma: no-cache
2016-10-11 15:24:43,263 DEBUG [http-bio-8080-exec-3] << Cache-Control: no-cache
2016-10-11 15:24:43,263 DEBUG [http-bio-8080-exec-3] << Expires: Thu, 01 Jan 1970 00:00:00 GMT
2016-10-11 15:24:43,263 DEBUG [http-bio-8080-exec-3] << Content-Type: application/json;charset=utf-8
2016-10-11 15:24:43,263 DEBUG [http-bio-8080-exec-3] << Content-Length: 83
2016-10-11 15:24:43,263 DEBUG [http-bio-8080-exec-3] << Date: Tue, 11 Oct 2016 13:24:43 GMT
2016-10-11 15:24:43,265 DEBUG [http-bio-8080-exec-3] Connection can be kept alive for 10000 MILLISECONDS
2016-10-11 15:24:43,274 DEBUG [http-bio-8080-exec-3] << "{"status":1,"data":{"RESERVED":569.81}}"
2016-10-11 15:24:43,275 DEBUG [http-bio-8080-exec-3] Connection [id: 0][route: {}->http://localhost:8080] can be kept alive for 10000 MILLISECONDS
2016-10-11 15:24:43,275 DEBUG [http-bio-8080-exec-3] Connection released: [id: 0][route: {}->http://localhost:8080][total kept alive: 1; route allocated: 1 of 200; total allocated: 1 of 200]
你可以看看:http://hc.apache.org/httpcomponents-client-ga/logging.html。
Therefore HttpClient utilizes the logging interface provided by the Commons Logging package. Commons Logging provides a simple and generalized log interface to various logging packages. By using Commons Logging, HttpClient can be configured for a variety of different logging behaviours. That means the user will have to make a choice which logging framework to use. By default Commons Logging supports the following logging frameworks:
- Log4J
- java.util.logging
- SimpleLog (internal to Commons Logging)
您观察到的绝大多数输出实际上是 "wire" 日志记录(即实际的 HTTP 流量):
The wire log is used to log all data transmitted to and from servers when executing HTTP requests. The wire log uses the
org.apache.http.wire
logging category. This log should only be enabled to debug problems, as it will produce an extremely large amount of log data.
简而言之:找到使用了哪个日志记录实现(例如 log4J?),找到它的配置文件,并减少 org.apache.http(.wire)
类别