Skip to the content

Capsid menu

src/log.ts

24 lines and 1 functions in DrDustinEdwards/capsid, at commit daa875b on 2026-10-24. This file on GitHub.

Functions

The functions of log.ts
FunctionLinesComplexityCoveredCRAPMutation
logEvent14 to 273100%OkCRAP 3No data: mutation testing has not run on this repo yet

Imports

It imports no other module of this repo. Imported by src/admin-client-audit.ts, src/agents.ts, src/auto-merge-tick.ts, src/backup.ts, src/bench.ts, src/controls-perform.ts, src/conventions-read.ts, src/dcr.ts, src/digest.ts, src/doc-index.ts, src/github/client.ts, src/index.ts, src/jobs-transition.ts, src/merge-safety.ts, src/merge-units.ts, src/model-drift.ts, src/ops-cloudflare.ts, src/ops-feed.ts, src/ops-otlp.ts, src/ops-packages.ts, src/ops-snapshot.ts, src/outcome-prs.ts, src/overnight-plan.ts, src/overnight.ts, src/portal-actions.ts, src/rate-limit.ts, src/routes.ts, src/routine-fire.ts, src/scope.ts, src/site-key.ts, src/site-repair.ts, src/site-watch.ts, src/stale-canon.ts, src/task-runs.ts, src/tick.ts, src/tools/repo-guards.ts, src/tools/repo.ts, src/trends.ts, src/watcher-findings.ts, src/watcher-gather.ts, src/watcher.ts.

Source

// The one place the Worker writes a log line. Every line is a JSON object with an
// upper-case `event` token to filter on and a `message` holding the human text, so a
// log search can match the token exactly and a person still reads the sentence.
//
// This file depends on nothing in the Worker (no Env), so any module can import it.
// It never throws: a log call sits inside catch blocks and cron steps, and a field
// that cannot be serialised (a circular object, a BigInt) must not turn a handled
// failure into a new one.

export type LogLevel = "log" | "warn" | "error";

export type LogFields = { message: string } & Record<string, unknown>;

export function logEvent(level: LogLevel, event: string, fields: LogFields): void {
  let line: string;
  try {
    const { message, event: _shadowed, ...rest } = fields;
    line = JSON.stringify({ event, message, ...rest });
  } catch {
    line = `${event} ${String(fields.message)}`;
  }
  try {
    console[level](line);
  } catch {
    // A console that throws has nowhere left to report to.
  }
}