AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a structured approach to instrumenting applications for log collection via PostHog. It guides the user through detecting their current logging stack, installing necessary OpenTelemetry SDKs, configuring OTLP exporters, and ensuring logs are enriched with structured properties for better observability. It also supports forwarding existing Datadog logs to PostHog, ensuring that developers can centralize their system logs alongside product analytics and session replays.
What it does
- Detects the application's language, framework, and existing logging infrastructure.
- Configures OpenTelemetry OTLP exporters to send logs to PostHog.
- Integrates log capture alongside existing logging handlers without replacing them.
- Promotes the use of structured, key-value based logging for better searchability.
- Sets up environment variables for secure API key and endpoint management.
- Provides a path to forward existing Datadog log streams to PostHog.
When to use it
- When you need to start capturing application logs in PostHog for the first time.
- After implementing new features or reviewing pull requests to ensure relevant events are logged.
- When you want to transition from plain text logging to structured, queryable logs.
- When you need to integrate PostHog logging into an existing Datadog-based setup.
Advantages
- Centralizes system logs with product analytics and session replays.
- Uses industry-standard OpenTelemetry protocols for broad compatibility.
- Maintains existing logging architecture by adding exporters as a sidecar or secondary output.
- Encourages best practices like wide-event logging for easier debugging.
Usage examples
- “Set up PostHog log capture for my Node.js application.”
- The agent will analyze the project, install the required OpenTelemetry packages, configure the OTLP exporter, and guide you through setting the necessary environment variables.
- “I am already using Datadog for logs, how do I send them to PostHog?”
- The agent will provide instructions to update your Datadog agent configuration to point to the PostHog Datadog-compatible ingest endpoint.
Install
Download the skill as a ZIP:
https://mp-staging.autogineer.com/agent-skills/posthog/omnibus-instrument-logs/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip omnibus-instrument-logs.zip -d .agents/skills
Documentation by PostHog
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, 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 log libraries (winston, pino, logging module, logrus, log4j, serilog, etc.).
Look for lockfiles 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.
Always install packages as a background task. Don't await completion; proceed with other work immediately.
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.
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.
If an env-file-tools MCP server is connected, use check_env_keys then set_env_values to configure the PostHog API key, host, and OpenTelemetry endpoint.
Reference these environment variables in code instead of hardcoding them.
Reference files
references/nextjs.md- Next.js logs installation - docsreferences/nodejs.md- Node.js logs installation - docsreferences/python.md- Python logs installation - docsreferences/go.md- Go logs installation - docsreferences/java.md- Java logs installation - docsreferences/datadog.md- Datadog logs installation - docsreferences/other.md- Other languages logs installation - docsreferences/start-here.md- Getting started with logs - docsreferences/search.md- Search logs - docsreferences/best-practices.md- Logging best practices - docsreferences/troubleshooting.md- Logs troubleshooting - docsreferences/link-session-replay.md- Link session replay - docsreferences/debug-logs-mcp.md- Debug logs with mcp - docs
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.
Structured logging: Prefer structured log formats with key-value properties over plain text messages.
Files
- ATTRIBUTION.mdAdded by AutoGineer939 B
- LICENSELicence file added by AutoGineer1.0 KiB
- SKILL.md4.4 KiB
- references/best-practices.md19.6 KiB
- references/datadog.md3.0 KiB
- references/debug-logs-mcp.md2.2 KiB
- references/go.md4.0 KiB
- references/java.md3.9 KiB
- references/link-session-replay.md4.9 KiB
- references/nextjs.md5.5 KiB
- references/nodejs.md3.6 KiB
- references/other.md2.7 KiB
- references/python.md3.6 KiB
- references/search.md2.5 KiB
- references/start-here.md2.4 KiB
- references/troubleshooting.md3.5 KiB
references/best-practices.md
Logging best practices - Docs
Most logging is bad. Not because people don't log enough. They log too much of the wrong things and too little of the right things. The result is millions of lines that are expensive to store and useless to query.
This guide covers what actually makes logs useful in production systems. PostHog ingests logs via OpenTelemetry (OTLP), so the patterns here are built around OTel's structured logging model: resource attributes, log attributes, and trace context.
This guide covers:
Centralize your logs
Logging requests, not code
Structured logging
Cardinality and dimensionality
Business context and OTel attributes
Building wide events
Log levels
Sampling
Trace and session context
Schema evolution
What not to log
Checklist
Centralize your logs
Centralizing your logs makes it possible to search across all your services in one place.
With PostHog, your logs live alongside your Product Analytics, Session Replays, and Feature Flags, so you can go from a log line to a user's session to the flag variant they were on without switching tools.
If you're already using posthog.capture(), you might wonder how logs differ from events. The key distinction is:
Events track what the user did (e.g. clicks, signups, purchases, feature usage)
Logs track what the system did (e.g. API requests, errors, retries, timeouts, configuration failures)
If you've been capturing things like database_connection_failed or stripe_api_timeout as PostHog events, those belong in logs instead.
Log what happened to requests, not what your code is doing
This is the single most important shift you can make.
PostHog AI
logger.info("Entering payment processing")
logger.info("Validating card details")
logger.info("Calling Stripe API")
logger.info("Stripe API returned")
logger.info("Updating database")
logger.info("Payment complete")Six log lines, none of them useful in production at INFO level. They tell you what the code does (you already know that, you wrote it), not what happened to a specific request.
Step-level logs aren't universally wrong. They're valuable at DEBUG level for diagnosing race conditions, understanding ordering in concurrent systems, or tracing through complex state machines. The point is: don't make them your default.
Your INFO-level logs should be wide events. Your DEBUG-level logs can be as granular as you need, turned on selectively when you're actively investigating.
Instead, emit one rich log per request per service:
JSON
PostHog AI
{
"event": "payment.completed",
"duration_ms": 342,
"posthog_distinct_id": "user_abc123",
"order_id": "ord_789",
"amount_cents": 4999,
"currency": "USD",
"payment_method": "card",
"provider": "stripe",
"provider_latency_ms": 287,
"retry_count": 0,
"feature_flags": ["new_checkout_flow"],
"subscription_tier": "pro"
}One line. Everything you need to debug, alert on, or analyze, all in one place. This is a wide event (sometimes called a canonical log line), and it's the foundation of useful logging.
This pattern works cleanly for request-response services. For long-running processes, event-driven architectures, or workflows that span multiple services over minutes or hours, a pure single-event approach is less practical.
In those cases, use a hybrid: emit a wide event at each meaningful stage boundary (job started, stage completed, job finished), with each event carrying the full accumulated context up to that point. You still get the benefits of wide events without relying on a single emit that might never fire.
Use structured logging
Plain text logs are optimized for writing, not querying. Structured logs (JSON key-value pairs) are the opposite. They're queryable, filterable, and machine-readable.
Bad:
PostHog AI
Payment failed for user abc123 - Stripe error: card_declined (amount: $49.99)Good:
JSON
PostHog AI
{
"event": "payment.failed",
"posthog_distinct_id": "user_abc123",
"error_type": "card_declined",
"provider": "stripe",
"amount_cents": 4999
}The structured version lets you query "all card_declined errors for pro-tier users in the last hour" without regex. The plain text version requires you to hope your string parsing doesn't break on edge cases.
Structured logs in PostHog
PostHog's log search works across all fields in structured logs, so the more context you include, the more useful your search and filtering becomes. Every key-value pair is a field you can filter on.
Think in cardinality and dimensionality
Two concepts that separate useful logs from noise. You want both high cardinality and high dimensionality. One wide event with 50 fields tells you more than 50 separate log lines with three fields each.
What is cardinality?
Cardinality is the number of unique values a field has. posthog_distinct_id has high cardinality (millions of unique values). log_level has low cardinality (5 values). High-cardinality fields are what enable you to debug specific requests and users.
Some teams avoid high-cardinality fields because older logging tools can't handle them efficiently. Modern columnar databases (like ClickHouse, which PostHog uses under the hood) handle high cardinality just fine. Don't let outdated tooling concerns stop you from logging the fields that matter.
What is dimensionality?
Dimensionality is the number of fields per log event. A log with three fields (timestamp, level, message) has low dimensionality. A wide event with 30+ fields has high dimensionality.
High dimensionality is what makes wide events powerful. Instead of scattering context across dozens of log lines, you pack it all into one event. This means every query can filter, group, and correlate across all those fields simultaneously.
Include business context
Technical context (status codes, latency, error types) is necessary but insufficient. Add the business context that turns debugging into understanding:
Who: user ID, account type, subscription tier, organization
What: order ID, cart contents, item count, Feature Flags
Where: service name, deployment version, region
How: payment method, auth provider, API version
How much: amount, quantity, retry count
This lets you move from "500 errors spiked" to "500 errors spiked for enterprise users using the new checkout flow with coupon codes."
In OpenTelemetry, this context splits into two layers.
Resource attributes are set once when your service starts. They describe the service itself:
service.name,deployment.environment,service.version,cloud.region. Every log from that process automatically includes them.Log attributes are set per event. They describe what happened in that specific request:
posthog_distinct_id,order_id,payment_method,duration_ms.
Correlate with Product Analytics
If you're using PostHog for Product Analytics, the business context in your logs can match the properties on your events. This means you can go from a log search result straight to seeing how that user behaves in your product, and vice versa.
Build events throughout the request lifecycle
Don't emit 15 separate logs as a request moves through your code. Instead, accumulate context onto a single event and emit it once when the request completes.
The implementation details vary by language, but the pattern is always the same. These examples use the OpenTelemetry APIs from the installation guide:
Python
Python's standard logging module with the extra parameter. The OpenTelemetry SDK (configured in the installation guide) picks up these attributes automatically.
Python
PostHog AI
import logging
logger = logging.getLogger(__name__)
def handle_checkout(request):
attrs = {
"event": "checkout",
"posthog_distinct_id": request.user.id,
"subscription_tier": request.user.tier,
}
cart = get_cart(request.user)
attrs.update({
"item_count": len(cart.items),
"cart_total_cents": cart.total_cents,
})
try:
payment = process_payment(cart)
attrs.update({
"payment_method": payment.method,
"provider": payment.provider,
"provider_latency_ms": payment.latency_ms,
"status": "success",
})
logger.info("checkout completed", extra=attrs)
except PaymentError as e:
attrs.update({"status": "failed", "error_type": e.code})
logger.error("checkout completed", extra=attrs)
raiseNode.js
The OpenTelemetry Logs API with logger.emit(). Attributes are passed as a dictionary on each log record. See the installation guide for SDK setup.
JavaScript
PostHog AI
import { logs } from "@opentelemetry/api-logs";
const logger = logs.getLogger("my-app");
function handleCheckout(req, res) {
const attrs = {
event: "checkout",
posthog_distinct_id: req.user.id,
subscription_tier: req.user.tier,
};
const cart = getCart(req.user);
Object.assign(attrs, { item_count: cart.items.length, cart_total_cents: cart.totalCents });
try {
const payment = processPayment(cart);
Object.assign(attrs, {
payment_method: payment.method,
provider: payment.provider,
provider_latency_ms: payment.latencyMs,
status: "success",
});
logger.emit({ severityText: "INFO", body: "checkout completed", attributes: attrs });
} catch (e) {
Object.assign(attrs, { status: "failed", error_type: e.code });
logger.emit({ severityText: "ERROR", body: "checkout completed", attributes: attrs });
throw e;
}
}Go
Go's standard slog package, bridged to OpenTelemetry via otelslog (configured in the installation guide). Each slog.With() call returns a new logger with additional attributes.
Go
PostHog AI
func HandleCheckout(w http.ResponseWriter, r *http.Request) {
log := slog.With(
"event", "checkout",
"posthog_distinct_id", r.Context().Value("posthog_distinct_id"),
)
cart, _ := getCart(r.Context())
log = log.With(
"item_count", len(cart.Items),
"cart_total_cents", cart.TotalCents,
)
payment, err := processPayment(r.Context(), cart)
if err != nil {
log.With(
"status", "failed",
"error_type", err.Code,
).ErrorContext(r.Context(), "checkout completed")
return
}
log.With(
"payment_method", payment.Method,
"provider", payment.Provider,
"provider_latency_ms", payment.LatencyMs,
"status", "success",
).InfoContext(r.Context(), "checkout completed")
}One log line at the end, containing everything. Each step accumulates attributes, and the final emit carries them all.
Watch out for context bloat
Only bind scalar values (strings, numbers, booleans) to your log context. If you accidentally attach a full API response, a large query result, or a serialized object, you'll hit payload size limits or memory issues. Log the fields you need for debugging, not entire data structures.
What if the process crashes?
If your application crashes before reaching the end of a request (segfault, OOM, power failure), the accumulated context never gets emitted. Make sure you have a global exception handler or finally block that flushes whatever context has been collected. For long-running background jobs, consider emitting a "started" log at the beginning and "checkpoint" logs at key milestones, so a crash doesn't mean total data loss.
Use log levels correctly
Log levels exist to control signal-to-noise ratio. Use them consistently:
| Level | Use for | Example |
|---|---|---|
| ERROR | Something failed and needs attention | Payment processing failed, database connection lost |
| WARN | Something unexpected that didn't cause failure | Retry succeeded on third attempt, deprecated API version used |
| INFO | Normal operations worth recording | Request completed, user signed up, deployment finished |
| DEBUG | Detailed info for active debugging | Cache hit/miss ratios, query plans, intermediate state |
Two rules of thumb:
The noisy ERROR trap
If you're logging at
ERROR, someone should eventually act on it. If no one ever looks at an error log, it's not an error. It's noise.DEBUGlogs should be off in production by default. Turn them on for specific services or requests when actively investigating.
Sample strategically
At scale, logging everything is expensive and unnecessary. Use tail sampling. Make sampling decisions after a request completes, based on the outcome:
Keep 100% of errors and exceptions
Keep 100% of requests that exceeded your p99 latency threshold
Keep 100% of requests from important accounts or flagged sessions
This gives you full visibility into problems while keeping costs manageable. You lose nothing useful. The sampled successful requests are statistically representative.
Tail sampling is the ideal, but it's genuinely hard to implement well. Your logging pipeline needs to buffer data in memory until a request completes, and in distributed systems you need consistent sampling decisions across services for the same trace. This is typically handled by an OpenTelemetry Collector with a tail sampling processor, but configuring it correctly takes real effort.
If your infrastructure doesn't support tail sampling yet, head sampling (randomly keeping a fixed percentage of requests up front) is a pragmatic starting point. It's less precise (you'll drop some errors and keep some boring requests), but it's better than logging everything or nothing. You can always move to tail sampling later.
How much does log storage cost in PostHog?
PostHog Logs is billed by GB ingested per month with volume-based pricing. Use the calculator on the pricing page for a full breakdown.
Add trace and session context
Isolated logs are hard to correlate. Adding trace IDs and session IDs connects individual log events to the broader request journey.
Pick a library that supports structured output, async/buffered writes, and low per-call overhead. If you're using the OpenTelemetry SDK, the OTel log bridge adds minimal overhead on top of your chosen library, so the library itself is the bottleneck, not the export pipeline.
When in doubt, benchmark your logging path under realistic load before shipping to production.
Since PostHog uses OpenTelemetry, trace context propagation is automatic. Your logs are already correlated by trace ID if you have the OTel SDK configured. If you're also using PostHog for Product Analytics or Session Replay, you can go further and link your logs to Session Replays, giving you the user's full experience alongside your backend logs.
Link logs to Session Replays
By adding a PostHog session ID and distinct ID to your log attributes, you can jump directly from a log line to the user's Session Replay. See the Session Replay linking guide to set this up.
Treat your log schema like an API contract
Once you adopt wide events, your field names and value formats become dependencies. Dashboards, alerts, and saved searches all break silently when someone renames error_type to err_code or changes duration_ms from an integer to a string. Treat changes to your log schema the same way you'd treat changes to a public API: communicate them, deprecate before removing, and avoid breaking existing consumers.
What not to log
Some things should never appear in your logs:
Secrets: API keys, passwords, tokens, credit card numbers. If you log these by accident, you now have a security incident and a logging problem.
Request and response bodies: Logging full payloads is one of the fastest ways to blow up storage costs and accidentally capture PII, auth tokens, or sensitive user data. Log the metadata (status code, content length, duration), not the body.
Personal data you don't need: Full email addresses, IP addresses, or other PII beyond what's required for debugging. If you need to correlate logs to a user but can't store raw identifiers, hash or tokenize them. Check your GDPR, HIPAA, or other compliance requirements, as even fields like
posthog_distinct_idoremailmay need masking depending on your jurisdiction.High-frequency health checks: Load balancer pings and liveness probes generate massive volume with zero debugging value. Exclude them.
Unnecessary duplication: If a downstream service logs the same event, you don't always need to log it again upstream. That said, when you're debugging a production incident at 2am, having key context from downstream calls in your own service's logs can save you from correlating across multiple systems under pressure. The rule of thumb: don't log a play-by-play of every call you make, but do include the outcome and any data you'd need to debug without switching to another service's logs.
Logging checklist
Use this to audit your existing logging or as a starting point for a new service.
Structural requirements
Logs are structured JSON key-value pairs, not plain text strings
Each request emits one wide event at the end, not a trail of step-by-step messages
Only scalar values (strings, numbers, booleans) are logged. No raw objects, large arrays, or full API response bodies
Context is accumulated throughout the request lifecycle (e.g.,
cart_totaladded once calculated,payment_idadded later)
Business and trace context
The "Who":
posthog_distinct_id,org_id,account_tier, or equivalentThe "What":
order_id,transaction_id,feature_flag_variants, or equivalentThe "Where":
service.name,service.version,deployment.environmentset as OTel resource attributesTrace IDs: OpenTelemetry
trace_idis attached so you can jump from logs to tracesSession IDs: PostHog
session_idis included to enable Session Replay linking
Levels and sampling
Log levels are correct: INFO for request completion, WARN for retries or non-breaking issues, ERROR only if someone needs to act
Health checks (
/healthz) and load balancer pings are excluded or sampled downA sampling strategy is in place (or planned) for high-traffic services
A
try/finallyor global error handler flushes log context if the process dies mid-request
Security and compliance
No secrets: API keys, Bearer tokens, and passwords are scrubbed
PII is masked: emails, physical addresses, and credit card numbers are hashed or removed per GDPR/HIPAA requirements
Request/response bodies are not logged (to avoid capturing sensitive user data)
Field names and value types are treated as a stable schema (changes are communicated)
Link logs to Session Replays for full user context
Community questions
Ask a question
Was this page useful?
HelpfulCould be better
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Needs: Needs an MCP server, Needs an API key, Needs Python
Security
What our scanner noticed· Low risk
- Instructions that use the network
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- PostHog
- Publisher
- PostHog (Official)
- Source
- PostHog/skills · skills/posthog/logs/skills/all · commit a5d30e7
- Licence
- MIT License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
- Also distributed in
- PostHog/skills
AutoGineer is not affiliated with or endorsed by PostHog.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download omnibus-instrument-logs.zip