在 HTTPComponents 中静默调试日志记录
Silence Debug Logging in HTTPComponents
如何使 http 组件中的调试日志静音?
09:23:22.145 [main] DEBUG o.a.h.c.protocol.RequestAddCookies - CookieSpec selected: default
09:23:22.145 [main] DEBUG o.a.h.c.protocol.RequestAuthCache - Auth cache not set in the context
09:23:22.145 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection request: [route: {}->http://example.org:80][total kept alive: 1; route allocated: 1 of 100; total allocated: 1 of 200]
09:23:22.145 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {}->http://diagnostics-uat.corp.apple.com:80][total kept alive: 0; route allocated: 1 of 100; total allocated: 1 of 200]
09:23:22.145 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Executing request POST /evt/detect HTTP/1.1
09:23:22.145 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Target auth state: UNCHALLENGED
09:23:22.145 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Proxy auth state: UNCHALLENGED
09:23:22.145 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> POST /evt/detect HTTP/1.1
09:23:22.145 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Length: 92
我尝试过基于代码的解决方案:
static {
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "warn");
}
public static void main(String[] args) throws Exception {
还有这个log4j.properties
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%c] %m%n
log4j.logger.org.apache.http=WARN
log4j.logger.org.apache.http.wire=WARN
但都没有用。感谢帮助,谢谢!
您的设置正确可能的原因可能是类路径中有另一个 log4j 配置文件并覆盖了您的配置。所以我会推荐这个。
- 确保你的 log4j.properties 文件在类路径中。使用以下命令查看进程打开的文件(假设是 unix)
lsof -p java-process-pid | grep log4j
- 在您的运行时文件夹中搜索 log4j.properties 并删除所有重复文件(如果可能)- 否则全部更改。
- 搜索 log4j.xml 并执行相同操作。
有时我看到第三方 jar 附带 log4j.xml 或 log4j.properties 所以在最坏的情况下使用以下命令检查所有 jar
jar -tvf a.jar | grep log4j
以编程方式,可以通过以下方式将其设置为静音:
(Apache HttpComponents 4.3.5)
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
...
Logger.getLogger("org.apache.http") .setLevel(Level.WARN);
Logger.getLogger("org.apache.http.wire") .setLevel(Level.WARN);
Logger.getLogger("org.apache.http.headers").setLevel(Level.WARN);
如何使 http 组件中的调试日志静音?
09:23:22.145 [main] DEBUG o.a.h.c.protocol.RequestAddCookies - CookieSpec selected: default
09:23:22.145 [main] DEBUG o.a.h.c.protocol.RequestAuthCache - Auth cache not set in the context
09:23:22.145 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection request: [route: {}->http://example.org:80][total kept alive: 1; route allocated: 1 of 100; total allocated: 1 of 200]
09:23:22.145 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {}->http://diagnostics-uat.corp.apple.com:80][total kept alive: 0; route allocated: 1 of 100; total allocated: 1 of 200]
09:23:22.145 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Executing request POST /evt/detect HTTP/1.1
09:23:22.145 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Target auth state: UNCHALLENGED
09:23:22.145 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Proxy auth state: UNCHALLENGED
09:23:22.145 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> POST /evt/detect HTTP/1.1
09:23:22.145 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Length: 92
我尝试过基于代码的解决方案:
static {
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "warn");
}
public static void main(String[] args) throws Exception {
还有这个log4j.properties
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%c] %m%n
log4j.logger.org.apache.http=WARN
log4j.logger.org.apache.http.wire=WARN
但都没有用。感谢帮助,谢谢!
您的设置正确可能的原因可能是类路径中有另一个 log4j 配置文件并覆盖了您的配置。所以我会推荐这个。 - 确保你的 log4j.properties 文件在类路径中。使用以下命令查看进程打开的文件(假设是 unix)
lsof -p java-process-pid | grep log4j
- 在您的运行时文件夹中搜索 log4j.properties 并删除所有重复文件(如果可能)- 否则全部更改。
- 搜索 log4j.xml 并执行相同操作。
有时我看到第三方 jar 附带 log4j.xml 或 log4j.properties 所以在最坏的情况下使用以下命令检查所有 jar
jar -tvf a.jar | grep log4j
以编程方式,可以通过以下方式将其设置为静音:
(Apache HttpComponents 4.3.5)
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
...
Logger.getLogger("org.apache.http") .setLevel(Level.WARN);
Logger.getLogger("org.apache.http.wire") .setLevel(Level.WARN);
Logger.getLogger("org.apache.http.headers").setLevel(Level.WARN);