Android Logs installation
The PostHog Android SDK has built-in support for capturing structured Logs from Android apps. The SDK handles the OTLP encoding, batching, on-disk persistence across app restarts, and lifecycle integration. You just call PostHog.logger.{trace,debug,info,warn,error,fatal}(...).
Manual capture only. Logs are emitted by your code. The SDK does not autocapture system log streams (
Log.d,Logcat,Timber).
Minimum version:
com.posthog:posthog-android@3.46.0or later. Bump the dependency in yourbuild.gradle(orbuild.gradle.kts) and re-sync.
- 1
Install posthog-android
RequiredIf you haven't installed
posthog-androidyet, follow the steps below. For full details, see the Android SDK guide.The best way to install the PostHog Android library is with a build system like Gradle. This ensures you can easily upgrade to the latest versions.
All you need to do is add the
posthog-androidmodule to your App'sbuild.gradleorbuild.gradle.kts:Configuration
The best place to initialize the client is in your
Applicationsubclass.Kotlin - 2
Configure logs in your PostHogAndroidConfig
RequiredConfigure Logs through
config.logsbefore callingPostHogAndroid.setup(...). All fields are optional; defaults are tuned for mobile (cellular bandwidth, battery, app lifecycle).KotlinThese resource attributes are captured at
setup(...)and apply to every batch. Mutatingconfig.logs.serviceName,environment,serviceVersion, orresourceAttributesafter setup has no effect. - 3
Capture logs
RequiredUse
PostHog.loggerfor the per-level convenience API.KotlinAvailable severity levels:
TRACE,DEBUG,INFO,WARN,ERROR,FATAL.Records are buffered, batched, persisted to disk, and flushed automatically – every 30 seconds, when the buffer hits the threshold, when the app moves to the background, or on
PostHog.flush().flush()drains events, Session Replay, and Logs together.Each record is automatically tagged with the current distinct ID, session ID, current screen, app foreground/background state, and active Feature Flags at the moment of capture.
From Java:
Java - 4
Test your setup
Recommended- Capture a test log from your app:Kotlin
- Open the PostHog Logs UI.
- Filter by
service.name = 'my-app'(or whatever value you set above).
You should see your record arrive within a few seconds.
- Capture a test log from your app:
- 5
Tune buffering, rate cap, and resource attributes
OptionalThe
logsconfig has knobs for high-volume apps:KotlinFull configuration reference:
Field Default What it does serviceNameapp package id OTLP service.nameresource attributeserviceVersionBuildConfig.VERSION_NAMEOTLP service.versionresource attributeenvironmentnullOTLP deployment.environmentresource attributeresourceAttributes{}Extra OTLP resource attributes (SDK keys win on collision) flushIntervalSeconds30Periodic flush interval flushAt20Buffer threshold that triggers an automatic flush maxBatchSize50Max records per outbound POST (halved on 413) maxBufferSize1000Max records held on disk before FIFO eviction rateCapMaxLogs500Max records per rateCapWindowSecondswindow. Set to0to disable.rateCapWindowSeconds10Rate-cap tumbling window length serviceName,serviceVersion,environment,resourceAttributes,flushAt, andmaxBatchSizeare captured atsetup(...); mutating them later has no effect.flushIntervalSeconds,maxBufferSize, and rate-cap fields are re-read at runtime. Defaults are tuned for cellular-aware mobile apps. RaiserateCapMaxLogsandmaxBufferSizefor high-volume scenarios. - 6
Filter or redact with beforeSend
OptionalbeforeSendruns synchronously before the rate cap, so dropped records don't consume the per-window budget. Use it for redaction, sampling, or filtering by level. Each hook receives an immutablePostHogLogRecordand returns either a (possibly modified) record ornullto drop it.KotlinCall
addBeforeSendmultiple times to compose a chain – hooks are evaluated left-to-right (registration order). Returningnullfrom any hook short-circuits and drops the record. A hook that throws is treated the same as returningnull(the record is dropped, the exception is logged via the SDK's internal debug logger). Returning a record with a blank body also drops the record.addBeforeSendandremoveBeforeSendare live – added or removed hooks take effect on the nextcaptureLogcall.From Java, register a
PostHogBeforeSendLogSAM:Java Next steps
CheckpointWhat you can do with your logsAction Description Why you need logs What logs show you that nothing else does Search logs Use the search interface to find specific log entries Filter by level Filter by INFO,WARN,ERROR, etc.Link session replay Connect logs to users and session replays by passing posthogDistinctIdandsessionIdLogging best practices Learn what to log, how to structure logs, and patterns that make logs useful in production