A Comprehensive Guide to Pretty Logging using Ktor in Android
When working with API requests in Android, logging formatted, readable JSON responses makes debugging much easier. In this blog, we’ll…
A Comprehensive Guide to Pretty Logging using Ktor in Android
Photo by Fabian Møller on Unsplash
When working with API requests in Android, logging formatted, readable JSON responses makes debugging much easier. In this blog, we’ll explore how you can pretty-print API requests and responses using Ktor.
Steps To Follow
1. Add Dependencies
Ensure your build.gradle.kts has the necessary dependencies:
dependencies {
implementation("io.ktor:ktor-client-core:2.3.5")
implementation("io.ktor:ktor-client-okhttp:2.3.5") // OkHttp for better logging
implementation("io.ktor:ktor-client-logging:2.3.5") // Ktor logging
implementation("com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.11") // OkHttp logging
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0") // JSON serialization
}
2. Enable Pretty Logging in Ktor
Custom Logger for Pretty JSON
Ktor’s Logging plugin does not format JSON by default. We can intercept logs and format JSON manually:
import android.util.Log
import io.ktor.client.*
import io.ktor.client.engine.okhttp.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.request.*
import org.json.JSONArray
import org.json.JSONObject
// Pretty Print JSON Function
fun prettyJson(json: String): String {
return try {
when {
json.startsWith("{") -> JSONObject(json).toString(4) // Format JSON Object
json.startsWith("[") -> JSONArray(json).toString(4) // Format JSON Array
else -> json
}
} catch (e: Exception) {
json // Return as is if formatting fails
}
}
// Custom Logger
object PrettyLogger : Logger {
override fun log(message: String) {
if (message.startsWith("{") || message.startsWith("[")) {
Log.d("KtorLogger", prettyJson(message)) // Pretty JSON
} else {
Log.d("KtorLogger", message) // Regular log
}
}
}
// Create Ktor Client with Pretty Logging
val client = HttpClient(OkHttp) {
install(Logging) {
logger = PrettyLogger
level = LogLevel.ALL // Log everything
}
}
// Make a Request
suspend fun makeApiRequest() {
val response = client.get("https://jsonplaceholder.typicode.com/posts/1").body<String>()
Log.d("API_RESPONSE", prettyJson(response)) // Log formatted JSON
}
How This Works
✔ Logs URL, Headers, Body, and Responses
✔ Automatically formats JSON for readability
✔ Uses Logcat (Log.d) for easy debugging in Android Studio
Example Output in Logcat
When making an API request, the Logcat output looks like this:
D/KtorLogger: URL: https://jsonplaceholder.typicode.com/posts/1
D/KtorLogger: METHOD: GET
D/KtorLogger: HEADERS: Accept: application/json
D/KtorLogger: BODY:
{
"userId": 1,
"id": 1,
"title": "Sample Post",
"body": "This is a sample response body."
}
This is much easier to read than a single-line, unformatted JSON string.
Key Takeaways
✅ Use Ktor’s
Loggingplugin with a customPrettyLoggerfor neat logs ✅ OkHttp’sLoggingInterceptoris powerful for native OkHttp clients ✅ Always log inDEBUGmode and disable logging in production!
메타데이터
- post_id
- aef6157119a3
- slug
- a-comprehensive-guide-to-pretty-logging-using-ktor-in-android-aef6157119a3
- url
- https://medium.com/@vinayakbhagwani/a-comprehensive-guide-to-pretty-logging-using-ktor-in-android-aef6157119a3
- canonical_url
- https://medium.com/@vinayakbhagwani/a-comprehensive-guide-to-pretty-logging-using-ktor-in-android-aef6157119a3
- author_url
- https://medium.com/@vinayakbhagwani
- status
- ok
- fetched_at
- 2026-08-17 06:51:02