0

I am trying to get apache httpclient logs on the console for HTTPBuilder's RESTClient in groovy. I tried various approaches, including the standard instructions and some other suggestions such as this and this, but to no avail.

Finally after much frustration, I got some help from this SO question, and though it's not a clean solution, it gets the job done - I do get the wire logs now.

Here's some sample code:

// test.groovy
import groovyx.net.http.RESTClient

import java.util.logging.ConsoleHandler
import java.util.logging.Level
import java.util.logging.Logger

// Remove default loggers
def logger=Logger.getLogger('')
def handlers=logger.handlers
handlers.each() { handler->logger.removeHandler(handler) }

// Log ALL to Console
logger.setLevel Level.FINE
def consoleHandler=new ConsoleHandler()
consoleHandler.setLevel Level.FINE
logger.addHandler(consoleHandler)

def myclient = new RESTClient( 'https://www.google.com/search' )

def resp = myclient.get( queryString: 'q=httpclient' )

The only problem now is that I want to use logback instead of java.util.Logging for this, because the rest of my application is already using logback.

I am now trying to get the equivalent logback.groovy configuration for the above, but it doesn't work:

// logback.groovy
appender("CONSOLE", ConsoleAppender)
{
    encoder(PatternLayoutEncoder)
    {
      pattern = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"
    }
}


root DEBUG, ["CONSOLE"]

logger "groovyx.net.http.HttpURLClient", DEBUG, ["CONSOLE"]
logger "org.apache.http", DEBUG, ["CONSOLE"]
logger "org.apache.http.headers", DEBUG, ["CONSOLE"]
logger "org.apache.http.wire", DEBUG, ["CONSOLE"]

I am not getting any apache headers or apache wire logs. I am very new to logback (and log4j and slf4j and java.util.Logging), so your help would be greatly appreciated.

try-catch-finally
  • 7,436
  • 6
  • 46
  • 67
nonbeing
  • 6,907
  • 6
  • 36
  • 46
  • I am afraid, I don't understand the question. – Ceki May 11 '12 at 14:50
  • I want to be able to perform this java.util.logging functionality (actual code given above) in logback: // Remove default loggers ... // Log ALL to Console – nonbeing May 13 '12 at 17:29
  • Note: [this answer to a question](https://stackoverflow.com/a/20407321/1078886) on how to send JUL (java.util.logging) to Logback is related (but since the underlying issue in this case is sending _commons logging_ to Logback it's not a duplicate). – try-catch-finally Jun 25 '17 at 12:56

1 Answers1

2

Being a library HttpClient is not to dictate which logging framework the user has to use. Therefore HttpClient utilizes the logging interface provided by the Commons Logging package

You should use JCL to SLF4j bridge to handle applications' JCL activity via SLF4j handler. See http://www.slf4j.org/legacy.html#jcl-over-slf4j

Konstantin Pavlov
  • 956
  • 1
  • 10
  • 24
  • This worked for me. Just add a dependency to 'org.slf4j:jcl-over-slf4j:1.7.13' or whatever the latest/compatible version is for you. – Mikezx6r Feb 20 '16 at 03:45