Better Stack Cloudflare Workers logging
Start logging in 5 minutes
Collect logs from your Cloudflare Workers.
Create a new source
Create a new JavaScript • Node.js source in Better Stack Logs.
Already created a Cloudflare Logpush or Cloudflare HTTP requests source?
Removing it and creating a new source instead will provide you much better experience with much more suitable default configuration.
Set up Cloudflare Worker
Either open your existing project with Wrangler CLI if you already have it set up, or initialize new "Hello World" Worker:
Install Better Stack logging library
Install Better Stack logging library for edge JS runtime @logtail/edge:
Create and use instance of logger
Import and instantiate Logtail class, add the execution context from the request, and use it for your application logs:
Deploy your worker
Deploy your worker using Wrangler CLI
Finally, open the worker's URL in your browser to run it.
You should see your logs in Better Stack → Live tail.
Additional information
Instantiate the
Logtailclass outside of thefetchhandler, so all requests share one logger. Each request sends its own logs right away and is kept alive only until they are sent.Providing the
ExecutionContextinstance (usually calledctx) will ensure all logs will be sent to Better Stack. To ensure best results, always use the returned logger instance fromLogtail::withExecutionContext()method.Want to learn more about log levels and middleware?
Continue to the Advanced usage of Logtail JavaScript client.
Logging from environments without ExecutionContext?
Some environments — like Cloudflare Durable Objects — don’t expose the ExecutionContext to your log handler. When this happens, the logger can't schedule log syncing automatically, and you might see a warning like this:
ExecutionContext hasn't been passed to the log method, which means syncing logs cannot be guaranteed. To ensure your logs will reach Better Stack, use logger.withExecutionContext(ctx) to log in your handler function. See https://betterstack.com/docs/logs/js-edge-execution-context/ for details.
To safely log in these environments:
Need help?
Please let us know at hello@betterstack.com.
We're happy to help! 🙏