前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >屏蔽禁用main方法RestTemplate控制台debug日志

屏蔽禁用main方法RestTemplate控制台debug日志

作者头像
九转成圣
发布2024-04-10 16:29:25
470
发布2024-04-10 16:29:25
举报
文章被收录于专栏:csdncsdn

背景

springboot项目,在main方法中用RestTemplate如果请求比较多,会被RestTemplate的debug日志刷屏,效果如下:

代码语言:javascript
复制
09:15:47.154 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https://xxx.cn/operation/customer/alarm/%E5%A7%94%E6%89%98%E6%8E%88%E6%9D%83%E4%B9%A6%E6%A8%A1%E7%89%8896043f1b-36bc-46bc-80fb-20eaedd9f532.pdf
09:15:47.154 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[application/octet-stream, application/json, application/*+json, */*]
09:15:47.163 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
09:15:47.163 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [[B] as "application/pdf"
09:15:47.165 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https://xxx.cn/operation/customer/alarm/%E5%A7%94%E6%89%98%E6%8E%88%E6%9D%83%E4%B9%A6%E6%A8%A1%E7%89%88ece04506-f90e-4913-854f-1784a2240e12.pdf
09:15:47.165 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[application/octet-stream, application/json, application/*+json, */*]
09:15:47.176 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
09:15:47.176 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [[B] as "application/pdf"
09:15:47.177 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https://xxx.cn/operation/station/alarm/%E6%B5%8B%E8%AF%95%E7%8E%AF%E5%A2%83%E5%B7%A1%E6%A3%80%E4%BB%BB%E5%8A%A1%E4%B8%AD%E5%92%8C%E5%AF%BC%E5%87%BA%E7%9A%84%E6%96%87%E4%BB%B6%E4%B8%AD%EF%BC%8C%E8%A6%86%E7%9B%96%E7%8E%87%EF%BC%8C%E5%9D%87%E5%BA%94%E8%AF%A5%E4%BF%9D%E7%95%99%E4%B8%A4%E4%BD%8D%E5%B0%8F%E6%95%B0fbe2b6f4-a2fa-455b-a45e-32fe56fb61f5.png
09:15:47.177 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[application/octet-stream, application/json, application/*+json, */*]
09:15:47.186 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
09:15:47.186 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [[B] as "image/png"
09:15:47.206 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https://xxx.cn/operation/customer/alarm/%E5%8F%A4%E9%A3%8E%E6%B0%B4%E5%A2%A8%E8%83%8C%E6%99%AF19592710-ab81-48f5-9d74-481ed33543fe.jpg
09:15:47.206 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[application/octet-stream, application/json, application/*+json, */*]
09:15:47.215 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
09:15:47.215 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [[B] as "image/jpeg"
09:15:47.220 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https://xxx.cn/operation/station/alarm/%E7%AE%A1%E7%BD%91%E5%9C%B0%E5%9B%BE-%E6%92%92%E7%82%B9%E5%8F%B3%E9%94%AE-%E8%AF%A6%E6%83%85%EF%BC%8C%E8%AE%BE%E5%A4%87%E8%AF%A6%E6%83%85%E9%A1%B5%E4%B8%AD%E2%80%9C%E8%B0%83%E5%8E%8B%E7%AE%B1%E7%AB%A3%E5%B7%A5%E6%97%B6%E9%97%B4%E2%80%9D%E8%B0%83%E6%95%B4%E4%B8%BA%E2%80%9C%E7%AB%A3%E5%B7%A5%E6%97%B6%E9%97%B4%E2%80%9D5fd1ed26-a984-4b97-a86e-e63a2cc5e46f.png
09:15:47.220 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[application/octet-stream, application/json, application/*+json, */*]
09:15:47.232 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
09:15:47.232 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [[B] as "image/png"
09:15:47.255 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https://xxx.cn/operation/station/alarm/%E6%90%9C%E7%B4%A2%E5%87%BA%E6%9D%A5%E7%9A%84%E7%BB%93%E6%9E%9C%E5%88%97%E8%A1%A8%E4%B8%AD%EF%BC%8C%E5%BA%94%E8%AF%A5%E5%B1%95%E7%A4%BA%E4%B8%BA%E2%80%9C%E8%B0%83%E5%8E%8B%E8%AE%BE%E5%A4%87%E2%80%9D5c5c88a6-8ce7-47dd-a4b9-3a0b222ebfa6.png
09:15:47.255 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[application/octet-stream, application/json, application/*+json, */*]
09:15:47.265 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
09:15:47.265 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [[B] as "image/png"
09:15:47.295 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https://xxx.cn/operation/customer/alarm/wallhaven-rrqveqd78d9fa3-36ef-4f4c-a44f-0dd961626688.jpg
09:15:47.295 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[application/octet-stream, application/json, application/*+json, */*]
09:15:47.327 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
09:15:47.327 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [[B] as "image/jpeg"
09:15:47.379 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https://xxx.cn/operation/customer/alarm/%E8%8D%89%E5%9C%B0%E4%B8%8A%E7%9A%84%E8%8D%89%EF%BC%8C%E7%94%A8%E5%A2%A8%E6%B0%B4%E6%89%8B%E7%BB%98345ee7e8-cadb-4e3e-8c20-262575d6fa82.jpg
09:15:47.380 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[application/octet-stream, application/json, application/*+json, */*]
09:15:47.391 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
09:15:47.391 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [[B] as "image/jpeg"
09:15:47.407 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https://xxx.cn/operation/customer/alarm/%E8%8D%89%E5%9C%B0%E4%B8%8A%E7%9A%84%E8%8D%89%EF%BC%8C%E7%94%A8%E5%A2%A8%E6%B0%B4%E6%89%8B%E7%BB%98df3bd32b-07d3-4b53-bec5-ded379e84f2a.jpg
09:15:47.407 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[application/octet-stream, application/json, application/*+json, */*]
09:15:47.421 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
09:15:47.421 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [[B] as "image/jpeg"
09:15:47.432 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https://xxx.cn/operation/customer/alarm/%E8%8D%89%E5%9C%B0%E4%B8%8A%E7%9A%84%E8%8D%89%EF%BC%8C%E7%94%A8%E5%A2%A8%E6%B0%B4%E6%89%8B%E7%BB%9860ac7533-f30c-41a3-9e77-e5ef2d38cfba.jpg
09:15:47.432 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[application/octet-stream, application/json, application/*+json, */*]
09:15:47.440 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
09:15:47.441 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [[B] as "image/jpeg"
09:15:47.456 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https://xxx.cn/operation/customer/alarm/%E5%8F%A4%E8%80%81%E7%9A%84%E4%B8%AD%E5%9B%BD%E8%A3%85%E9%A5%B0ef83fb3e-80c9-4837-b4ac-83cb6c5073cc.jpg
09:15:47.456 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[application/octet-stream, application/json, application/*+json, */*]
09:15:47.465 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
09:15:47.465 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [[B] as "image/jpeg"
09:15:47.485 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https://xxx.cn/operation/project/alarm/da70238946b37f89134e95413772b59d2b8ba26d-17b4-49b0-b115-367b562867ea.jpg
09:15:47.486 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[application/octet-stream, application/json, application/*+json, */*]
09:15:47.545 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
09:15:47.546 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [[B] as "image/jpeg"
09:15:47.547 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https://xxx.cn/operation/project/alarm/%E5%AE%89%E5%85%A8%E7%8A%B6%E6%80%81%E8%AF%84%E4%BC%B0%E5%AE%9E%E6%97%B6%E8%AF%84%E5%88%86%E6%8A%A5%E5%91%8A20230530%20(1)d40a9209-64fc-4244-a45c-e3ac87c3e1fb.pdf
09:15:47.547 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[application/octet-stream, application/json, application/*+json, */*]
09:15:47.557 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
09:15:47.557 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [[B] as "application/pdf"
09:15:47.558 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https://xxx.cn/operation/station/alarm/%E6%90%9C%E7%B4%A2%E5%87%BA%E6%9D%A5%E7%9A%84%E7%BB%93%E6%9E%9C%E5%88%97%E8%A1%A8%E4%B8%AD%EF%BC%8C%E5%BA%94%E8%AF%A5%E5%B1%95%E7%A4%BA%E4%B8%BA%E2%80%9C%E8%B0%83%E5%8E%8B%E8%AE%BE%E5%A4%87%E2%80%9D56e7f40b-5b6b-4b79-acd8-c30d4add7f4d.png
09:15:47.558 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[application/octet-stream, application/json, application/*+json, */*]
09:15:47.573 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
09:15:47.574 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [[B] as "image/png"

解决方案

代码语言:javascript
复制
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;
​
public class RestTemplateTest {
   public static void main(String[] args) {
       // Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
       Logger logger = (Logger) LoggerFactory.getLogger("ROOT");
       logger.setLevel(Level.INFO);
       RestTemplate restTemplate = new RestTemplate();
       String response = restTemplate.getForObject("https://www.baidu.com/", String.class);
       System.out.println("response = " + response);
​
   }
}

运行效果

代码语言:javascript
复制
response = <!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>ç¾åº¦ä¸ä¸ï¼ä½ å°±ç¥é</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=ç¾åº¦ä¸ä¸ class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>æ°é»</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>å°å¾</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>è§é¢</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>è´´å§</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>ç»å½</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">ç»å½</a>');
                </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">æ´å¤äº§å</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>å³äºç¾åº¦</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使ç¨ç¾åº¦åå¿è¯»</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>æè§åé¦</a>&nbsp;京ICPè¯030173å·&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

可以看到在运行之后不再有debug日志了

说明

Logger.ROOT_LOGGER_NAME是Logger接口定义的常量,值是"ROOT"

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-03-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • 解决方案
  • 运行效果
  • 说明
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档