Blyp Docs

Cloudflare Workers

Workers use a separate API from the Node/Bun server adapters.

import { initWorkersLogger, createWorkersLogger } from "@blyp/core/workers";

initWorkersLogger({
  env: { service: "my-worker" },
  customProps: (request) => ({
    requestId: request.headers.get("x-request-id"),
  }),
});

export default {
  async fetch(request: Request): Promise<Response> {
    const log = createWorkersLogger(request);

    log.set({ action: "handle_request" });
    log.info("worker started");

    const response = Response.json({ ok: true });
    log.emit({ response });

    return response;
  },
};

Differences from the Node/Bun adapters

Error emission

const log = createWorkersLogger(request);

try {
  // handler work
} catch (error) {
  log.emit({ error });
  throw error;
}

emit() returns the first emitted HttpRequestLog record and does not emit twice for the same request logger.

What emitted request logs look like

When you call emit({ response }), Blyp writes request output like:

[INFO]  GET  /health        200  2ms
[INFO]  POST /checkout      200  143ms
[INFO]  GET  /users/42      404  8ms
[ERROR] POST /payments      500  1203ms

Fields included automatically: method, path, status code, and duration.

In production (NDJSON):

{"level":"info","time":1710000000000,"msg":"GET /health","type":"http_request","method":"GET","url":"/health","statusCode":200,"responseTime":2}
{"level":"info","time":1710000000001,"msg":"POST /checkout","type":"http_request","method":"POST","url":"/checkout","statusCode":200,"responseTime":143}

Relevant types

import type {
  WorkersLoggerConfig,
  WorkersEmitOptions,
  WorkersRequestLogger,
} from "@blyp/core/workers";