Created
April 26, 2019 16:16
-
-
Save rezaiyan/82e0768f25fbeafa53265d0ad295bfbe to your computer and use it in GitHub Desktop.
Pretty Json Logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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