Skip to content

Instantly share code, notes, and snippets.

@rezaiyan
Created April 26, 2019 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rezaiyan/82e0768f25fbeafa53265d0ad295bfbe to your computer and use it in GitHub Desktop.
Save rezaiyan/82e0768f25fbeafa53265d0ad295bfbe to your computer and use it in GitHub Desktop.
Pretty Json Logger
//Reference//
//https://github.com/orhanobut/logger
//https://github.com/JakeWharton/timber
// 1- Add this initializations to the oncreate method of Application class
Logger.addLogAdapter(AndroidLogAdapter())
Timber.plant(Timber.DebugTree())
// 2- Create a snippet to detect log is a json format (I used a string extention function)
fun String.isJson(): Boolean {
try {
JSONObject(this)
} catch (ex: JSONException) {
try {
JSONArray(this)
} catch (ex1: JSONException) {
return false
}
}
return true
}
// 3- Add this interceptor to clientBuilder
val httpLoggingInterceptor = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { message ->
if (message.isJson())
Logger.json(message)
else Timber.d(message)
})
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment