Turning on Logging in HttpClient
Asked Answered
O

10

33

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.

Outcast answered 28/2, 2012 at 13:23 Comment(1)
stuck at the same problem. :'( Guess there is something wrong with httpClient 4.x ...Burdett
H
18

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
Hagans answered 29/3, 2019 at 10:16 Comment(0)
N
12

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.

Noria answered 29/2, 2012 at 15:51 Comment(1)
That would be fine if those instructions didn't completely suck. Does anyone know of a useful configuration guide with examples?Brote
D
10

http://hc.apache.org/httpcomponents-client-ga/logging.html is wrong in section Log4j Examples.

To get full wire logging on HttpClient 4.x with Log4j use:

log4j.category.org.apache.http.wire=DEBUG
Dairying answered 21/4, 2016 at 18:25 Comment(0)
N
5

In a Spring Boot 2.2.x app with httpclient 4.5.x this config in he application.yml helped me:

logging:
  level:
    org.apache.http: DEBUG
Nationalize answered 15/7, 2020 at 7:29 Comment(0)
T
2

Have you also tried including the first line from the examples:

System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");

This should register the SimpleLog implementation with commons-logging.

Tara answered 28/2, 2012 at 15:57 Comment(1)
yes, still nothing (I modified the question above to reflect the attempt)Outcast
G
2

I had to add "commons-logging-1.2.jar" to the build path.

Girvin answered 4/7, 2016 at 9:55 Comment(0)
B
2

After hours of searching without a dependency manager like maven this is how it finally worked for me to install Apache components HttpClient 5.0 and the other jars required for logging

1- Downloading Apache components HttpClient 5.0

Go to https://hc.apache.org/downloads.cgi and download the HttpClient 5.0 library binary folder including all it's jars

2- Downloading Log4j 2 implementation a.k.a "core"

according to this note

The Log4j 2 implementation a.k.a "core" is not included in the HttpClient distribution. You can include it in your project using Maven, Ivy, Gradle, or SBT

I checked the maven xml file and downloaded these 2 files

3- Downloading SLF4J Bridge

After step 2 you will still find errors and obviously there is something still missed, I kept on reading this page and found another note that says

If existing components use SLF4J and you want to have this logging routed to Log4j 2, then add the following but do not remove any SLF4J dependencies.

I downloaded this jar file log4j-slf4j-impl-2.13.1.jar

4- No more downloading

Now you need to add the HttpClient 5.0 jars folder and the other 3 jars we just downloaded to your classpath (from configure Build path if you are using Eclipse) and test if logging works using one of the examples here . Create a file named log4j2.xml at the root of your application classpath with this contents

<Configuration>
  <Appenders>
    <Console name="STDOUT">
      <PatternLayout pattern="%d %-5level [%logger] %msg%n%xThrowable" />
    </Console>
  </Appenders>
  <Loggers>
    <Logger name="org.apache.hc.client5.http" level="DEBUG">
      <AppenderRef ref="Console"/>
    </Logger>
    <Logger name="org.apache.hc.client5.http.wire" level="DEBUG">
      <AppenderRef ref="Console"/>
    </Logger>
    <Root level="INFO">
      <AppenderRef ref="STDOUT" />
    </Root>
  </Loggers>
</Configuration>

Voila , you should now see many logs like this one in your console, when HttpClient is sending a http request

2020-04-01 19:49:08,597 DEBUG [org.apache.hc.client5.http.impl.classic.ConnectExec] ex-00000001: opening connection {}->http://www.example.com:80

5- Final thoughts

  • Keep your eye on the versions of files, if you are reading this post in the future make sure that the versions of the jar files are compatible

  • Make sure your class path is clear and you are not using or installing any other jars that could break the applications, for example I installed the entire Log4j2 library folder, and it fixed some errors, but produced new errors.

Brosy answered 1/4, 2020 at 18:50 Comment(1)
Thank you for the detailed explanation. It is really works.Schlieren
T
0

This is how I enabled the logging in Apache HttpClient 5 using Log4j2 in Eclipse. I added these libraries:

enter image description here

  • log4j-api-2.12.4.jar
  • log4j-core-2.12.4.jar
  • log4j-slf4j-impl-2.12.4.jar

Then, I created the configuration file log4j2.xml.

<Configuration>
  <Appenders>
    <Console name="STDOUT">
      <PatternLayout pattern="%d %-5level [%logger] %msg%n%xThrowable" />
    </Console>
  </Appenders>
  <Loggers>
    <Logger name="org.apache.hc.client5.http" level="DEBUG">
      <AppenderRef ref="STDOUT"/>
    </Logger>
    <Logger name="org.apache.hc.client5.http.wire" level="DEBUG">
      <AppenderRef ref="STDOUT"/>
    </Logger>
    <Logger name="org.apache.hc.client5.http.header" level="DEBUG">
      <AppenderRef ref="STDOUT"/>
    </Logger>
    <Root level="INFO">
      <AppenderRef ref="STDOUT" />
    </Root>
  </Loggers>
</Configuration>

Put it in a new forlder /res and add the folder as Class Folder (Properties -> Java Build Path -> Add Class Folder)

Done.

Theodore answered 10/1, 2022 at 22:9 Comment(0)
C
0

If you are using a Spring Boot starter like spring-boot-starter-web or spring-boot-starter-webflux, etc., it depends transitively on the logging starter which uses Logback by default.

Create a basic logback.xml file at src/main/resources/logback.xml and add an entry for org.apache.http:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

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

</configuration>
Contraceptive answered 9/9, 2022 at 0:33 Comment(0)
B
0

You could do the below:

Edit logback.xml to include the below:

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

If using log4j.properties, then edit and add the below:

log4j.logger.httpclient.wire.header=DEBUG
log4j.logger.httpclient.wire.content=DEBUG

In case, if you want to get rid of the logging, just use WARN instead of DEBUG

Banna answered 8/11, 2023 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.