square / retrofit

A type-safe HTTP client for Android and the JVM
https://square.github.io/retrofit/
Apache License 2.0
43.14k stars 7.31k forks source link

Request and response logging of the unserialized Java types #378

Open lsuski opened 10 years ago

lsuski commented 10 years ago

Hi, I'd like to log only request and response body along with http status code and execution time (no headers, urls and methods names). Is there possibility to make log level mechanism more flexible? For example use some interface or simple class with methods like isLogStatus(), isLogBody() etc. and ovveride it for own needs. Thanks

thimes commented 10 years ago

Log is already an interface. You can create a class of your own that prints as little or as much as you want, and then set it on the RestAdapter, like so:

   RestAdapter restAdapter = new RestAdapter.Builder().setLog(new MyLogImpl()).build();

What you print is up to you. I'd also suggest looking into the Emitter class, for help with printing.

lsuski commented 10 years ago

Log is an interface but LogLevel is not. I would need to check message content passed to Log.log() method to reject e.g. headers, which is not very handy and require a knowledge of RestAdapter message format. Thanks.

BTW. Where is the class Emitter. I couldn't find it in Retrofit.

thimes commented 10 years ago

Huh, I apologize. I thought that was part of retrofit, looking closer, it's just an interface I made to abstract away that part of output.

I could envision a LogLevel builder that contains methods for setting the logging of headers, body, url, and status, among others. What else would you want to see in the builder?

lsuski commented 10 years ago

Things that you mentioned + exceptions are sufficient for me. Thanks a lot for interest.

JakeWharton commented 10 years ago

Writing this down so it's not in my head:

Loggers will get callbacks for request and response much like they do internally. Log level will become part of the default logging impl. You can replace the logger with one of your own.

Turbo87 commented 10 years ago

@JakeWharton is that planned for 2.0 or also for the 1.x branch?

JakeWharton commented 10 years ago

This is all 2.0 stuff. I'm trying to wrap up a 1.6 this week so I can start the work on 2.0 ASAP.

JakeWharton commented 10 years ago

Another thing: how do we handle binary vs text data by default?

JakeWharton commented 10 years ago

We need to be sure to hand the Class<?> reference to the logger for which the events are being generated so that loggers can present the logs as if the interfaces themselves were generating them.

jacobtabak commented 10 years ago

Came here looking for a solution to logging binary data - for now, I will use a different rest adapter. Do we need to consider the case where a TypedFile contains ASCII data and should potentially be logged?

hosamaly commented 9 years ago

It looks like 2.0.0-beta1 removed logging support altogether in commit 40bfa340bb back in January. Any idea when it might return?

JakeWharton commented 9 years ago

Nope. Maybe never. Use OkHttp interceptors in the mean time.

On Mon, Aug 31, 2015 at 12:37 PM Hosam Aly notifications@github.com wrote:

It looks like 2.0.0-beta1 removed logging support altogether in commit 40bfa34 https://github.com/square/retrofit/commit/40bfa340bbff6b18c691f74b40e09f27b98ab920 back in January. Any idea when it might return?

— Reply to this email directly or view it on GitHub https://github.com/square/retrofit/issues/378#issuecomment-136423624.

hosamaly commented 9 years ago

Thanks. FWIW, here is an example for a logging interceptor for OkHttp: https://github.com/square/okhttp/wiki/Interceptors

vitaliyistomov commented 9 years ago

Any chances to have default supported by Square logging interceptor implementation for OkHttp with logging of the bodies of the request and response like it was in Retrofit 1.x? I believe every second person would be interested in having that instead of tweaking the snippet from https://github.com/square/okhttp/wiki/Interceptors to log full request/response for debugging every time.

cemo commented 9 years ago

Is this already resolved by https://github.com/square/okhttp/pull/1874?

OkHttpClient client = new OkHttpClient();
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(Level.BASIC);
client.interceptors().add(logging);
JakeWharton commented 9 years ago

No. That's logging at the HTTP layer which I mostly don't care about. Retrofit needs logging at the application layer using your types, not HTTP, for the representation.

On Sun, Sep 27, 2015, 3:33 PM Cemalettin Koc notifications@github.com wrote:

Is this already solved by square/okhttp#1874 https://github.com/square/okhttp/pull/1874?

OkHttpClient client = new OkHttpClient(); HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(Level.BASIC); client.interceptors().add(logging);

— Reply to this email directly or view it on GitHub https://github.com/square/retrofit/issues/378#issuecomment-143589285.