最近在用HttpClient请求api的时候,控制台打印了太多无用的日志
14530 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> POST /api/Run/GetRoadRun HTTP/1.1
14530 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Length: 43
14530 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Type: application/json
14530 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Encoding: UTF-8
这些日志即会浪费性能也会覆盖有用的日志,查了官网,设置log4j,尝试在log4j.properties中这样设置
log4j.logger.httpclient.wire.header=DEBUG
log4j.logger.org.apache.commons.httpclient=DEBUG
并没有起作用,然后继续百度,查找到一篇关键的文章
https://blog.youkuaiyun.com/evanxuhe/article/details/79488672
才明白是SpringBoot使用logback框架的时候,commons-logging会自动转移到logback,所以需要设置与logback相关的配置文件。需要在resource路径下新建一个xml文件,添加下面的内容,设置里面的name和level就可以
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<!-- definition of appender STDOUT -->
<appender name="org.apache.http.wire" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<!-- appender referenced after it is defined -->
<appender-ref ref="org.apache.http.wire"/>
</root>
</configuration>
当把其中level="DEBUG"改为“WARN”就可以关闭这些日志了