# Collect Cloudflare Workers Logs to Serverless Kafka

> **Source:** https://upstash.com/blog/cloudflare-workers-logs-kafka
> **Date:** 2022-06-14
> **Author(s):** Noah Fischer
> **Reading time:** 2 min read
> **Tags:** cloudflare-workers, kafka, edge, logs
> **Format:** text/markdown — machine-readable content for agents and LLMs

---

In this tutorial, we will show how to collect logs from Cloudflare Workers to Serverless Kafka.

### Kafka Setup

Create a Kafka cluster and topic using [Upstash Console](https://console.upstash.com) or [Upstash CLI](https://github.com/upstash/cli). Copy the Webhook URL to be used in the next steps.

### Project Setup

We will use Wrangler 2 for deployment, so please install (or upgrade) [Wrangler 2](https://developers.cloudflare.com/workers/wrangler/).

Create a folder for your project and run `wrangler init`. Select `js`:

```shell
➜  quickstart-cloudflare-workers wrangler init
 ⛅️ wrangler 2.0.7
-------------------
Using npm as package manager.
✨ Created wrangler.toml
Would you like to use git to manage this Worker? (y/n)
✨ Initialized git repository
No package.json found. Would you like to create one? (y/n)
✨ Created package.json
Would you like to use TypeScript? (y/n)
Would you like to create a Worker at src/index.js? (y/n)
✨ Created src/index.js
```

### The Code

Update `src/index.js` as below:

```js
export default {
  async fetch(request, env, context) {
    context.waitUntil(postLog("some log from cf workers!"));
    return new Response("Hello World!");
  },
};

function postLog(data) {
  return fetch("REPLACE_UPSTASH_WEBHOOK_URL", {
    method: "POST",
    body: data,
  });
}
```

Copy/paste the webhook URL from [Upstash Console](https://console.upstash.com). Webhook URL simply pushes everything posted to the Kafka. You can also use [Upstash Kafka SDK](https://github.com/upstash/upstash-kafka) or directly the [REST API](/docs/kafka/rest).

### Test and Deploy

You can test the function locally with `wrangler dev`.

Deploy your function to Cloudflare with `wrangler publish`

The endpoint of the function will be printed. You can check if logs are collected in Kafka by copying the `curl` expression from the console:

```shell
curl https://definite-goldfish-14184-us1-rest-kafka.upstash.io/consume/GROUP_NAME/GROUP_INSTANCE_NAME/mytopic -H "Kafka-Auto-Offset-Reset: earliest" -u \
  REPLACE_HERE
```