Junior — Middle
How can request logging be implemented in the system?
sobes.tech AI
Answer from AI
There are several approaches to logging requests in an Android application:
- Using Interceptor in OkHttp — if you use OkHttp for network requests, you can add an Interceptor that intercepts and logs all requests and responses.
val loggingInterceptor = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}
val client = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build()
-
Manual logging — before sending a request and after receiving a response, output the necessary information (URL, headers, body) to Logcat.
-
Using third-party libraries — for example, Timber for convenient logging.
-
Saving logs to a file — if you need to save logs for later analysis, you can write them to a file on the device.
It is important to note that logging request and response bodies may contain sensitive data, so in release builds, logging level should be limited.