No more Retrofit, move to Ktor on Android — Logging and Interceptor
In the previous article here, we see how to implement Ktor on Android, now we focus on Logging and Interceptor.
Logging
First, add the logging dependencies in build.gradle
:
implementation "io.ktor:ktor-client-logging:$ktor_version"
Next, we have to install Logging
on HttpClient
:
install(Logging) {
logger = Logger.DEFAULT
level = LogLevel.ALL
}
Logger.DEFAULT
uses an SLF4J logging framework. For Native targets, set this property toLogger.SIMPLE
.- The
level
property specifies the logging level.
Extra
To see log displayed on Logcat
you should use Logger.SIMPLE
, but it is possible to create a CustomHttpLogger
to customize logger messages. Here the CustomHttpLogger
class:
class CustomHttpLogger : Logger {
override fun log(message: String) {
Log.d("loggerTag", message)
}
}
We have to install it on HttpClient
:
install(Logging) {
logger = CustomHttpLogger()
level = LogLevel.ALL
}