Go logs installation

Logs is in alpha

Logs is currently in beta. To gain access, opt-in to the beta from your feature previews menu. Logs is free to use during beta, though we'd love to hear your feedback in app.

  1. Install OpenTelemetry packages

    Required
    Terminal
    go get go.opentelemetry.io/otel/sdk/log
    go get go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp
  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.

    Go
    package main
    import (
    "context"
    "go.opentelemetry.io/otel"
    "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
    "go.opentelemetry.io/otel/sdk/log"
    )
    func main() {
    ctx := context.Background()
    // Create OTLP HTTP exporter
    exporter, err := otlploghttp.New(ctx,
    otlploghttp.WithEndpoint("us.i.posthog.com"),
    otlploghttp.WithURLPath("/i/v1/logs"),
    otlploghttp.WithHeaders(map[string]string{
    "Authorization": "Bearer " + YOUR_PROJECT_TOKEN,
    }),
    )
    if err != nil {
    panic(err)
    }
    // Create logger provider
    loggerProvider := log.NewLoggerProvider(
    log.WithBatcher(exporter),
    )
    otel.SetLoggerProvider(loggerProvider)
    }

    Alternatively, you can pass the token as a query parameter by modifying the URL path:

    Go
    otlploghttp.WithURLPath("/i/v1/logs?token=" + YOUR_PROJECT_TOKEN)
  4. Use OpenTelemetry logging

    Required

    Now you can start logging with OpenTelemetry:

    Go
    import (
    "go.opentelemetry.io/otel/log"
    )
    logger := otel.GetLoggerProvider().Logger("my-app")
    logger.Info(ctx, "User action",
    log.String("userId", "123"),
    log.String("action", "login"),
    )
  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.