How to log request and response body with Retrofit-Android?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Retrofit itself delegates HTTP transport to OkHttp, so request and response body logging is normally added at the OkHttp layer. The standard tool is HttpLoggingInterceptor, which can log nothing, basic metadata, headers, or full bodies depending on the level you choose.
Add the Logging Interceptor
The typical setup is:
Level.BODY logs request lines, response lines, headers, and bodies. That is the most verbose setting and the one people usually want during debugging.
What the Levels Mean
The built-in levels are:
- '
NONE' - '
BASIC' - '
HEADERS' - '
BODY'
BODY is convenient, but it is also the riskiest because payloads can contain sensitive data or be very large.
Use Body Logging Only Where It Makes Sense
For local debugging, BODY is often fine. For release builds, it is usually better to reduce logging or disable it entirely:
That keeps detailed logs out of production by default.
Redact Sensitive Headers
If the requests contain tokens or credentials, redact them:
This is a strong default even in development, because logs have a habit of being copied into bug reports and shared chat threads.
When You Need More Than the Built-In Interceptor
Sometimes you want custom formatting, structured logging, or selective logging only for certain endpoints. In that case, write your own OkHttp interceptor:
This does not automatically dump request and response bodies the way HttpLoggingInterceptor does, but it gives full control over what gets logged and where it goes.
Be Careful with Body Logging in Production
Full-body logging can expose:
- passwords
- auth tokens
- personal data
- large binary payloads
It can also slow down requests slightly and clutter logs so much that important signals become harder to find. For those reasons, BODY logging is usually a debug-only tool.
Common Pitfalls
The biggest pitfall is trying to configure logging in Retrofit itself instead of in the underlying OkHttp client. Retrofit does not own the wire-level logging mechanism.
Another common mistake is leaving BODY logging enabled in release builds. That can leak sensitive data and create unnecessary overhead.
People also forget that not every response body is text. Logging large binary or compressed content may be noisy, slow, or simply not useful.
If you are debugging only one service call, a temporary custom interceptor scoped to that client is often cleaner than enabling full-body logging for the entire app.
Summary
- Use OkHttp's
HttpLoggingInterceptorto log Retrofit requests and responses. - Set the level to
BODYwhen you need full request and response payloads during debugging. - Prefer lower logging levels or
NONEoutside debug builds. - Redact sensitive headers such as
AuthorizationandCookie. - Use a custom interceptor when you need more control than the built-in logger provides.
- Be selective with body logging on large or sensitive APIs so the logs stay useful.
Related reading
- How to log source file name and line number in Python
- how to log Spring 5 WebClient call
- How to log the active configuration in a Spring Boot application?
- How to log Trace messages with log4net?
- How to maintain bi-directional relationships with Spring Data REST and JPA?
- How to make a post request with the Python requests library?
- How to lose margin/padding in UITextView
- How to make a background 20 transparent on Android

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.