Back to Blog
Lesson 34 of the Cloudflare: Cloudflare for Developers: DNS to CDN course
Cloud NativeAugust 12, 20264 min read

Observability and Logging: Monitoring Cloudflare Workers Health

Master observability and logging to monitor your application's health. Learn to view real-time Worker logs and analyze traffic patterns in the Cloudflare dashboard.

CloudflareWorkersObservabilityLoggingMonitoringHealth
A security guard sitting inside a glass booth in Istanbul, Türkiye.

Previously in this course, we explored advanced routing patterns to manage complex traffic flows. In this lesson, we add Observability and Logging to your toolkit, allowing you to see exactly what your Workers are doing in production and how to identify when things go wrong.

Observability from First Principles

In a serverless environment like Cloudflare Workers, you don't have access to the underlying server hardware or OS-level logs. Instead, Observability—the ability to measure the internal state of your system by examining its outputs—relies on two pillars: Logging (capturing events) and Analytics (aggregating metrics).

While we’ve used console.log for local debugging in debugging workers with wrangler, production observability requires a different mindset. You aren't just looking for a quick fix; you are monitoring the Health of your application to ensure it meets performance SLAs and user expectations.

Accessing Worker Logs

When your Worker is deployed to the edge, logs aren't written to a local file. They are streamed through Cloudflare’s infrastructure. To view these in real-time for a live deployment, use the Wrangler CLI:

Bash
npx wrangler tail <worker-name>

The tail command initiates a connection to the edge network and streams logs as they happen. This is invaluable for identifying issues that only appear under real user traffic.

Identifying Common Error Patterns

When monitoring your logs, watch for these common patterns:

  1. Uncaught Exceptions: Errors that crash the execution context.
  2. 500 Status Codes: Often indicate failures in your D1 database or R2 storage integration.
  3. Timeout Errors: Occur if your code takes longer than the allowed CPU time (often due to unoptimized loops or slow network fetches).

Analyzing Traffic in the Dashboard

While logs provide the "who, what, and where" of specific requests, the Cloudflare Dashboard provides the "big picture" of your application's Monitoring health.

  1. Log into the Cloudflare Dashboard and select your domain.
  2. Navigate to Workers & Pages in the sidebar.
  3. Click on your specific Worker.
  4. Select the Analytics tab.

Here you will see graphs for:

  • Requests: Total volume of traffic hitting your Worker.
  • Duration: The CPU time consumed by your script.
  • Errors: A breakdown of HTTP 5xx errors versus successful 2xx responses.

Hands-on Exercise: Logging and Auditing

Let’s update our "Edge Interceptor" project to ensure we are logging critical path data.

  1. Edit your worker code to include a structured log entry:
    JAVASCRIPT
    export default {
      async fetch(request, env, ctx) {
        const start = Date.now();
        const response = await fetch(request);
        const duration = Date.now() - start;
    
        console.log(CE9178">`Path: ${new URL(request.url).pathname} | Duration: ${duration}ms | Status: ${response.status}`);
        
        return response;
      }
    }
  2. Deploy the updated worker using npx wrangler deploy.
  3. Run npx wrangler tail <name> in your terminal.
  4. Trigger a request to your site and observe the output in your terminal.

Common Pitfalls

  • Log Verbosity: Don't log sensitive information like API keys or user passwords. It’s a major security risk.
  • Performance Impact: Excessive logging can slightly increase the CPU time of your worker, though Cloudflare is highly optimized for this. Keep it to necessary state changes and errors.
  • Ignoring the Analytics Tab: Many developers rely solely on logs and miss the broader trends (like a slow spike in latency) that are only visible in the aggregated Analytics charts.

FAQ

Q: Do I need to pay for logging? A: Standard logs are available as part of your Worker usage. Advanced log forwarding to external services (like Datadog or Splunk) may require an Enterprise plan or specific configuration.

Q: Why don't I see logs in the dashboard? A: Ensure your Worker is actually receiving traffic. If you are using a custom domain, check your Workers Routes configuration.

Q: How long are logs kept? A: wrangler tail provides a live stream. For historical log analysis, consider enabling Logpush.

Recap

Observability is your best defense against production outages. By combining wrangler tail for immediate debugging and the Cloudflare Dashboard for long-term health metrics, you gain full visibility into your serverless infrastructure. Remember to monitor your error patterns regularly to catch regressions early.

Up next: Error Handling and Alerts — we will move from observing issues to proactively reacting to them with automated alerts.

Similar Posts