Srijan Guchhait

Reading Cloudflare Worker logs from the CLI

How to watch a Worker's logs from a terminal with wrangler tail, filter the stream with its flags and with jq, and what the limits are.

  • cloudflare
  • cli
  • debugging

When a deploy goes wrong I want the logs in my terminal, next to the commands I am already running. Cloudflare offers this with wrangler tail. This post covers how it works, how to narrow the stream and where it stops being useful.

I wrote this from the Cloudflare documentation. I tested the jq filters on sample JSON that I made up to match the documented shape. I did not paste live output from a real Worker, so the sample lines below are simplified.

Start a tail

Run the tail from your project folder, or name the Worker.

npx wrangler tail

After it starts, you get a live feed of console output and exceptions for each request the Worker receives. This is real-time logging. It covers invocation logs, custom logs, errors and uncaught exceptions.

Choose the output format

The --format flag takes json or pretty. Pretty is easier to read while you watch. JSON is the one to use when another tool reads the stream. Each event is a structured JSON object, and the documentation shows how to pick a field out of it with jq.

npx wrangler tail --format json | jq -r '.event.request.url'

Narrow it with flags

A busy Worker produces more lines than you can read. The tail command has flags that filter on the Cloudflare side, before the lines reach you. The documented ones include these:

  • --status with ok, error or canceled
  • --method for the HTTP method
  • --search for text in the logged messages
  • --ip for a client address
  • --header for a request header
  • --sampling-rate for a fraction of the events
npx wrangler tail --status error --method POST

Narrow it further with jq

Flags cover the common cases. For anything else, filter the JSON locally. This example keeps only events whose outcome is not ok and prints one line for each.

npx wrangler tail --format json \
  | jq -r 'select(.outcome != "ok") | "\(.event.request.method) \(.event.request.url) \(.outcome)"'

I ran that filter against two sample events, one with outcome ok and one with outcome exception. It skipped the first and printed the method, the URL and the outcome of the second.

The limits

Know these before you rely on the tail.

  • A high-traffic Worker can push the tail into sampling mode. Some messages are then dropped and a warning shows up in the log. Add filter flags to keep the volume down.
  • At most 10 clients can view the logs of one Worker at the same time. Dashboard sessions and wrangler tail calls both count.
  • With WebSocket handlers, console.log output stays hidden until the client closes the connection. Then everything prints at once.
  • The tail shows what happens now. It is not a history.

For history, persist the logs

Workers Logs stores logs in your Cloudflare account, and you query them in the dashboard. You turn it on with the observability setting in the Wrangler config.

[observability]
enabled = true
head_sampling_rate = 1

The sampling rate is the share of requests that get logged. A value of 1 logs all of them. The documentation also recommends logging in JSON form, because Workers Logs then indexes the fields and you can filter on them instead of matching text.

What about the cf CLI

I use the newer cf command line tool for Cloudflare work. I searched it for a command that streams Worker logs and found none. The search returned only unrelated results about live video inputs. For tailing, I use wrangler tail, run through npx. If cf adds a tail command later, this post is out of date and I will fix it.

My routine

During a deploy I keep one terminal on wrangler tail --status error. Silence there is useful information. For anything older than the current session, I open the saved logs in the dashboard and filter on the JSON fields.

← All posts