retrofit icon indicating copy to clipboard operation
retrofit copied to clipboard

Request and response logging of the unserialized Java types

Open lsuski opened this issue 11 years ago • 17 comments

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

lsuski avatar Jan 14 '14 14:01 lsuski

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.

thimes avatar Jan 14 '14 14:01 thimes

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.

lsuski avatar Jan 14 '14 15:01 lsuski

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?

thimes avatar Jan 14 '14 15:01 thimes

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

lsuski avatar Jan 14 '14 16:01 lsuski

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.

JakeWharton avatar Jun 02 '14 16:06 JakeWharton

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

Turbo87 avatar Jun 02 '14 16:06 Turbo87

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 avatar Jun 02 '14 16:06 JakeWharton

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

JakeWharton avatar Jul 08 '14 15:07 JakeWharton

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.

JakeWharton avatar Jul 19 '14 08:07 JakeWharton

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?

jacobtabak avatar Nov 03 '14 20:11 jacobtabak

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

hosamaly avatar Aug 31 '15 16:08 hosamaly

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

On Mon, Aug 31, 2015 at 12:37 PM Hosam Aly [email protected] 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.

JakeWharton avatar Aug 31 '15 16:08 JakeWharton

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

hosamaly avatar Aug 31 '15 16:08 hosamaly

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.

vitaliyistomov avatar Sep 16 '15 13:09 vitaliyistomov

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);

cemo avatar Sep 27 '15 19:09 cemo

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 [email protected] 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.

JakeWharton avatar Sep 27 '15 19:09 JakeWharton

Might be time for an event listener

https://publicobject.com/2022/05/01/eventlisteners-are-good/

JakeWharton avatar Dec 04 '24 15:12 JakeWharton