JavaScript web logs installation
The PostHog JavaScript web SDK ships with first-class support for Logs. Send structured log records with the posthog.logger API or opt in to autocapture of console.* calls.
Minimum version:
posthog-js@1.368.0or later. Runnpm update posthog-js(or your package manager's equivalent) to update.
- 1
Install posthog-js
RequiredIf you haven't already, install
posthog-jsusing your package manager: - 2
Configure logs at init
RequiredSet your service identity in
posthog.init. These fields map to standard OpenTelemetry resource attributes and apply to every log record this client sends.WebOption Description serviceNameIdentifies the app in the Logs UI. Maps to the OpenTelemetry service.nameresource attribute. Defaults to'unknown_service'.environmentDeployment environment, e.g. 'production'or'staging'. Maps todeployment.environment.serviceVersionRelease version, e.g. '1.2.3'. Maps toservice.version.resourceAttributesAdditional OpenTelemetry resource attributes, e.g. { 'host.name': 'web-01' }.flushIntervalMsHow often batched log records are flushed. Defaults to 3000.maxBufferSizeBuffer size before forcing a flush. Defaults to 100.maxLogsPerIntervalRate limit per flush window. Excess records are dropped with a single warning. Defaults to 1000.See the web SDK config reference for the complete list.
- 3
Send structured logs with posthog.logger
Recommendedposthog.loggeris the declarative path. Each call produces an OTLP log record with a severity level and typed attributes you can filter and query on in the Logs product.WebAvailable severity levels:
trace,debug,info,warn,error, andfatal.Logger calls don't write to the browser console, they go directly to PostHog as structured records. You can query logs by attribute, such as
total_cents > 1000, instead of string-searching log output. Nothing extra appears in the user's browser console.For full control over a log record, including trace correlation, use
posthog.captureLog:WebAttribute values can be strings, numbers, booleans, arrays, or plain objects.
nullandundefinedvalues are dropped. - 4
Autocapture console logs
OptionalTo forward
console.log,console.warn,console.error, and friends without changing your code, setlogs.captureConsoleLogs: true.WebWarning: Overriding a system library like
consolehas a large blast radius. Everything your app or its dependencies write to the console gets forwarded to PostHog, including:- Debug output referencing passwords, tokens, API keys, or session identifiers
- Stack traces containing user input
- Third-party library chatter you don't control
This can breach data privacy compliance and is difficult to clean up. Before turning it on:
- Audit what your app and its dependencies currently log.
- Get sign-off from anyone in your organization whose code runs in the browser.
- Prefer
posthog.loggerfor anything you actually care about – it's structured, queryable, and doesn't pull in unrelated output.
posthog.loggerandcaptureConsoleLogsare independent and can be enabled together. The logger path emits OTLP records directly; the autocapture path scrapes console strings. - 5
Link logs to sessions and users
RecommendedWhen
posthog-jsis initialized, the web SDK automatically attaches the currentdistinct_idandsession_idto every log record. Log entries appear linked to the matching Session Replay and person without any extra setup.See Link Session Replay for how this surfaces in the UI, and how to do the equivalent from server-side SDKs.
- 6
Test your setup
RecommendedOnce everything is configured, verify log records are flowing:
- Call
posthog.logger.info('hello from posthog-js')somewhere your page executes. - Open the Logs interface and filter by the
serviceNameyou set above. - Confirm the record shows up with the attributes you sent.
- Call
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