今天有个小任务,需要调用接口同步数据,然后就写了一个JAVA类,打算直接本地执行main()去同步数据。
代码中使用了apache的httpclient来访问接口,发现输出日志太多了,
httpclient maven依赖如下:
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency>
日志输出示例如下:
11:37:01.845 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]" 11:37:01.845 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_121)[\r][\n]" 11:37:01.845 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]" 11:37:01.902 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 200 [\r][\n]" 11:37:01.903 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Date: Tue, 09 Jul 2019 03:37:03 GMT[\r][\n]" 11:37:01.903 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Content-Type: application/json;charset=UTF-8[\r][\n]" 11:37:01.903 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Transfer-Encoding: chunked[\r][\n]" 11:37:01.903 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Connection: keep-alive[\r][\n]" 11:37:01.903 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "X-Application-Context: " 11:37:01.903 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Set-Cookie: JSESSIONID=29D13760D2D3E2493442448AA192DBDF;path=/;HttpOnly[\r][\n]" 11:37:01.903 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Expires: 0[\r][\n]" 11:37:01.903 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "X-Content-Type-Options: nosniff[\r][\n]" 11:37:01.903 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "X-XSS-Protection: 1; mode=block[\r][\n]" 11:37:01.903 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "X-Frame-Options: DENY[\r][\n]"
密密麻麻一堆的DEBUG日志,乌压压的,都是没什么用的信息。
请求单个接口还好,如果请求接口过多就会很快的覆盖掉关键的日志。
Apache官网文档也没有找到对应的说明,可能是我找的方法不对?附上官网地址:http://hc.apache.org/httpcomponents-client-4.5.x/logging.html
然后百度也没有找到很好的解决办法,大家都是千篇一律的粘贴复制,很是头疼。
自己捣鼓好半天。
最后的解决办法还是老套路:项目中增加logback.xml文件,文件路径看各自的项目,我的是springboot,就放在了resources目录下。然后logback.xml文件中配置如下:
<configuration debug="false"> <logger name="org.apache" level="DEBUG" /> <logger name="org.apache.http.wire" level="DEBUG" /> <logger name="org.apache.http.headers" level="INFO" /> <property name="CONSOLE_LOG_PATTERN" value="%date{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) %magenta(%-4relative) --- [%yellow(%15.15thread)] %cyan(%-40.40logger{39}) : %msg%n"/> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> </encoder> </appender> <root level="ERROR"> <appender-ref ref="STDOUT"/> </root> </configuration>
org.apache.http.wire:配置wire输入的日志级别的
org.apache.http.headers:配置headers输入的日志级别的
org.apache:配置org.apache路径下的相关的日志的输出的
...
改其他的日志的输出也是同理
其实这个配置不用多说,很简单很基础的logback的配置,只不过一时没想起来。这就很尴尬...
日志以记之。
不是说跑的main方法么,改配置文件有用?不会被加载吧
@nopromises 日志使用的是logback,logback初始化加载配置文件顺序是logback-text.xml,logback.groovy,logback.xml,按顺序去读文件,文件存在就加载配置,main方法里面也会生效