Install OpenTelemetry logging

Logs is in beta

Logs is currently in early beta. While in beta, logs is free to use.

We're always looking for feedback to improve logs, please reach out to us directly in app.

  1. Install OpenTelemetry packages

    Required

    PostHog logs works with any OpenTelemetry-compatible client. You don't need any PostHog-specific packages - just use standard OpenTelemetry libraries.

    Terminal
    npm install @opentelemetry/sdk-node @opentelemetry/exporter-logs-otlp-http @opentelemetry/api-logs
  2. Get your project token

    Required

    You'll need your PostHog project token to authenticate log requests. This is the same token you use for capturing events and exceptions.

    You can find your project token in Project Settings.

  3. Configure the SDK

    Required

    Set up the OpenTelemetry SDK to send logs to PostHog.

    JavaScript
    import { NodeSDK } from '@opentelemetry/sdk-node';
    import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http';
    import { SimpleLogRecordProcessor } from '@opentelemetry/sdk-logs';
    const sdk = new NodeSDK({
    logRecordProcessor: new SimpleLogRecordProcessor(
    new OTLPLogExporter({
    url: 'https://us.i.posthog.com/i/v1/logs',
    headers: {
    'Authorization': `Bearer ${YOUR_PROJECT_TOKEN}`
    }
    })
    )
    });
    sdk.start();

    Alternatively, you can pass the token as a query parameter:

    JavaScript
    const sdk = new NodeSDK({
    logRecordProcessor: new SimpleLogRecordProcessor(
    new OTLPLogExporter({
    url: `https://us.i.posthog.com/i/v1/logs?token=${YOUR_PROJECT_TOKEN}`
    })
    )
    });
  4. Use OpenTelemetry logging

    Required

    Now you can start logging with OpenTelemetry. Here are examples for each language:

    JavaScript
    import { logs } from '@opentelemetry/api-logs';
    const logger = logs.getLogger('my-app');
    // Log with different levels
    logger.info('User action', { userId: '123', action: 'login' });
    logger.warn('Deprecated API used', { endpoint: '/old-api' });
    logger.error('Database connection failed', { error: 'Connection timeout' });
  5. Test your setup

    Recommended

    Once everything is configured, test that logs are flowing into PostHog:

    1. Send a test log from your application
    2. Check the PostHog logs interface for your log entries
    3. Verify the logs appear in your project
    View your logs in PostHog
  6. Next steps

    Checkpoint
    What you can do with your logs
    ActionDescription
    Search logsUse the search interface to find specific log entries
    Filter by levelFilter by INFO, WARN, ERROR, etc.
    Set up alertsGet notified when specific log patterns occur
    Correlate with eventsConnect log data with your PostHog analytics
    Troubleshoot common issues

Community questions

Was this page useful?

Questions about this page? or post a community question.