Skip to content

A Kotlin Multiplatform library for logging, inspecting, and persisting HTTP network traffic in Ktor client

License

Notifications You must be signed in to change notification settings

MambaWoW/KtorSniffer

Repository files navigation

KtorSniffer

Maven Central

KtorSniffer is a Kotlin Multiplatform library for logging, inspecting, and persisting HTTP network traffic in Ktor client applications. It is designed to help developers debug, analyze, and visualize network requests and responses across Android, iOS.

Features

  • Automatic Logging: Intercepts and logs all HTTP requests and responses made via Ktor client.
  • Persistence: Stores network logs in a local database (Room).
  • UI Integration: Comes with a ready-to-use Compose Multiplatform UI for browsing and inspecting logs.
  • ProtoBuf Support: Handles ProtoBuf request/response bodies in addition to JSON and plain text.
  • Customizable: Easily configure how to format ProtoBuf bodies.

Screenshots

Log List JSON General View JSON Response Details ProtoBuf Request ProtoBuf Response

Getting Started

1. Add KtorSniffer to Your Project

Note: Instructions below assume you have a Kotlin Multiplatform project with Ktor client and Compose Multiplatform set up.

Add the dependency to your shared module:

implementation("io.github.frankieshao:ktorsniffer:0.1.1")

2. Install the Plugin in Your Ktor Client

val client = HttpClient {
    install(Sniffer)
    // ... your other plugins
}

3. (Android) Initialize the Database

In your Application or main Activity:

class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        KtorSniffer.init(this)
    }
}

Note: You must call KtorSniffer.init(context) before using the log UI or DAO on Android.

4. Use the Compose UI

You can embed the provided Compose UI to browse and inspect logs:

@Composable
fun MyApp() {
    KtorSnifferScreen(onBack = { /* handle back navigation */ })
}

How It Works

  • Interception: The Sniffer plugin hooks into Ktor's client pipeline, capturing request and response data at multiple stages.
  • Persistence: Logs are saved to a local database using a multiplatform DAO abstraction (Room on Android).
  • UI: The Compose UI provides a searchable, filterable list of network logs, with detail views for headers, bodies, and errors.

Configuration

You can customize logging behavior via the SnifferConfig DSL:

install(Sniffer) {
    protoBuf { /* configure ProtoBuf formatting */ }
    coroutineScope { /* provide custom CoroutineScope if needed */ }
}

Supported Platforms

  • Android (Room database, Compose UI)
  • iOS (Room database, Compose UI)

Example

See the composeApp module for a full sample app demonstrating GET/POST/ProtoBuf requests and the log UI.

Roadmap

  • Export logs to file
  • Integration with other logging/analytics tools

Contributing

Contributions, issues, and feature requests are welcome!
Feel free to open an issue or submit a pull request.

License

MIT License


Enjoy debugging with KtorSniffer!
If you like this project, please star it on GitHub and share your feedback.

About

A Kotlin Multiplatform library for logging, inspecting, and persisting HTTP network traffic in Ktor client

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •