How to log webhook data in a Cloudflare worker

I’m writing my first Cloudflare worker. It accepts a 3rd party webhook. I’ve done wrangler publish and I’m trying to console.log the webhook headers and payload so I can see what’s in it. I’ve searched the docs and forum but haven’t found an answer.

If there isn’t a way to see the console.log output from a 3rd party webhook (which can’t be received on localhost), is there another suggested workflow for debugging this kind of situation?

For runtime debugging, I use an external logging solution. I use graphana and Loki on a VPS. Also check out logflare for an out of the box solution.

1 Like

Thanks for the reply. Logflare’s website says $90/month. I don’t want to send logs for my whole website into a 3rd party service but just want to temporarily inspect what is passing through one function during development.

Is paying extra money or deploying another VPS the only way to debug Cloudflare workers when they need to receive 3rd party webhooks? :confused:

You can return debug information in the headers, it’s mentioned in the blog post here: https://blog.cloudflare.com/debugging-serverless-apps/

1 Like

Thanks, that’s a step closer, but when it’s a 3rd party webhook, I don’t have access to the response from the worker. Only the 3rd party webhook will hit this function.

I’ll try setting it up to email me the debugging information for now, but it seems really convoluted.

You can of course just save the debug to the KV database and read it from there. But it might add delays up to 10-20 sec when you read it from API due to it being distributed.

1 Like

Thanks, I might look into it later. I’m think it will probably be faster for me to rewrite it as a small Express app and deploy it on my own server at this point. I have another endpoint to write that is going to cause a similar situation, except there is more than one kind of payload, and I’ll need to inspect the requests as they come in or it’s going to be painful.

Sounds like the best option.
Workers are unfortunately not suitable for quite a lot of things, yet…

1 Like

I totally get it if you want to roll your own… but just for the record you can send Logflare any arbitrary log event and json metadata just make a POST request from your worker with whatever data you want.

Also, we’re currently free, and will have a generous free plan (like ~5,000,000 events a month). That pricing info is there to set the expectation that we will be charging.

Thanks, I only want to log one simple function, not install a 3rd party app on my entire site.

With Firebase Cloud Functions, whatever you console.log gets sent into a logs page, and it’s really easy to use. I hope Cloudflare adds something like that, because it has useful features that Firebase doesn’t have (like being able to use separate repos for each function, routing to the function from anywhere, and an opportunity to experiment with Rust).

That’s what I meant though, you don’t have to install the app.

I totally agree this should be easier with Cloudflare, it’s partly why I built Logflare.

You could make your own version of console.log and have it POST data to whatever logging solution you would prefer.

1 Like

Thanks, that’s a possibility. I’m not familiar with those kinds of logging services, but I’ll research it. I guess I could write a Firebase cloud function to log my Cloudflare workers. :thinking:

It looks like the lack of built-in logs essentially halves the number of requests included in the pricing plans?

Maybe I didn’t understand Logflare. When I clicked a link on the site it asked me to install it on my domain through the Cloudflare interface or create an Elixir server. Is there a way to just POST a small amount of logging data into Logflare without installing a Cloudflare app or an Elixir server? I would try that if it existed and didn’t take long to figure out.

I’m planning to use Phoenix on a future server, but just need a couple of quick cloud functions at the moment.

Ah yep that isn’t obvious until you sign up and create a source… here is a curl example though:

curl -X "POST" "https://api.logflare.app/logs" \
-H 'Content-Type: application/json' \
-H 'X-API-KEY: YOUR_API_KEY' \
-d $'{
  "source": "YOUR_SOURCE_ID",
  "log_entry": "This is another log message.",
  "metadata": {
    "ip_address": "100.100.100.100",
    "request_method": "POST",
    "custom_user_data": {
      "vip": true,
      "id": 38,
      "login_count": 154,
      "company": "Apple",
      "address": {
        "zip": "11111",
        "st": "NY",
        "street": "123 W Main St",
        "city": "New York"
      }
    },
    "datacenter": "aws",
    "request_headers": {
      "connection": "close",
      "user_agent": "chrome"
    }
  }
}'
1 Like

Thanks, that looks like it should work.