React Native logs installation
PostHog's React Native SDK has built-in support for capturing structured logs. Unlike other languages where you wire OpenTelemetry directly, the SDK handles the OTLP encoding, batching, persistence, and lifecycle for you. You just call posthog.captureLog(...) or posthog.logger.{trace,debug,info,warn,error,fatal}(...).
JavaScript layer only. Logs are captured from the JavaScript side of your app. Native logs from your iOS or Android code (e.g.
os_log,Log.d) are not collected.
Minimum version:
posthog-react-native@4.44.0or later. Runnpx expo install posthog-react-native(Expo) or your package manager's equivalent to update.
- 1
Install posthog-react-native
RequiredIf you haven't already, install and initialize
posthog-react-nativeusing the steps below. For full details, see the React Native SDK guide.Our React Native enables you to integrate PostHog with your React Native project. For React Native projects built with Expo, there are no mobile native dependencies outside of supported Expo packages.
To install, add the
posthog-react-nativepackage to your project as well as the required peer dependencies.Expo apps
TerminalReact Native apps
TerminalReact Native Web and macOS
If you're using React Native Web or React Native macOS, do not use the expo-file-system package since the Web and macOS targets aren't supported, use the @react-native-async-storage/async-storage package instead.
Configuration
With the PosthogProvider
The recommended way to set up PostHog for React Native is to use the
PostHogProvider. This utilizes the Context API to pass the PostHog client around, and enables autocapture.To set up
PostHogProvider, add it to yourApp.jsorApp.tsfile:App.jsThen you can access PostHog using the
usePostHog()hook:React NativeWithout the PosthogProvider
If you prefer not to use the provider, you can initialize PostHog in its own file and import the instance from there:
posthog.tsThen you can access PostHog by importing your instance:
React NativeYou can even use this instance with the PostHogProvider:
React Native - 2
Configure logs in your PostHog options
RequiredAdd a
logsblock to your PostHog initialization. All fields are optional; defaults are tuned for mobile (cellular bandwidth, battery, OS lifecycle).React Native - 3
Capture logs
RequiredUse
posthog.loggerfor the per-level convenience API, orposthog.captureLogfor full control over level, attributes, and trace context.React NativeRecords are buffered, batched, persisted to disk, and flushed automatically – every 10 seconds, on AppState change (foreground ↔ background), on buffer fill, or on
posthog.shutdown(). For an immediate drain, callawait posthog.flushLogs().Each record is automatically tagged with the user's distinct ID, session ID, current screen, app foreground/background state, and active feature flags at the moment of capture.
- 4
Test your setup
Recommended- Capture a test log from your app:React Native
- 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 filtering
OptionalThe
logsconfig has knobs for high-volume apps:React NativeFull configuration reference:
Field Default What it does serviceName'unknown_service'OTLP service.nameresource attributeserviceVersionundefined OTLP service.versionresource attributeenvironmentundefined OTLP deployment.environmentresource attributeresourceAttributes{}Extra OTLP resource attributes flushIntervalMs10000Periodic flush interval in ms maxBufferSize100Max records held in memory before eviction maxBatchRecordsPerPost50Max records per outbound POST (halved on 413) rateCap.maxLogs500Max records per windowMswindowrateCap.windowMs10000Rate-cap window length in ms beforeSendundefined Pre-send filter (return nullto drop)Defaults are tuned for cellular-aware mobile apps (~50 logs/sec ceiling, ~16KB max queue file). Raise
rateCap.maxLogsandmaxBufferSizefor high-volume scenarios. - 6
Filtering with beforeSend
OptionalThe
beforeSendhook runs synchronously before the rate cap, so dropped records don't consume the per-interval budget. Use it for redaction, sampling, or filtering by level:React NativeYou can also pass an array of functions to form a chain (evaluated left-to-right). A
nullreturn from any link short-circuits and drops the record. A throwing filter is caught and skipped – the chain continues with the previous return value, so a buggy filter degrades to a no-op rather than crashing your app. 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