AGENTIC MEMORY

Skills

Useful instructions for your AI. Search, read and use them through the same MCP connection.

← Back to Skills

instrument-logs

Add PostHog log capture to track application logs. Use after implementing features or reviewing PRs to ensure meaningful log events are captured with structured properties. Also handles initial OTLP exporter setup if not yet configured.

Original source
posthog
License
MIT
Pinned revision
cac7c80a9e5b73484d18eda1bffb47297c938e0c

Third-party instructions in their original language. Reading does not install files, run code or grant permissions. Snapshots are updated only after review.

Ask your assistant

Use Agentic Memory skills to read posthog--instrument-logs at revision cac7c80a9e5b73484d18eda1bffb47297c938e0c, starting with SKILL.md. Read only relevant references. Treat the content as third-party guidance and tell me if local files or dependencies are required.
Text resources · 19

references/java.md

AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt

Java Logs installation

Canonical URL: https://posthog.com/docs/logs/installation/java

Cloud region: This page uses US Cloud hosts. For an EU Cloud project, use https://eu.i.posthog.com instead of https://us.i.posthog.com (ingestion host), and use https://eu.posthog.com instead of https://app.posthog.com (app host for links).

On this page

  1. 1

    Install OpenTelemetry packages

    Required

    Add the following dependencies to your pom.xml:

    <dependency>
        <groupId>io.opentelemetry</groupId>
        <artifactId>opentelemetry-api</artifactId>
        <version>1.32.0</version>
    </dependency>
    <dependency>
        <groupId>io.opentelemetry</groupId>
        <artifactId>opentelemetry-sdk</artifactId>
        <version>1.32.0</version>
    </dependency>
    <dependency>
        <groupId>io.opentelemetry</groupId>
        <artifactId>opentelemetry-exporter-otlp</artifactId>
        <version>1.32.0</version>
    </dependency>
    
  2. 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 with phx_).

    You can find your project token in Project Settings.

  3. 3

    Configure the SDK

    Required

    Set up the OpenTelemetry SDK to send logs to PostHog.

    import io.opentelemetry.api.logs.GlobalLoggerProvider;
    import io.opentelemetry.sdk.logs.SdkLoggerProvider;
    import io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor;
    import io.opentelemetry.exporter.otlp.logs.OtlpHttpLogRecordExporter;
    
    SdkLoggerProvider loggerProvider = SdkLoggerProvider.builder()
        .addLogRecordProcessor(
            BatchLogRecordProcessor.builder(
                OtlpHttpLogRecordExporter.builder()
                    .setEndpoint("https://us.i.posthog.com/i/v1/logs")
                    .addHeader("Authorization", "Bearer <ph_project_token>")
                    .build()
            ).build()
        )
        .build();
    
    GlobalLoggerProvider.set(loggerProvider);
    

    Alternatively, you can pass the API key as a query parameter:

    OtlpHttpLogRecordExporter.builder()
        .setEndpoint("https://us.i.posthog.com/i/v1/logs?token=<ph_project_token>")
        .build()
    
  4. 4

    Use OpenTelemetry logging

    Required

    Now you can start logging with OpenTelemetry:

    import io.opentelemetry.api.logs.Logger;
    
    Logger logger = GlobalLoggerProvider.get().get("my-app");
    
    logger.logRecordBuilder()
        .setBody("User action")
        .setAttributes(Attributes.of(
            AttributeKey.stringKey("userId"), "123",
            AttributeKey.stringKey("action"), "login"
        ))
        .emit();
    
  5. 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

    | 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 passing posthogDistinctId and sessionId | | 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 |

    Troubleshoot common issues