10/1/2025
Cloudflare Tail Worker

With Cloudflare Tail Worker you direct logging to one place - everything is in the ecosystem and in real time. Until recently I only used external tools like Sentry, LogRocket or Loki with Grafana, but setting them up requires knowing the processing and takes time.
Very often, when you use SaaS (Software as a Service) services you don't have access to the logs - or if you do, the logs are heavily truncated. Then you configure your development environment as close as possible to production and try to simulate the errors or create a process to which you send the errors and from there amplify them to an external tool.
Every Worker in Cloudflare has its own logs, but you don't have much control over them. And when you need to
monitor more than one Worker, relative to the time when some event occurred in all these Workers,
things get complicated. That's why you create, separately, a normal Worker, but the async fetch(request, env, ctx)
function you rename to async tail(events, env, ctx)
and you already have a defined Tail Worker.
// src/index.ts
export default {
async tail(events: TraceItem[], _env: unknown, _ctx: unknown) {
if (!events || events.length === 0) {
return;
}
console.log(`[TAIL] Received ${events.length} events`);
for (const trace of events) {
try {
handleTraceItem(trace); // Your function for processing events
} catch (error) {
console.error(`[TAIL_ERROR] Error processing event: ${error instanceof Error ? error.message : String(error)}`);
}
}
console.log(`[TAIL] Completed processing ${events.length} events`);
}
} satisfies ExportedHandler;
You name your Worker - for example: tail-for-me-all-the-app-events
. You add to wrangler.jsonc
the line
{
// ...
"observability": {
"enabled": true
}
}
and upload it to Cloudflare. Done - you have an active Tail Worker.
From here on, in each Producer Worker whose logs you want to monitor, you add to its wrangler.jsonc
:
{
// ...
"tail_consumers": [
{
"service": "tail-for-me-all-the-app-events"
}
]
}
Currently there's no limit on the number of Producer Workers that can send their logs to a given Tail Worker. You should know that above a certain CPU usage time, Cloudflare will charge you additionally.
I mainly follow 2 rules
- If two or more Producer Workers share at least one database - they use a common Tail Worker.
- A Producer Worker uses more than one Tail Worker only if it has defined access rules.
handleTraceItem
function.