Powerful tool to monitor Ktor Client requests and responses, making it easier to debug and analyze network communication.
By default, KtorMonitor
:
- android -> is enabled for
debug
builds and disabled forrelease
builds - ios -> is enabled for
debug
builds and disabled forrelease
builds - desktop -> is enabled for all builds
kotlin {
sourceSets {
commonMain.dependencies {
implementation("ro.cosminmihu.ktor:ktor-monitor-logging:1.5.0")
}
}
}
HttpClient {
install(KtorMonitorLogging) {
sanitizeHeader { header -> header == "Authorization" }
filter { request -> !request.url.host.contains("cosminmihu.ro") }
showNotification = true
retentionPeriod = RetentionPeriod.OneHour
maxContentLength = ContentLength.Default
}
}
sanitizeHeader
- sanitize sensitive headers to avoid their values appearing in the logsfilter
- filter logs for calls matching a predicate.showNotification
- Keep track of latest requests and responses into notification. Default is true. Android and iOS only. Notifications permission needs to be granted.retentionPeriod
- The retention period for the logs. Default is 1h.maxContentLength
- The maximum length of the content that will be logged. After this, body will be truncated. Default is 250_000. To log the entire body useContentLength.Full
.
Check out below how to interate Ktor Monitor for different platforms.
Compose Multiplatform (all platforms)
- Use
KtorMonitor
Composable
@Composable
fun Composable() {
KtorMonitor()
}
Android
- If
showNotifcation = true
and android.permission.POST_NOTIFICATIONS is granted, the library will display a notification showing a summary of ongoing KTOR activity. Tapping on the notification launches the fullKtorMonitor
. - Apps can optionally use the
KtorMonitor()
Composable directly into own Composable code.
Desktop Compose
- Use
KtorMonitorWindow
Composable
fun main() = application {
var showKtorMonitor by rememberSaveable { mutableStateOf(false) }
KtorMonitorWindow(
onCloseRequest = { showKtorMonitor = false },
show = showKtorMonitor
)
}
- Use
KtorMonitorWindow
Composable withKtorMonitorMenuItem
fun main() = application {
var showKtorMonitor by rememberSaveable { mutableStateOf(false) }
Tray(
icon = painterResource(Res.drawable.ic_launcher),
menu = {
KtorMonitorMenuItem { showKtorMonitor = true }
}
)
KtorMonitorWindow(
show = showKtorMonitor,
onCloseRequest = { showKtorMonitor = false }
)
}
Desktop Swing
- Use
KtorMonitorPanel
Swing Panel
fun main() = application {
SwingUtilities.invokeLater {
val frame = JFrame()
frame.add(KtorMonitorPanel, BorderLayout.CENTER)
frame.isVisible = true
}
}
iOS
-
If
showNotifcation = true
and notification permission is granted, the library will display a notification showing a summary of ongoing KTOR activity. -
Use
KtorMonitorViewController
fun MainViewController() = KtorMonitorViewController()
struct KtorMonitorView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIViewController {
MainViewControllerKt.MainViewController()
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}
struct ContentView: View {
var body: some View {
KtorMonitorView()
.ignoresSafeArea()
}
}
Feel free to send feedback or file an issue.
Some parts of this project are reusing ideas that are originally coming from chucker.
Thanks to ChuckerTeam for chucker!
Thanks to JetBrains for Ktor and Kotlin!
KtorMonitor is maintained and improved during nights, weekends and whenever team has free time. If you use KtorMonitor in your project, please consider sponsoring us.
You can sponsor us by clicking ♥ Sponsor Button.
KtorMonitor is brought to you by these contributors.