Vercel3 min read

Vercel Log Drains: Centralize Production Errors the Right Way

Author:Viraj Rakholiya

What is a Log Drain?

A log drain is a mechanism that automatically forwards logs generated by a temporary or serverless environment (like Vercel functions or Heroku dynos) to a centralized, persistent external logging service (like Datadog, Axiom, or Better Stack). Because serverless environments are ephemeral and do not store logs permanently, a log drain is required to retain, search, and analyze application behavior and errors over time.


Vercel log drains forward your Next.js server, edge, and build logs to an external tool in real-time. Without a log drain, a console.error in a serverless function is essentially useless in production—it's ephemeral, heavily truncated, unstructured, and completely gone shortly after the execution ends. You cannot search through historical logs on Vercel's default tier effectively.

If you are serious about production reliability, setting up a log drain is mandatory.

What You Should Be Draining

You don't want to drain everything blindly, or you'll pay massive ingestion fees. Focus on high-signal data:

  1. API Routes and Server Components: Capture console.error and console.warn from these environments. Always ensure a requestId is attached.
  2. Edge Middleware Failures: Edge functions run on a limited runtime (V8 isolates). You cannot use standard Node.js logging SDKs. You must use fetch-based reporting to send logs out, and the drain captures these.
  3. Build Failures: Log drains can capture failures in generateStaticParams when upstream APIs are down during deployment, helping you debug build timeouts.

Add Context Before Logging

A log that simply says "Payment failed" is noise. You need structured context to debug it. Add the user ID and route before logging.

// Bad
console.error("Payment failed", err.message);

// Good
console.error(JSON.stringify({
  level: 'error', 
  route: '/api/checkout',
  userId: session.user.id, 
  reqId: request.headers.get('x-request-id'), 
  message: err.message
}));

Structured JSON always beats plain text. Tools like Loki, Datadog, or Better Stack can parse JSON instantly, allowing you to filter by userId or route reliably.

Don't Rely on Stdout Alone

Standard output (stdout) has no session concept, no breadcrumbs, and no intelligent grouping. It's just a raw stream of text.

You must pair log drains (which tell you what happened around the error) with error tracking SDKs (which tell you the exact stack trace and code location). They should be linked by the reqId.

This is where Relia shines. When configured with your Next.js app, Relia links the context from your log drain directly to a diagnosed code fix. The debugging process doesn't end with a search query in a logging tool; it ends with an automatically generated Pull Request fixing the bug.

Security Warning: Always redact authentication headers, session tokens, and PII before shipping logs out. Searchable logs leak data fast if you log full request objects blindly.

Read more on how to manage this data without getting overwhelmed in our guide: /blog/too-many-production-alerts-fix-noise.

FAQ

Do log drains slow Vercel functions?

Minimal if async. Avoid heavy SDKs in edge — keep middleware under ~50ms.

How long to retain Vercel logs?

7-30 days pre-PMF. Longer only for payments/compliance.

Logs vs error tracking?

Logs tell what happened, errors tell where in code. You need both linked by reqId.

[ MORE ARTICLES ]

Read Next

View all →