Log from Workers
Logging is a fundamental building block supporting application development — it can provide insights during the initial stages of development and is often times crucial to understanding an issue occurring in production.
The Workers platform captures all console.log
’s and uncaught exceptions, in addition to information about the event itself.
Add custom logs
By default a Worker will emit execution logs containing details about the Request, Response and related metadata.
In addition, you can add custom logs throughout your code. Any console.log
statements within your Worker will be visible in the dashboard or wrangler tail
. The following example demonstrates a custom console.log
within a Worker request handler.
export default { async fetch(request) { const { cf } = request; const { city, country } = cf;
console.log(`Request came from city: ${city} in country: ${country}`);
return new Response("Hello worker!", { headers: { "content-type": "text/plain" }, }); }
}
addEventListener("fetch", (event) => { event.respondWith(handleRequest(event.request));
});
/** * Respond with hello worker text * @param {Request} request */
async function handleRequest(request) { const { cf } = request; const { city, country } = cf;
console.log(`Request came from city: ${city} in country: ${country}`);
return new Response("Hello worker!", { headers: { "content-type": "text/plain" }, });
}
After you deploy the above code, view the real-time logs in the dashboard or wrangler tail
.
View logs from the dashboard
To view real-time logs associated with any deployed Worker using the Cloudflare dashboard:
- Log in to the Cloudflare dashboard and select your account.
- In Account Home, go to Workers & Pages.
- In Overview, select your Worker > and select Logs.
View logs using wrangler tail
To view real-time logs associated with any deployed Worker using Wrangler:
- Go to your Worker project directory.
- Run
npx wrangler tail
.
This will log any incoming requests to your application available in your local terminal.
The output of each wrangler tail
log is a structured JSON object:
{ "outcome": "ok", "scriptName": null, "exceptions": [], "logs": [], "eventTimestamp": 1590680082349, "event": { "request": { "url": "https://www.bytesized.xyz/", "method": "GET", "headers": {}, "cf": {} } }
}
By piping the output to tools like jq
, you can query and manipulate the requests to look for specific information:
$ npx wrangler tail | jq .event.request.url
"https://www.bytesized.xyz/""https://www.bytesized.xyz/component---src-pages-index-js-a77e385e3bde5b78dbf6.js""https://www.bytesized.xyz/page-data/app-data.json"
You can customize how wrangler tail
works to fit your needs. Refer to the wrangler tail
documentation for available configuration options.
Limits
Note that:
- Workers logs are not stored. You can start and stop the stream at any time to view them, but they do not persist.
- If your Worker has a high volume of traffic, the real-time logs might enter sampling mode. This will cause some of your messages to be dropped and a warning to appear in your logs.
- Logs from any Durable Objects your Worker is using will show up in the dashboard.
- A maximum of 10 clients can view a Worker’s logs at one time. This can be a combination of either dashboard sessions or
wrangler tail
calls.
Persist logs
Logs can be persisted in two ways:
Workers Logpush allows you to send Workers Trace Event Logs to a supported destination. Worker’s Trace Events Logpush includes metadata about requests and responses, unstructured console.log()
messages and any uncaught exceptions.
Refer to the Workers Logpush documentation to learn how to create and configure Logpush jobs.
Tail Workers allow you to automatically invoke Tail Workers after the invocation of a producer Worker (the Worker the Tail Worker will track) that contains the application logic. It captures events after the producer has finished executing. You can filter, change the format of the data and send events to any HTTP endpoint.
Refer to the Tail Workers documentation to learn how to create and configure Tail Workers.
Related resources
- Errors and exceptions - Review common Workers errors.
- Local development and testing - Develop and test you Workers locally.
- Logpush - Learn how to push Workers Trace Event Logs to supported destinations.
- Tail Workers - Learn how to attach Tail Workers to transform your logs and send them to HTTP endpoints.
- Source maps and stack traces - Learn how to enable source maps and generate stack traces for Workers.