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.
How do I install this agent skill?
npx skills add https://github.com/posthog/ai-plugin --skill instrument-logsIs this agent skill safe to install?
- Gen Agent Trust Hubpass
The 'instrument-logs' skill is a legitimate developer utility for integrating PostHog logging and OpenTelemetry into various applications. It follows secure development practices by utilizing environment variables for secrets, performing targeted code modifications, and relying on standard package managers for dependencies. No malicious patterns or security risks were detected.
- Socketpass
No alerts
- Snykpass
Risk: LOW · No issues
What does this agent skill do?
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.
- If the project keeps more than one lockfile for the same manifest, such as a second Gemfile lock for another bundle, update each one.
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 project token that the tool or CLI which started this setup already gave you (for example in its output or instructions).
- If it gave you none, call the PostHog MCP server's
projects-gettool to list the projects. If more than one comes back, ask the user which project to use. Then callproject-getwith that project'sidand read itsapi_token. Before you write the token, tell the user the project'snameand confirm it is the one this app should send to. If the MCP server is not connected or not authenticated, ask the user for their PostHog project token instead. - For the PostHog host URL: use the host that the tool or CLI which started this setup already gave you. If it gave you none, use the
api_hostfield of theproject-getresponse. If you still have no host from the calling tool, the MCP response, or 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 the same host as above with the path
/v1:https://us.i.posthog.com/v1(US) orhttps://eu.i.posthog.com/v1(EU). - Write these values to the appropriate env file using the framework's naming convention.
- Reference these environment variables in code instead of hardcoding them.
- List the key names in
.env.example, or the env template the project already uses, with placeholder values, so the next developer knows what to set. Create.env.exampleif the project has none. Never put real values in it. - Read the keys as optional: a missing key must never break the build or boot. Follow the missing-configuration rule in
references/COMMANDMENTS.md.
Reference files
references/nextjs.md- Next.js logs installationreferences/nodejs.md- Node.js logs installationreferences/python.md- Python logs installationreferences/go.md- Go logs installationreferences/java.md- Java logs installationreferences/datadog.md- Datadog logs installationreferences/android.md- Android logs installationreferences/react-native.md- React native logs installationreferences/ios.md- Ios logs installationreferences/flutter.md- Flutter logs installationreferences/other.md- Other languages logs installationreferences/start-here.md- Getting started with logsreferences/search.md- Search logsreferences/best-practices.md- Logging best practicesreferences/troubleshooting.md- Logs troubleshootingreferences/link-session-replay.md- Link session replayreferences/mcp.md- Use logs over PostHog mcpreferences/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.
How can the creator link this skill?
Add the canonical catalog link to the repository README so users can inspect current installs and available audits. The publishing guide covers the complete discovery path.
<a href="https://skillzs.dev/skills/posthog/ai-plugin/instrument-logs">View instrument-logs on skillZs</a>