Logging and monitoring for non 200 responses in Worker

I don’t want to use Sentry for this current project I’m building. It’s a small private app with very limited memberships.

I want to use Workers Trace Events (I think) and push my logs to an R2 bucket. I see in the docs how to accomplish this but I’m confused as to the roll of a Tail worker in the process?

Or is the Tail Worker a completely different way to accomplish my goal.

Here’s what I’m thinking…

I want to send custom console.logs only when my Workers hits a non-200 response. I can send the internal function/handler that was being called and the endpoint that failed.

Would this be better accomplished in a Tail Worker or through Worker Trace Events?

Thanks for any help on getting some simple logging setup.

Hello @danboyle8637

To log non-200 responses in Cloudflare Workers and send them to an R2 bucket, you can use Worker Trace Events. Here’s a simplified step-by-step guide:

  • In your Worker script, add logic to check the response status.
  • If the status is not 200, use console.log() to output the desired information.
  • Enable Trace Events in your Worker’s configuration.
  • Set up a Worker to listen for these Trace Events and write them to an R2 bucket.

The Tail Worker is a separate tool for real-time debugging and is not necessary for this logging setup. It’s more for live troubleshooting rather than storing logs.

Thanks so much for this help. That helps with Trace Workers being more for read time debugging.

One more quick question… on your step four… “Setup a Worker to listen to these Trace Events…”

This might be a dumb question…

But if I’m console.log in my “production” Worker… how does my other worker listen to these events if it’s not a Tail Worker?

This is where my gap in understand is.

Thanks for any further clarification. It is helping a lot!

Nevermind… I found the docs to set this up.

Thanks for pointing me into the right direction!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.