DEV Community

Shiva Thapa
Shiva Thapa

Posted on Originally published at Medium on

Your Stack Trace Says Where It Died. It Won’t Say Why.

Building one logging, crash, and performance pipeline for a Kotlin Multiplatform app — one that scrubs its own secrets, is forbidden from crashing the app, and behaves the same on Android and iOS.

Here is a crash report I stared at for twenty minutes and learned nothing from.

A NullPointerException, six frames deep, all of them inside a coroutine dispatcher. No app class in the stack. No indication of which screen the user was on, whether they were signed in, what language the UI was in, or which of our two backend tiers the build was pointed at. The stack told me, with total precision, the one thing I could already guess: a value was null when the code expected it not to be.

It did not tell me why. And “why,” on a phone you will never hold, in the hands of a farmer on a 3G connection in a district you have never visited, is the entire job.

A stack trace is an autopsy. Observability is the medical history. The autopsy tells you the organ that failed. The history tells you the patient skipped a dose, that they were on their third screen in a flow, offline, mid-way through a payment, on the app’s oldest supported OS. One of those is actionable. This is a piece about building the second one — for a Kotlin Multiplatform app, where “the second one” has to work identically on two platforms that share none of the same tooling underneath.

I’ll build it up in layers, because that’s how it should be built: a structured logger at the bottom, then the pipeline that carries its output to the places that matter, then the policies — redaction, noise control, privacy, environment gating — that decide what’s allowed through.

Structured Logging Is Not “printf With Levels”

Most logging in most apps is string concatenation with a severity attached:

Log.d("TAG", "User $userId loaded $count plans in ${elapsed}ms")
Enter fullscreen mode Exit fullscreen mode

That line is fine for a human reading Logcat at their desk. It is nearly useless everywhere else. You cannot query it. You cannot aggregate “loaded plans” events across ten thousand devices to get a p95. You cannot redact $userId without a regex that also mangles the rest. The moment the log leaves the developer's eyeballs and heads toward a backend, a file, or a crash report, its shape stops being prose and starts being data — and prose-shaped data is the thing you will regret.

Structured logging separates the stable message from the variable fields:

logger.info(
    attrs = {
        attr("plan_count", count)
        attr("duration_ms", elapsed)
    }
) { "plans loaded" }
Enter fullscreen mode Exit fullscreen mode

The message "plans loaded" is now a low-cardinality key you can group by. plan_count and duration_ms are typed attributes you can chart. And — this matters more than it looks — the message is a lambda. It is only ever built if the log level is actually enabled. Every senior codebase eventually finds the log line that dominates a hot path because someone wrote Log.d("state = ${expensiveSerialize(state)}") and shipped it; lazy message evaluation makes that mistake structurally impossible, not merely discouraged.

For the Kotlin Multiplatform app in this story, the logger at the bottom of the stack is KMP-Logger a small library I maintain on Maven Central. I’m going to use it as the concrete example because I know every corner of it, but nothing here is specific to it — the ideas port to Timber, to SLF4J, to whatever you already have. Its model is the one you want from any structured logger:

// One-liner API for casual call sites - picks up a default tag.
Log.i("app started")
Log.e("token refresh failed", throwable = error)

// Named logger with structured attributes for the paths you'll query later.
private val log = LoggerFactory.get("PlanScreen")
log.error(
    throwable = e,
    attrs = { attr("category", category); attr("retry_count", 3) },
) { "plan load failed" }
Enter fullscreen mode Exit fullscreen mode

Under the hood every call becomes a LogEvent — a level, a logger name (the tag), an optional message, a map of attributes, an ambient context, and an optional throwable. That object is the currency of the whole pipeline. Everything downstream — Logcat, the crash backend, the redactor — operates on LogEvent, not on a formatted string. Hold onto that; it's the hinge the entire design turns on.

Two Kotlin Multiplatform details worth naming, because they’re the difference between a logger that works in commonMain and one that only pretends to. First, the native output is per-platform under one API: the same Log.i lands in Logcat on Android, NSLog on iOS, console on web, stdout on JVM/native — resolved through expect/actual, invisible to your shared code. Second, there is no reflection and no Throwable::class.java in the shared path, because neither exists on Kotlin/Native. A logger built Android-first quietly assumes the JVM; one built multiplatform-first cannot, and that constraint turns out to make it cleaner everywhere.

One Pipe, Many Destinations — and the Insight Most Teams Miss

Here is the architectural claim this whole article is organized around, and the one I’d defend hardest in a review:

Logging and crash reporting are not two systems. They are one pipeline with two of its outputs wired to different backends.

Almost everyone builds them separately. There’s a logging call (Log.e(...)) and, somewhere else, a crash call (Crashlytics.recordException(...)), and the two drift. Logs have context the crash report lacks; crash reports fire on paths that never logged. You end up correlating them by timestamp at 2 a.m.

The fix is a sink. A structured logger’s output isn’t a destination, it’s a stream, and a sink is anything that consumes the stream:

interface LogSink {
    fun emit(event: LogEvent)
    fun flush()
}
Enter fullscreen mode Exit fullscreen mode

You install however many you want. In this app, three:

LoggerFactory.install(
    LoggerConfig.Builder()
        .minLevel(if (isDebug) LogLevel.VERBOSE else LogLevel.INFO)
        .addSink(DefaultLogSink()) // Logcat / NSLog / stdout
        .addSink(SanitizingSink(crashSink, minLevel = WARN)) // crash backend
        // (debug builds also add a pretty console sink)
        .build()
)
Enter fullscreen mode Exit fullscreen mode

The second sink is the payoff. It takes the same LogEvent stream Logcat sees and routes it into the crash backend by severity:

  • Below WARN: dropped. The crash backend does not care that a screen loaded.
  • WARN and above: written as a breadcrumb  — a timestamped line appended to a rolling buffer that the crash backend replays, in order, on the next crash. This is the “medical history”: the trail of steps that led to the fall.
  • ERROR and above with a throwable: additionally recorded as a non-fatal  — a handled exception, grouped and counted like a crash but without taking the process down, carrying the logger’s tag, message, and attributes as custom keys so the report knows its own identity.

Write one log line. It shows up in Logcat for the developer at their desk, and — if it’s serious enough — it becomes crash context for the incident you’ll debug next week, assembled automatically. You stop maintaining two mental models. The log you already write is the observability you were going to bolt on.

class CrashReporterLogSink(private val crashReporter: CrashReporter) : LogSink {
    override fun emit(event: LogEvent) {
        if (event.level.ordinal < LogLevel.WARN.ordinal) return
        crashReporter.log("[${event.level}] ${event.loggerName}: ${event.message}")

        val throwable = event.throwable ?: return
        if (event.level.ordinal < LogLevel.ERROR.ordinal) return
        crashReporter.recordNonFatal(throwable, keysFrom(event))
    }
}
Enter fullscreen mode Exit fullscreen mode

CrashReporter here is a deliberately small interface — log, setKey, recordNonFatal, setUserId, setCollectionEnabled — that maps exactly onto a crash backend's customization surface without leaking the SDK into feature code. That indirection is what lets the same sink target Crashlytics today and something else tomorrow, and it's what lets the sink exist in commonMain at all. More on why that matters when we get to the platform boundary.

The Observability Layer Is the One Layer Allowed to Fail Silently

Read that sink again and notice what’s missing: it has no error handling. Now think about where it runs. emit fires on whatever thread was trying to log — a background dispatcher, the main thread, a coroutine mid-flight. If recordNonFatal throws — a malformed key, an SDK in a bad state, an OOM at exactly the wrong moment — that exception surfaces on a thread whose only crime was trying to write a log line.

Your telemetry must never be the cause of the incident it exists to record. So the real sink wraps its body:

override fun emit(event: LogEvent) {
    if (event.level.ordinal < LogLevel.WARN.ordinal) return
    runCatching {
        crashReporter.log("[${event.level}] ${event.loggerName}: ${event.message}")
        // ... record non-fatal ...
    }
}
Enter fullscreen mode Exit fullscreen mode

This is the inversion that separates observability code from feature code. Everywhere else in the app, a swallowed exception is a bug — you want failures loud. Here, a swallowed exception is the contract. The logging layer is infrastructure the rest of the app leans on precisely because it can never push back. A logger that can crash the app is worse than no logger, because it fails in production, under load, on the exact devices you can’t reproduce.

The same principle shows up one level down, in startup. The pipeline is installed in Application.onCreate — the single worst place in the entire app to throw, because a crash there crash-loops every launch and there is no screen from which the user can escape it. So the installation, the SDK bring-up, everything in that window is defensive by default. Infrastructure that runs before the app has a face has to assume it's the last line standing.

Redaction Belongs in the Pipeline, Not at the Call Site

Now the uncomfortable part. That crash sink transmits log payloads off the device. And developers log everything — request bodies, headers, auth flows — especially the developer debugging the auth flow at 6 p.m. who logs the full Authorization header "just for a second" and forgets to delete it.

You cannot solve this with a code-review rule. “Don’t log secrets” is a rule every team has and every team violates, because the violation is invisible until it’s a breach. The person who leaks the token is never the person who wrote the policy.

So redaction moves into the pipeline, as a middleware sink that wraps the real one:

class SanitizingSink(
    private val delegate: LogSink,
    private val minLevel: LogLevel = LogLevel.VERBOSE,
) : LogSink {
    override fun emit(event: LogEvent) {
        if (event.level.ordinal < minLevel.ordinal) return
        delegate.emit(event.scrubbed()) // JWTs, Bearer tokens, password=…, otp=…, attr keys named "token"/"secret"/…
    }
}
Enter fullscreen mode Exit fullscreen mode

The point isn’t the regexes — those are the boring, conservative part (three-segment base64 for JWTs, Bearer …, name=value for the usual sensitive names, plus attribute keys that look sensitive get replaced wholesale). The point is where it sits. Because it wraps the sink, it doesn't matter which of the app's forty modules produced the event, or whether that module's author had ever heard of the redaction policy. Every payload bound for a backend passes through one chokepoint. Redaction stops being a discipline and becomes a property of the wire.

Two senior touches on top. It wraps only the off-device sink — Logcat on a developer’s own machine doesn’t need scrubbing and shouldn’t pay for it. And it carries a minLevel gate that short-circuits before the regex work: the crash sink ignores anything below WARN anyway, so scrubbing the INFO firehose would burn CPU on events that get dropped one layer later. Put the redactor where the secrets can escape, and make it free where they can't.

Not Every Exception Is a Bug

The first week the crash pipeline was live, the non-fatals dashboard lit up. Thousands of them. Almost all the same shape: a farmer’s phone lost signal mid-request, the HTTP call threw, the code logged it at ERROR with the throwable attached — and the sink dutifully filed it as a handled exception. UnknownHostException, SocketTimeoutException, connection reset, TLS handshake failure, over and over, from every corner of the app.

None of it was a bug. An observability system that reports everything reports nothing  — the signal you need drowns in the noise you can’t act on. A user in a valley with one bar is not an app defect. There is no fix. There is no code change. It is weather.

So the pipeline filters transport failures out of the non-fatal stream — while still leaving them as breadcrumbs, because “the last three calls timed out” is genuinely useful context for whatever does eventually crash:

val throwable = event.throwable
if (throwable == null ||
    event.level.ordinal < LogLevel.ERROR.ordinal ||
    throwable.hasTransportFailure() // walks the cause chain, bounded, self-cause-guarded
) return
crashReporter.recordNonFatal(throwable, keysFrom(event))
Enter fullscreen mode Exit fullscreen mode

The interesting decision is how you identify a transport failure across a multiplatform network stack, and it’s a small masterclass in the gap between what you’d like to match and what you can. The clean answer is “match these specific exception types.” You can’t. The timeout and stream-reset types inside a modern HTTP client are library-internal and get renamed by the release-mode optimizer, so neither an is SpecificType check nor a name match survives to production. The answer that does survive is to match their common ancestor — on Android, every offline/DNS/reset/timeout/TLS failure is ultimately a java.io.IOException, one supertype, no dependency on the network library, obfuscation-proof. The trade-off is honest and worth stating out loud: a genuine local-disk IOException gets suppressed too. It's rare, it's usually environmental, the breadcrumb still fires — accepted. Good filters come with a footnote, and if yours doesn't, you haven't found its edge yet.

On iOS the same predicate returns a flat false, because that platform's HTTP engine doesn't raise IOException and the network layer already demotes its transport errors to a warning at the source — so nothing reaches the filter to begin with. One expect/actual function, two honest platform-specific answers. Which is the whole texture of KMP: the policy is shared, the mechanism is not, and pretending otherwise is how you ship a filter that works on one platform and silently fails on the other.

Make the Report Answer “Why,” Not “What”

Back to the crash report I opened with — the one that told me nothing. The fix for that report is not more stack frames. It’s the two things a stack can never carry: the state the app was in, and the path the user took to get there.

Crash backends give you two instruments for exactly this, and most teams use neither:

Custom keys are session-global key/values pinned to the top of every subsequent report. They answer what state was the app in when it died. You set them once, centrally, from the app’s identity and navigation, and every crash that follows inherits them:

crashReporter.setKey("current_screen", screenName) // updated on every navigation
crashReporter.setKey("is_logged_in", userToken != null)
crashReporter.setKey("app_language", language) // nepali / english
crashReporter.setKey("app_environment", environment) // production / staging / local
crashReporter.setKey("district_id", districtId) // coarse region - never GPS
Enter fullscreen mode Exit fullscreen mode

Breadcrumbs are the timestamped, ordered trail — the path. Wire them into one place, the navigation observer, and every screen the user visited replays in sequence on the next crash. One listener, a flat graph, total coverage:

navController.addOnDestinationChangedListener { _, destination, _ ->
    val screen = destination.route?.toScreenName() ?: return@listener
    analyticsHelper.logScreenView(screen) // analytics gets it
    crashReporter.setKey("current_screen", screen) // crash state gets it
    crashReporter.log("screen: $screen") // crash trail gets it
}
Enter fullscreen mode Exit fullscreen mode

Notice that one navigation event feeds three consumers — analytics, crash state, crash trail — from a single centralized site. No screen calls this itself. Centralizing telemetry is not tidiness; it’s the only way it stays complete, because the screen someone adds next month gets covered whether or not they remember to instrument it. The telemetry a developer has to remember to add is the telemetry that’s missing on the screen where it matters.

And a genuinely non-obvious trap while we’re here: when you record a handled exception, the backend captures the throwable’s own stack — where it was thrown — not where you called recordNonFatal. For a network error thrown deep in the HTTP client's dispatcher, that stack is all library frames, none of yours. You cannot tell the source from the stack. The only way to attribute it is the custom keys the sink attaches — the logger tag, the URL, the method. The keys aren't decoration; for a whole class of non-fatals they're the only thing that says where the failure came from.

Privacy Is an Architecture Decision, Not a Review Comment

Every method that reaches a backend in this app carries the same warning in its contract, and it is load-bearing:

Never pass PII — phone numbers, names, free text, exact GPS, account numbers, amounts tied to identity — through any method here. Use coarse, derived signals only.

This is not boilerplate. For this app it’s existential in a way western analytics guides rarely feel. The user is a farmer. Their phone number is their identity, their login, and — in a country where mobile money rides on that number — a key to their money. An exact GPS coordinate isn’t a “location,” it’s their house and their field. The cost of a leak here is not a GDPR fine; it’s a specific person, findable.

So the privacy posture is built into the shape of the telemetry, not left to whoever’s writing the event:

  • Coarse over exact, always. district_id, not latitude/longitude. is_company, not the company name. A price bucket, not the amount. You cannot leak what you never collected.
  • Pseudonymous identity, one-way. To stitch a user’s events across sessions you need a stable id, but it must never be the phone number. It’s a hash — computed on-device, one-directional by design, the raw number never leaving the phone. (Choose that hash carefully: a naive hash of a value from a small, guessable keyspace — like a national phone-number range — is reversible by brute force, which quietly turns your “pseudonymous” id back into PII. Salt it, or accept and document the risk deliberately; don’t stumble into it.)
  • The interface is where the rule lives. Because every call site depends on one small AnalyticsHelper / CrashReporter interface whose documented contract bans PII, the constraint is visible at the exact moment an engineer is about to log something. A rule in a wiki is a rule nobody reads. A rule in the KDoc of the method you're calling is a rule you can't miss.

The general principle: minimize at the source. Every downstream policy — retention, access control, redaction — is a mitigation for data you chose to collect. The data you decline to collect needs no policy at all.

Free in Development, Silent in Production

There’s a tension in everything above. In development you want the full firehose — every event, pretty-printed, in your face. In production you want almost none of it leaving the device, and what does leave sanitized and gated. The same code has to do both.

The pattern is a debug/release swap at the dependency-injection seam. Every observability abstraction has a real implementation and a no-op (or log-only) one, and the container picks by build type:

single<AnalyticsHelper> { if (isDebug) StubAnalyticsHelper() else FirebaseAnalyticsHelper() }
single<CrashReporter> { if (isDebug) StubCrashReporter() else FirebaseCrashReporter() }
single<PerformanceTracer> { if (isDebug) NoOpPerformanceTracer else FirebasePerformanceTracer() }
Enter fullscreen mode Exit fullscreen mode

In debug, every call is real code that goes nowhere near a backend — the stub logs to the console so you can see what would have been sent, and nothing ships. In release, the SDK-backed implementations report. Feature code is identical in both; it depends on the interface and never learns which body it got. This is belt-and-suspenders with a second buckle: the SDKs’ own collection flags are also gated off in debug at the manifest/startup level, so even the automatic events (app-start traces, screen renders) stay quiet. Two independent guards, because “we didn’t mean to send that from a debug build” is a mistake you get to make exactly once before it’s real users’ data.

A subtle bonus: because the debug path is real objects, not null and not a compile-time strip, the observability code runs in every test and every developer session. It can't rot. The no-op that never executes is the no-op that's silently broken when you finally need it.

Traces: Measure What the SDK Can’t See — and Don’t Double-Count What It Can

The third pillar is performance, and here the senior move is mostly about restraint.

Modern performance SDKs auto-instrument a lot for free: app start, foreground/background, screen rendering, and — critically — network requests, because the HTTP client runs over the platform’s native stack (OkHttp on Android, NSURLSession on iOS) which the SDK already hooks. The rookie instinct is to wrap every network call in a custom trace. Don't. You'll double-count what the SDK already measures, and now you have two numbers that disagree and no idea which to trust.

Custom traces earn their place only for spans the SDK cannot see — app-level operations that span several calls plus local work: a feed assembling from three sources, a plan list loading and rendering, a chat message with an image and audio payload being prepared and sent. Those are real user-perceived durations that no automatic hook captures.

And when you do trace, add the dimension that makes the number mean something:

performanceTracer.trace("plan_load") { trace ->
    category?.let { trace.putAttribute("category", it) }
    fetch().also { result ->
        trace.putAttribute("result", when {
            result.isSuccess -> "success"
            result.isEmpty -> "empty" // no plans in this category - normal, NOT an error
            else -> "error"
        })
        result.getOrNull()?.let { trace.putMetric("plan_count", it.size.toLong()) }
    }
}
Enter fullscreen mode Exit fullscreen mode

That empty bucket is the whole lesson. An empty result is a normal outcome — this category has no plans yet — but the naive two-way success/error split files it under error, and now your error rate is fiction and your alerting cries wolf. The distinction between "failed" and "succeeded with nothing to show" is invisible to any automatic instrument and invisible to a boolean; you have to name it. A trace's segments are only as honest as the person who bucketed them.

The Part That’s Actually Hard: Two Platforms, One Pipeline

Everything so far would be true on a single-platform app. Kotlin Multiplatform adds one constraint that reshapes the whole design: the shared code in commonMain cannot see a platform SDK. No Firebase types, no android.util.Log, no NSError. The pipeline lives in shared code; the backends live on the platforms. Something has to cross that gap without dragging the SDK across with it.

Three moves make it work, and they’re the transferable part for anyone doing native-SDK work in KMP:

The SDK type never crosses the boundary — a handle does. A performance trace is a live SDK object with a lifecycle. It must not leak into Kotlin. So the Swift side owns the real Trace objects in a map and hands Kotlin an opaque token string; Kotlin passes the token back on putAttribute/stop, Swift resolves it to the real trace. Kotlin holds a String; the SDK object stays on its own side of the wall, its type never named in shared code.

Impedance mismatches get translated at the edge. A crash backend cannot ingest a Kotlin Throwable. So the bridge reconstitutes it as a native error — domain set to the Kotlin class name (so issues still group correctly), the message as the description, the Kotlin/Native stack tucked into the error's userInfo. A handled KMP failure surfaces as a grouped non-fatal with its real stack attached, even though the crash SDK has never heard of Kotlin.

Watch the module graph, or it’ll bite you. The logging module cannot depend on the analytics/crash module — but the crash sink needs the crash reporter. If you wire the sink inside the logger, you get a dependency cycle, and on iOS the added twist that the crash sink needs a native bridge that lives in the analytics module, not the logging one. The resolution is dependency inversion at the seam: the logger’s installer accepts a sink from outside — install(isDebug, crashSink) — and startup, which sits above both modules and can see everything, constructs the crash sink and injects it, release builds only. The logging module stays SDK-agnostic and reusable; the wiring happens at the one place that's allowed to know about both. When two low-level modules need to cooperate, the cooperation usually belongs to a third module above them, not to a new edge between them.

What We’ve Built

Step back and the whole thing is one pipeline with policies bolted to it at the right layers:

  • A structured logger (KMP-Logger at the base — typed events, lazy messages, per-tag levels, native output on every platform — so the app logs data, not prose.
  • One stream, many sinks: Logcat for the developer, a crash sink for incidents, a performance tracer for durations. The log you write once becomes the crash context you read later, assembled for free.
  • A sink that can’t crash the app, because the observability layer is the one layer whose failures must be silent.
  • Redaction in the pipeline, not at the call site, so no future engineer can leak a token by forgetting a rule.
  • A non-fatal filter that drops the weather and keeps the bugs, with its trade-off written down.
  • Crash reports that answer “why”: centralized custom keys for the state, breadcrumbs for the path, keys-as-attribution for the stacks that lie.
  • Privacy in the shape of the data  — coarse, derived, pseudonymous — because the cheapest data to protect is the data you never collected.
  • A debug/release swap that’s a firehose in development and silent in production, guarded twice.
  • Traces for what the SDK can’t see, with an honest success/empty/error dimension, and the discipline not to double-count what it can.
  • All of it shared across Android and iOS , with the SDK objects held firmly on their own side of the cinterop wall.

None of these are exotic. Each is one small decision made at the layer where it belongs. That’s the actual thesis: observability is not a product you install, it’s a set of boundaries you choose  — what fails silently, what gets scrubbed, what gets dropped, what never gets collected. The SDK gives you a pipe. Where you put the valves is the engineering.

What’s Next

A few edges I’m still working, in case you’re ahead of me on any: sampling high-volume breadcrumbs on the very lowest-memory devices, where even the rolling buffer has a cost; a structured-event schema shared between the analytics catalog and the crash keys so the two vocabularies can’t drift; and richer offline buffering, since a meaningful share of our users are disconnected at the exact moment something interesting happens and the report has to survive until they reconnect.

If you’re building observability for a multiplatform app — or arguing with someone about whether logs and crashes are one system or two — I’d genuinely like to hear how you drew the lines. And if you want the structured-logging layer underneath all of this, KMP-Logger is open source and on Maven Central. Tell me what breaks. ❤️

Top comments (0)