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.

4

1 回答 1

2

作为一个库 HttpClient 并不是要规定用户必须使用哪个日志框架。因此 HttpClient 使用了 Commons Logging 包提供的日志接口

您应该使用 JCL 到 SLF4j 桥来通过 SLF4j 处理程序处理应用程序的 JCL 活动。见http://www.slf4j.org/legacy.html#jcl-over-slf4j

于 2014-02-20T07:24:23.567 回答