references/nodejs.md
AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt
Node.js Logs installation
Canonical URL: https://posthog.com/docs/logs/installation/nodejs
Cloud region: This page uses US Cloud hosts. For an EU Cloud project, use
https://eu.i.posthog.cominstead ofhttps://us.i.posthog.com(ingestion host), and usehttps://eu.posthog.cominstead ofhttps://app.posthog.com(app host for links).
On this page
- Install OpenTelemetry packages
- Get your project token
- Configure the SDK
- Use OpenTelemetry logging
- Test your setup
- Next steps
Note: If you use Pino or Winston, follow the Pino or Winston guide instead. It sends your existing logs without changes to your logger calls.
-
1
Install OpenTelemetry packages
Required
npm install @opentelemetry/sdk-node @opentelemetry/exporter-logs-otlp-http @opentelemetry/api-logs @opentelemetry/resources @opentelemetry/sdk-logs -
2
Get your project token
Required
You'll need your PostHog project token to authenticate log requests. This is the same key you use for capturing events and exceptions with the PostHog SDK.
Important: Use your project token which starts with
phc_. Do not use a personal API key (which starts withphx_).You can find your project token in Project Settings.
-
3
Configure the SDK
Required
Set up the OpenTelemetry SDK to send logs to PostHog.
import { NodeSDK } from '@opentelemetry/sdk-node'; import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http'; import { BatchLogRecordProcessor } from '@opentelemetry/sdk-logs'; import { resourceFromAttributes } from '@opentelemetry/resources'; const sdk = new NodeSDK({ resource: resourceFromAttributes({ 'service.name': 'my-node-service', }), logRecordProcessors: [ new BatchLogRecordProcessor({ exporter: new OTLPLogExporter({ url: 'https://us.i.posthog.com/i/v1/logs', headers: { 'Authorization': 'Bearer <ph_project_token>' } }) }) ] }); sdk.start();Alternatively, you can pass the API key as a query parameter:
const sdk = new NodeSDK({ logRecordProcessors: [ new BatchLogRecordProcessor({ exporter: new OTLPLogExporter({ url: 'https://us.i.posthog.com/i/v1/logs?token=<ph_project_token>' }) }) ] }); -
4
Use OpenTelemetry logging
Required
Now you can start logging with OpenTelemetry:
import { logs } from '@opentelemetry/api-logs'; const logger = logs.getLogger('my-app'); // Log with different levels and attributes logger.emit({ severityText: 'trace', body: 'log data', attributes: {'my_attribute': 'stringValue'} }); logger.emit({ severityText: 'warn', body: 'log data', attributes: {'warning_count': 3} }); logger.emit({ severityText: 'error', body: 'log data', attributes: {'json_attribute': [1,2,3]} }); -
5
Test your setup
Recommended
Once everything is configured, test that logs are flowing into PostHog:
- Send a test log from your application
- Check the PostHog Logs interface for your log entries
- Verify the logs appear in your project
-
Next steps
Checkpoint
What you can do with your logs
| Action | 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 passingposthogDistinctIdandsessionId| | Link logs to a person | Surface every log emitted on behalf of a user on their PostHog person profile | | Logging best practices | Learn what to log, how to structure logs, and patterns that make logs useful in production |