Add PostHog log capture
Use this skill to add PostHog log capture for new or changed code. Use it after implementing features or reviewing PRs to ensure meaningful log events are captured with structured properties. If PostHog log export is not yet configured, this skill also covers initial OTLP exporter setup. Supports any platform or language.
Supported platforms: Next.js, Node.js, Python, Go, Java, Datadog, Android, React Native, iOS, and any language via OpenTelemetry.
Instructions
Follow these steps IN ORDER:
STEP 1: Analyze the codebase and detect the platform.
- Detect the language, framework, and existing logging setup.
- Look for dependency files and project files (package.json, Podfile, Package.swift, requirements.txt, go.mod, pom.xml, etc.).
- Look for log libraries (winston, pino, logging module, logrus, log4j, serilog, os_log, Logger, etc.).
- Look for lockfiles (pnpm-lock.yaml, package-lock.json, yarn.lock, bun.lockb, go.sum, Podfile.lock, Package.resolved, etc.) to determine the package manager.
- Check for existing PostHog log export setup. If the OTLP exporter is already configured, skip to STEP 5 to add log capture for new code.
STEP 2: Research log capture. (Skip if PostHog log export is already configured.)
2.1. Find the reference file below that matches the detected platform — it is the source of truth for OTLP exporter configuration and integration with existing logging. Read it now.
2.2. If no reference matches, use the "Other Languages" reference as a fallback — it covers the generic OpenTelemetry approach.
STEP 3: Install dependencies. (Skip if PostHog log export is already configured.)
- Install the OpenTelemetry SDK and OTLP exporter packages for the detected platform.
- Do not manually edit dependency files — use the package manager's install command.
STEP 4: Configure the OTLP exporter. (Skip if PostHog log export is already configured.)
- PostHog logs use the OpenTelemetry protocol. Set up an OTLP exporter pointed at PostHog's ingest endpoint.
- For SDK-native log support such as Android, React Native, and iOS, follow the platform reference instead of adding a separate OTLP exporter.
- Follow the platform-specific reference for the exact configuration.
STEP 5: Integrate with existing logging.
- Add the PostHog log exporter alongside existing logging. Don't replace existing log handlers or outputs.
- Do not alter the fundamental architecture of existing files. Make additions minimal and targeted.
- You must read a file immediately before attempting to write it.
STEP 6: Add structured properties.
- Ensure logs include structured key-value properties for filtering and search in PostHog.
- Prefer structured log formats with key-value properties over plain text messages.
STEP 7: Set up environment variables.
- Check if the project already has PostHog environment variables configured (e.g. in
.env, .env.local, or framework-specific env files). If valid values already exist, skip this step.
- If the PostHog project token is missing, use the PostHog MCP server's
projects-get tool to retrieve the project's api_token. If multiple projects are returned, ask the user which project to use. If the MCP server is not connected or not authenticated, ask the user for their PostHog project token instead.
- For the PostHog host URL: check the
projects-get MCP response for a region field — US maps to https://us.i.posthog.com, EU maps to https://eu.i.posthog.com. If the region is not available from the MCP response or from existing project configuration, ask the user: "Are you on PostHog US Cloud or EU Cloud?" Do not assume US Cloud.
- For the OpenTelemetry endpoint, use
https://us.i.posthog.com/v1 (US) or https://eu.i.posthog.com/v1 (EU), matching the region determined above.
- Write these values to the appropriate env file using the framework's naming convention.
- Reference these environment variables in code instead of hardcoding them.
Reference files
references/nextjs.md - Next.js logs installation - docs
references/nodejs.md - Node.js logs installation - docs
references/python.md - Python logs installation - docs
references/go.md - Go logs installation - docs
references/java.md - Java logs installation - docs
references/datadog.md - Datadog logs installation - docs
references/android.md - Android logs installation - docs
references/react-native.md - React native logs installation - docs
references/ios.md - Ios logs installation - docs
references/flutter.md - Flutter logs installation - docs
references/other.md - Other languages logs installation - docs
references/start-here.md - Getting started with logs - docs
references/search.md - Search logs - docs
references/best-practices.md - Logging best practices - docs
references/troubleshooting.md - Logs troubleshooting - docs
references/link-session-replay.md - Link session replay - docs
references/mcp.md - Use logs over PostHog mcp - docs
references/COMMANDMENTS.md - Framework-specific rules the integration must follow
Each platform reference contains specific OTLP configuration, SDK setup, and integration patterns. Find the one matching the user's stack.
Key principles
- Environment variables: Always use environment variables for PostHog keys and OpenTelemetry endpoints. Never hardcode them.
- Minimal changes: Add log export alongside existing logging. Don't replace or restructure existing logging code.
- OpenTelemetry: PostHog logs use the OpenTelemetry protocol. Configure an OTLP exporter pointed at PostHog's ingest endpoint unless the platform SDK provides native log capture.
- SDK-native logs: For Android, React Native, and iOS, use the SDK logger/capture APIs from the platform reference instead of adding a separate OTLP exporter.
- Structured logging: Prefer structured log formats with key-value properties over plain text messages.
1---
2name: instrument-logs
3description: >-
4 Add PostHog log capture to track application logs. Use after implementing
5 features or reviewing PRs to ensure meaningful log events are captured with
6 structured properties. Also handles initial OTLP exporter setup if not yet
7 configured.
8metadata:
9 author: PostHog
10---
11
12# Add PostHog log capture
13
14Use this skill to add PostHog log capture for new or changed code. Use it after implementing features or reviewing PRs to ensure meaningful log events are captured with structured properties. If PostHog log export is not yet configured, this skill also covers initial OTLP exporter setup. Supports any platform or language.
15
16Supported platforms: Next.js, Node.js, Python, Go, Java, Datadog, Android, React Native, iOS, and any language via OpenTelemetry.
17
18## Instructions
19
20Follow these steps IN ORDER:
21
22STEP 1: Analyze the codebase and detect the platform.
23 - Detect the language, framework, and existing logging setup.
24 - Look for dependency files and project files (package.json, Podfile, Package.swift, requirements.txt, go.mod, pom.xml, etc.).
25 - Look for log libraries (winston, pino, logging module, logrus, log4j, serilog, os_log, Logger, etc.).
26 - Look for lockfiles (pnpm-lock.yaml, package-lock.json, yarn.lock, bun.lockb, go.sum, Podfile.lock, Package.resolved, etc.) to determine the package manager.
27 - Check for existing PostHog log export setup. If the OTLP exporter is already configured, skip to STEP 5 to add log capture for new code.
28
29STEP 2: Research log capture. (Skip if PostHog log export is already configured.)
30 2.1. Find the reference file below that matches the detected platform — it is the source of truth for OTLP exporter configuration and integration with existing logging. Read it now.
31 2.2. If no reference matches, use the "Other Languages" reference as a fallback — it covers the generic OpenTelemetry approach.
32
33STEP 3: Install dependencies. (Skip if PostHog log export is already configured.)
34 - Install the OpenTelemetry SDK and OTLP exporter packages for the detected platform.
35 - Do not manually edit dependency files — use the package manager's install command.
36
37STEP 4: Configure the OTLP exporter. (Skip if PostHog log export is already configured.)
38 - PostHog logs use the OpenTelemetry protocol. Set up an OTLP exporter pointed at PostHog's ingest endpoint.
39 - For SDK-native log support such as Android, React Native, and iOS, follow the platform reference instead of adding a separate OTLP exporter.
40 - Follow the platform-specific reference for the exact configuration.
41
42STEP 5: Integrate with existing logging.
43 - Add the PostHog log exporter alongside existing logging. Don't replace existing log handlers or outputs.
44 - Do not alter the fundamental architecture of existing files. Make additions minimal and targeted.
45 - You must read a file immediately before attempting to write it.
46
47STEP 6: Add structured properties.
48 - Ensure logs include structured key-value properties for filtering and search in PostHog.
49 - Prefer structured log formats with key-value properties over plain text messages.
50
51STEP 7: Set up environment variables.
52 - Check if the project already has PostHog environment variables configured (e.g. in `.env`, `.env.local`, or framework-specific env files). If valid values already exist, skip this step.
53 - If the PostHog project token is missing, use the PostHog MCP server's `projects-get` tool to retrieve the project's `api_token`. If multiple projects are returned, ask the user which project to use. If the MCP server is not connected or not authenticated, ask the user for their PostHog project token instead.
54 - For the PostHog host URL: check the `projects-get` MCP response for a `region` field — `US` maps to `https://us.i.posthog.com`, `EU` maps to `https://eu.i.posthog.com`. If the region is not available from the MCP response or from existing project configuration, ask the user: "Are you on PostHog US Cloud or EU Cloud?" Do not assume US Cloud.
55 - For the OpenTelemetry endpoint, use `https://us.i.posthog.com/v1` (US) or `https://eu.i.posthog.com/v1` (EU), matching the region determined above.
56 - Write these values to the appropriate env file using the framework's naming convention.
57 - Reference these environment variables in code instead of hardcoding them.
58
59## Reference files
60
61- `references/nextjs.md` - Next.js logs installation - docs
62- `references/nodejs.md` - Node.js logs installation - docs
63- `references/python.md` - Python logs installation - docs
64- `references/go.md` - Go logs installation - docs
65- `references/java.md` - Java logs installation - docs
66- `references/datadog.md` - Datadog logs installation - docs
67- `references/android.md` - Android logs installation - docs
68- `references/react-native.md` - React native logs installation - docs
69- `references/ios.md` - Ios logs installation - docs
70- `references/flutter.md` - Flutter logs installation - docs
71- `references/other.md` - Other languages logs installation - docs
72- `references/start-here.md` - Getting started with logs - docs
73- `references/search.md` - Search logs - docs
74- `references/best-practices.md` - Logging best practices - docs
75- `references/troubleshooting.md` - Logs troubleshooting - docs
76- `references/link-session-replay.md` - Link session replay - docs
77- `references/mcp.md` - Use logs over PostHog mcp - docs
78- `references/COMMANDMENTS.md` - Framework-specific rules the integration must follow
79
80Each platform reference contains specific OTLP configuration, SDK setup, and integration patterns. Find the one matching the user's stack.
81
82## Key principles
83
84- **Environment variables**: Always use environment variables for PostHog keys and OpenTelemetry endpoints. Never hardcode them.
85- **Minimal changes**: Add log export alongside existing logging. Don't replace or restructure existing logging code.
86- **OpenTelemetry**: PostHog logs use the OpenTelemetry protocol. Configure an OTLP exporter pointed at PostHog's ingest endpoint unless the platform SDK provides native log capture.
87- **SDK-native logs**: For Android, React Native, and iOS, use the SDK logger/capture APIs from the platform reference instead of adding a separate OTLP exporter.
88- **Structured logging**: Prefer structured log formats with key-value properties over plain text messages.