Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turning on Logging in HttpClient

Tags:

java

logging

How do you properly turn on Logging for Apache Commons HttpClient

Right now I am doing this and getting no log messages except the one I triggered explicitly as a test:

public class HttpTest1 {     static Log log = LogFactory.getLog(Class.class);  public static void main(String[] args) throws ClientProtocolException, IOException{         log.warn("Logging Works");         System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");          System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");         System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");         System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug");          DefaultHttpClient client = new DefaultHttpClient();          HttpGet method = new HttpGet("http://www.google.com");          HttpResponse response = client.execute(method);         BufferedReader rd = new BufferedReader(new InputStreamReader(                 response.getEntity().getContent()));          String line = "";         while ((line = rd.readLine()) != null) {             System.out.println(line);         }     }      } 

Output:

Feb 28, 2012 2:16:31 PM httpTest.HttpTest1 main WARNING: Logging Works <!doctype html><html itemscope itemtype="http://schema.org/WebPage"><head><meta http-equiv="content-type" content="text/html; charset=ISO-8859-2"><meta itemprop="image" content="/images/google_favicon_128.png"><title>Google</title><script>window.google={kEI:"stNMT4yfMInFtAaK_5yGDw",getEI:function(a){var d;while(a&&!(a.getAttribute&&(d=a.getAttribute("eid"))))a=a.parentNode;return d||google.kEI},https:function(){return window.location.protocol=="https:"},kEXPI:"33551,34324,34904,35055,35091,36604,36683,36934,36946,37017",kCSI:{e:"33551,34324,34904,35055,35091,36604,36683,36934,36946,37017",ei:"stNMT4yfMInFtAaK_5yGDw"},authuser:0, ml:function(){},kHL:"sl",time:function(){return(new Date).getTime()},log:function(a,d,f,h){var e=new Image,g=google,k=g.lc,i=g.li,m="";e.onerror=(e.onload=(e.onabort=function(){delete k[i]}));k[i]=e;if(!f&&d.search("&ei=")==-1)m="&ei="+google.getEI(h);var j=f||"/gen_204?atyp=i&ct="+a+"&cad="+d+m+"&zx="+google.time(),b=/^http:/i;if(b.test(j)&&google.https()){google.ml(new Error("GLMM"),false,{src:j}); delete k[i];return}e.src=j;g.li=i+1},lc:[],li:0,Toolbelt:{},y:{},x:function(a,d){google.y[a.id]= [a,d];return false}};(function(){var a= google.kEI;window.pp_tos={};var d=document.domain.replace(/.*?(google\..*)/,"$1");function f(){var b=0;try{var c=document.cookie.match(/PP_TOS_ACK=([^;]*)/);b=c&&!isNaN(c[1])?c[1]:0}catch(l){e("error","read_cookie")}return b}function h(b){var c=f();if(c>=0){g(++c);if(f()>0&&!b)m()}}function e(b,c){var l=new Image;c=c?"&emsg="+c:"";l.src=["//",d,"/gen_204?atyp=i&ct=pp_tos&cd=",b,"&source=","web","&ei=",a,c].join("");window.pp_tos.beacon=l}function g(b){var c=new Date((new Date).getTime()+5184000000); try{var l="PP_TOS_ACK="+b+"; expires="+c.toGMTString()+"; path=/; domain="+d;document.cookie=l}catch(n){e("error","write_cookie")}}function k(){g(-1);e("dismiss");j()}function i(){g(-1);e("tellmore");j()}function m(){var b=document.getElementById("bb_pp_tos_ack");if(b){b.style.display="";e("display")}}function j(){var b=document.getElementById("bb_pp_tos_ack");if(b)b.style.display="none"}window.pp_tos.handleDismiss=k;window.pp_tos.handleLearnMore=i;window.pp_tos.checkCookie=h})(); window.google.sn="webhp";window.google.timers={};window.google.startTick=function(a,b){window.google.timers[a]={t:{start:(new Date).getTime()},bfr:!(!b)}};window.google.tick=function(a,b,c){if(!window.google.timers[a])google.startTick(a);window.google.timers[a].t[b]=c||(new Date).getTime()};google.startTick("load",true);try{}catch(u){} var _gjwl=location;function _gjuc(){var e=_gjwl.href.indexOf("#");if(e>=0){var a=_gjwl.href.substring(e);if(a.indexOf("&q=")>0||a.indexOf("#q=")>=0){a=a.substring(1);if(a.indexOf("#")==-1){for(var c=0;c<a.length;){var d=c;if(a.charAt(d)=="&")++d;var b=a.indexOf("&",d);if(b==-1)b=a.length;var f=a.substring(d,b);if(f.indexOf("fp=")==0){a=a.substring(0,c)+a.substring(b,a.length);b=c}else if(f=="cad=h")return 0;c=b}_gjwl.href="/search?"+a+"&cad=h";return 1}}}return 0}function _gjp(){!(window._gjwl.hash&& window._gjuc())&&setTimeout(_gjp,500)}; window._gjp && _gjp()</script><style>#gbar,#guser{font-size:13px;padding-top:1px !important;}#gbar{height:22px}#guser{padding-bottom:7px !important;text-align:right}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}@media all{.gb1{height:22px;margin-right:.5em;vertical-align:top}#gbar{float:left}}a.gb1,a.gb4{text-decoration:underline !important}a.gb1,a.gb4{color:#00c !important}</style><style id=gstyle>body{margin:0;overflow-y:scroll}#gog{padding:3px 8px 0}td{line-height:.8em}.gac_m td{line-height:17px}form{margin-bottom:20px}body,td,a,p,.h{font-family:arial,sans-serif}.h{color:#36c;font-size:20px}.q{color:#00c}.ts td{padding:0}.ts{border-collapse:collapse}em{font-weight:bold;font-style:normal}.lst{height:25px;width:496px}.gsfi,.lst{font:18px arial,sans-serif}.gsfs{font:17px arial,sans-serif}.tiah{width:458px}.ds{border-bottom:solid 1px #e7e7e7;border-right:solid 1px #e7e7e7;display:-moz-inline-box;display:inline-block;margin:3px 0 4px;margin-left:4px}input{font-family:inherit}a.gb1,a.gb2,a.gb3,a.gb4{color:#11c !important}body{background:#fff;color:black}a{color:#11c;text-decoration:none}a:hover,a:active{text-decoration:underline}.fl a{color:#36c}a:visited{color:#551a8b}a.gb1,a.gb4{text-decoration:underline}a.gb3:hover{text-decoration:none}#ghead a.gb2:hover{color:#fff!important}.sblc{padding-top:5px}.sblc a{display:block;margin:2px 0;margin-left:13px;font-size:11px;}.lsbb{background:#eee;border:solid 1px;border-color:#ccc #999 #999 #ccc;height:30px;display:block}.pp-new-desktop,.pp-new-mobile{color:red}.ftl,#fll a{display:inline-block;margin:0 12px}.lsb{background:url(/images/srpr/nav_logo80.png) 0 -258px repeat-x;border:none;color:#000;cursor:pointer;height:30px;margin:0;outline:0;font:15px arial,sans-serif;vertical-align:top}.lsb:active{background:#ccc}.lst:focus{outline:none}#addlang a{padding:0 3px}.gac_v div{display:none}.gac_v .gac_v2,.gac_bt{display:block!important}table.gssb_c{z-index:986  }.nbcl{background:url(/images/srpr/nav_logo80.png) no-repeat ;height:px;width:px}</style><script>window.rwt=function(a,f,g,l,m,h,c,n,i){try{if(a===window){a=window.event.srcElement;while(a){if(a.href)break;a=a.parentNode}}var b=encodeURIComponent||escape,d;d=a.getAttribute("href");if(c&&c.substring(0,6)!="&sig2=")c="&sig2="+c;var o=["/url?sa=t","","&cd=",b(m),i?"&authuser="+b(i):"",google.j&&google.j.pf?"&sqi=2":"","&ved=",b(n),"&url=",b(d).replace(/\+/g,"%2B"),"&ei=","stNMT4yfMInFtAaK_5yGDw",h?"&usg="+h:"",c].join("");a.href=o;a.onmousedown=""}catch(p){}return true}; </script></head><body bgcolor=#ffffff text=#000000 link=#0000cc vlink=#551a8b alink=#ff0000 onload="document.f&&document.f.q.focus();document.gbqf&&document.gbqf.q.focus();if(document.images)new Image().src='/images/srpr/nav_logo80.png'" ><textarea id=csi style=display:none></textarea><div id=mngb><div id=gbar><nobr><b class=gb1>Iskanje</b> <a class=gb1 href="http://www.google.si/imghp?hl=sl&tab=wi">Slike</a> <a class=gb1 href="http://maps.google.si/maps?hl=sl&tab=wl">Zemljevidi</a> <a class=gb1 href="http://translate.google.si/?hl=sl&tab=wT">Prevajalnik</a> <a class=gb1 href="http://scholar.google.si/schhp?hl=sl&tab=ws">U�enjak</a> <a class=gb1 href="http://www.google.si/blogsearch?hl=sl&tab=wb">Spletni dnevniki</a> <a class=gb1 href="https://mail.google.com/mail/?tab=wm">Gmail</a> </nobr></div><div id=guser width=100%><nobr><span id=gbn class=gbi></span><span id=gbf class=gbf></span><span id=gbe><a  href="/url?sa=p&pref=ig&pval=3&q=http://www.google.si/ig%3Fhl%3Dsl%26source%3Diglk&usg=AFQjCNHCrHelGlcr9HOQNsjP9j1ZtQCSgQ" class=gb4>iGoogle</a> | </span><a href="http://www.google.com/history/optout?hl=sl" class=gb4>Spletna zgodovina</a> | <a  href="/preferences?hl=sl" class=gb4>Nastavitve</a> | <a id=gb_70 href="https://accounts.google.com/ServiceLogin?hl=sl&continue=http://www.google.si/" class=gb4>Prijava</a></nobr></div><div class=gbh style=left:0></div><div class=gbh style=right:0></div></div><center><br clear=all id=lgpd><div id=lga><div style="padding:28px 0 3px"><div align=left style="background:url(/intl/en_com/images/srpr/logo1w.png) no-repeat;height:110px;width:276px" title="Google" id=hplogo onload="window.lol&&lol()"><div nowrap style="color:#777;font-size:16px;font-weight:bold;left:214px;position:relative;top:70px">Slovenija</div></div></div><br></div><form action="/search" name=f><table cellpadding=0 cellspacing=0><tr valign=top><td width=25%>&nbsp;</td><td align=center nowrap><input name=hl type=hidden value=sl><input name=source type=hidden value=hp><input type=hidden name=ie value="ISO-8859-2"><div class=ds style="height:32px;margin:4px 0"><div style="position:relative;zoom:1"><input autocomplete=off maxlength=2048 name=q class="lst tiah" title="Iskanje Google" value="" size=57 style="background:#fff;border:1px solid #ccc;border-bottom-color:#999;border-right-color:#999;color:#000;margin:0;padding:5px 8px 0 6px;padding-right:38px;vertical-align:top"><img src="/textinputassistant/tia.png" width=27 height=23 alt="" style="position:absolute;cursor:pointer;right:5px;top:4px;z-index:300" onclick="var s=document.createElement('script');s.src='/textinputassistant/1/sl_tia.js';google.dom.append(s);"/></div></div><br style="line-height:0"><span class=ds ><span class=lsbb><input name=btnG type=submit value="Iskanje Google" class=lsb></span></span><span class=ds><span class=lsbb><input name=btnI type=submit class=lsb value="Klik na sre�o"></span></span></td><td nowrap width=25% align=left class="fl sblc"><a href="/advanced_search?hl=sl">Napredno iskanje</a><a href="/language_tools?hl=sl">Jezikovna orodja</a></td></tr></table><input type=hidden id=gbv name=gbv value="1"></form><div style="font-size:83%;min-height:3.5em"><br></div><div id=res></div><span id=footer><center id=fctr><div style="font-size:10pt"><div id=fll style="margin:19px auto;text-align:center"><a href="http://adwords.google.com/select?hl=sl">Ogla�evalski programi</a><a href="/intl/sl/about.html">Google &#8211; vse o Googlu</a><a href="http://www.google.com/ncr" class="gl nobr">Google.com in English</a></div></div><p style="color:#767676;font-size:8pt">&copy; 2012</p></center></span> </center><div id=xjsd></div><div id=xjsi><script>if(google.y)google.y.first=[];google.dlj=function(b){window.setTimeout(function(){var a=document.createElement("script");a.src=b;document.getElementById("xjsd").appendChild(a)},0)}; if(google.y)google.y.first=[];if(!google.xjs){google.dstr=[];google.rein=[];if(google.timers&&google.timers.load.t){google.timers.load.t.xjsls=new Date().getTime();}google.dlj('/extern_js/f/CgJzbBICc2kgASswRTgALCswWjgALCswDjgALCswFzgALCswPDgALCswUTgALCswCjgAmgICaGUsKzCYATgALCswFjgALCswGTgALCswQTgALCswTTgALCswTjgALCswVDgALCswaTgALCswkAE4ACwrMJIBOAAsKzDVATgALCsw2AE4ACwrMBg4ACwrMCY4ACyAAlCQAmc/KdS-qUb5nqw.js');google.xjs=1}google.neegg=1;google.mc=[];google.mc=google.mc.concat([[69,{}],[14,{}],[60,{}],[23,{}],[81,{}],[10,{"client":"hp","dh":true,"ds":"","fl":true,"host":"google.si","jsonp":true,"msgs":{"lcky":"Klik na sre�o","lml":"Ve� o tem","psrc":"To iskanje je bilo odstranjeno iz \u003Ca href=\"/history\"\u003Ezgodovine iskanja\u003C/a\u003E","psrl":"Odstrani","srch":"Iskanje Google"},"ovr":{"fm":1,"o":1,"pf":1,"ps":1,"sn":1,"sw":1},"pq":"","scd":10,"sce":5}],[152,{}],[65,{}],[213,{"pberr":"\u003Cfont color=red\u003ENapaka:\u003C/font\u003E Stre�nik ni mogel dokon�ati va�e zahteve. Poskusite znova �ez 30 sekund."}],[78,{}],[25,{"g":8,"k":false,"m":{"app":true,"bks":true,"blg":true,"dsc":true,"evn":true,"fin":true,"flm":true,"frm":true,"isch":true,"klg":true,"mbl":true,"nws":true,"plcs":true,"ppl":true,"prc":true,"pts":true,"rcp":true,"shop":true,"vid":true},"t":null}],[216,{}],[105,{}],[22,{"db":false,"m_errors":{"32":"�al ni drugih rezultatov.","default":"\u003Cfont color=red\u003ENapaka:\u003C/font\u003E Stre�nik ni mogel dokon�ati va�e zahteve. Poskusite znova �ez 30 sekund."},"m_tip":"Kliknite za ve� informacij","nlpm":"-153px -84px","nlpp":"-153px -70px","utp":false}],[77,{}],[146,{}],[144,{}],[84,{}],[24,{}],[38,{}]]);google.y.first.push(function(){if(google.med){google.med('init');google.initHistory();google.med('history');}google.History&&google.History.initialize('/')});if(google.j&&google.j.en&&google.j.xi){window.setTimeout(google.j.xi,0);}</script></div><script>(function(){ var b,d,e,f;function g(a,c){if(a.removeEventListener){a.removeEventListener("load",c,false);a.removeEventListener("error",c,false)}else{a.detachEvent("onload",c);a.detachEvent("onerror",c)}}function h(a){f=(new Date).getTime();++d;a=a||window.event;var c=a.target||a.srcElement;g(c,h)}var i=document.getElementsByTagName("img");b=i.length;d=0;for(var j=0,k;j<b;++j){k=i[j];if(k.complete||typeof k.src!="string"||!k.src)++d;else if(k.addEventListener){k.addEventListener("load",h,false);k.addEventListener("error", h,false)}else{k.attachEvent("onload",h);k.attachEvent("onerror",h)}}e=b-d;function l(){if(!google.timers.load.t)return;google.timers.load.t.ol=(new Date).getTime();google.timers.load.t.iml=f;google.kCSI.imc=d;google.kCSI.imn=b;google.kCSI.imp=e;if(google.stt!==undefined)google.kCSI.stt=google.stt;google.timers.load.t.xjs&&google.report&&google.report(google.timers.load,google.kCSI)}if(window.addEventListener)window.addEventListener("load", l,false);else if(window.attachEvent)window.attachEvent("onload",l);google.timers.load.t.prt=(f=(new Date).getTime()); })(); </script> 

I am using eclipse.

like image 641
tjb Avatar asked Feb 28 '12 13:02

tjb


People also ask

Does Apache HttpClient use Log4j?

Note: Log4j is not included in the HttpClient distribution.

What is a wire logger?

Wire Logging The wire log is used to log all data transmitted to and from servers when executing HTTP requests. This log should only be enabled to debug problems, as it will produce an extremely large amount of log data, some of it in binary format.

Does HttpClient need to be closed?

You do not need to explicitly close the HttpClient, however, (you may be doing this already but worth noting) you should ensure that connections are released after method execution. Edit: The ClientConnectionManager within the HttpClient is going to be responsible for maintaining the state of connections.

How does httpclient handle logging?

The HttpClient library provides efficient, up-to-date, and feature-rich implementation client site of the HTTP protocol. Indeed as a library, HttpClient doesn't force logging implementation. With this intention, version 4.5, provides logs with Commons Logging. Similarly, the latest version, 5.1, uses a logging facade provided by SLF4J.

How do I enable HTTP logging?

HTTP Logging is enabled with UseHttpLogging, which adds HTTP logging middleware. By default, HTTP Logging logs common properties such as path, status-code, and headers for requests and responses. Add the following line to the appsettings.Development.json file at the "LogLevel": { level so the HTTP logs are displayed:

What is HTTP logging in ASP NET Core?

ASP. NET Core HTTP Logging is a middleware that logs information about HTTP requests and HTTP responses. HTTP logging provides logs of: Record information about incoming requests and responses. Filter which parts of the request and response are logged. Filtering which headers to log.

How can I reduce the performance of an HTTP logging request?

Filtering which headers to log. HTTP Logging can reduce the performance of an app, especially when logging the request and response bodies. Consider the performance impact when selecting fields to log. Test the performance impact of the selected logging properties. HTTP Logging can potentially log personally identifiable information (PII).


2 Answers

I faced same problem today. And I found out a solution for this problem. It's pretty simple. Just add jcl-over-slf4j dependency to forward JCL logging (Apache Commons Logging) to slf4j.

<dependency>     <groupId>org.slf4j</groupId>     <artifactId>jcl-over-slf4j</artifactId>     <version>1.7.5</version> </dependency> 

After that you can config log file as Log4j Examples. If you use logback you can put the following to your logback.xml

<logger name="org.apache.http" level="DEBUG"/> 

Or if you use log4j

log4j.logger.org.apache.http=DEBUG 
like image 146
thangdc94 Avatar answered Sep 25 '22 15:09

thangdc94


This is due to the HttpClient version mix-up. Your code is obviously using HttpClient 4.x API, while the logging settings are for an older Commons HttpClient release (3.x).

Please refer to the HttpClient 4.x logging guide for instructions on logging configuration for various logging toolkits.

like image 39
ok2c Avatar answered Sep 23 '22 15:09

ok2c