Skip to content

Powerful tool to monitor Ktor Client requests and responses, making it easier to debug and analyze network communication.

License

Notifications You must be signed in to change notification settings

CosminMihuMDC/KtorMonitor

Repository files navigation

Maven Central License PRs Welcome Platforms GitHub stars GitHub forks

KtorMonitor

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 for release builds
  • ios -> is enabled for debug builds and disabled for release builds
  • desktop -> is enabled for all builds

Setup

kotlin {
    sourceSets {
        commonMain.dependencies {
            implementation("ro.cosminmihu.ktor:ktor-monitor-logging:1.5.0")
        }
    }
}

Install Ktor Client Plugin

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 logs
  • filter - 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 use ContentLength.Full.

🧩 Integration

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 full KtorMonitor.
  • 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 with KtorMonitorMenuItem
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()
    }
}

✍️ Feedback

Feel free to send feedback or file an issue.

🙌 Acknowledgments

Kotlin Compose Multiplatform Coil Coil Coil

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!

💸 Sponsors

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.

🙏🏻 Credits

KtorMonitor is brought to you by these contributors.