logging

Verwenden beim Hinzufügen von Logs, Debuggen oder Arbeiten mit dem Logger im gesamten SDK und der Container-Laufzeit. Deckt das Konstruktor-Injektionsmuster, Child-Logger, … ab.

npx skills add https://github.com/cloudflare/sandbox-sdk --skill logging

Logging

Pattern: Explicit Constructor Injection

Loggers are passed explicitly via constructor injection throughout the codebase. There is no global/ambient logger.

import type { Logger } from '@repo/shared';

class MyService {
  constructor(private logger: Logger) {}

  async doWork(context: WorkContext) {
    const childLogger = this.logger.child({ operation: 'work' });
    childLogger.info('Working', { context });
  }
}

Child loggers

Use logger.child({ ... }) to attach structured context that will appear on every log line from that child. Prefer child loggers at the boundary of a unit of work (request, operation, session) rather than re-passing context on every call.

Configuration

Two environment variables, both read once at startup:

VarValuesPurpose
SANDBOX_LOG_LEVELdebug | info | warn | errorMinimum level emitted
SANDBOX_LOG_FORMATjson | prettyOutput format

Use json in production (machine-parseable) and pretty for local dev.

In Tests

Use createNoOpLogger() from @repo/shared to silence logging in tests:

import { createNoOpLogger } from '@repo/shared';

const service = new MyService(createNoOpLogger());

Don't construct real loggers in unit tests — they add noise and can mask real failures with log output.

When Adding Logs

  • Log at info for significant lifecycle events (operation started/completed)
  • Log at debug for fine-grained tracing (request bodies, intermediate state)
  • Log at warn for recoverable anomalies
  • Log at error for failures that surface to the caller; include the error object as structured context: logger.error('Failed', { err })
  • Pass structured context as the second argument, not via string interpolation

Mehr Skills von cloudflare

workerd-api-review
cloudflare
Leistungsoptimierung, API-Design & Kompatibilität, Sicherheitslücken und Einhaltung von Standardspezifikationen für das Code-Review von workerd. Deckt tcmalloc-bewusste…
official
workerd-safety-review
cloudflare
Speichersicherheit, Threadsicherheit, Nebenläufigkeit und kritische Erkennungsmuster für die Codeüberprüfung von workerd. Behandelt V8/KJ-Grenzgefahren, Lebenszyklusverwaltung,…
official
module-registry
cloudflare
Laden, wenn mit dem Modul-Register in workerd gearbeitet wird – Lesen, Ändern, Debuggen oder Überprüfen von Modulauflösung, Kompilierung, Evaluierung oder Registrierung…
official
reproduce
cloudflare
Reproduzieren Sie ein cloudflare/agents GitHub-Problem, indem Sie ein minimales Agents/Worker-Projekt erstellen und es in einem temporären Cloudflare-Konto bereitstellen, dann berichten…
official
local-explorer
cloudflare
Wie man Produkte/Ressourcen zum lokalen Explorer oder zur lokalen API hinzufügt. Verwenden Sie dies bei der Implementierung neuer lokaler APIs oder UI-Routen unter…
official
commit-categories
cloudflare
Commit-Kategorisierungsregeln für Changelogs und "What's new"-Zusammenfassungen. MUSS geladen werden, bevor Commits in Changelog- oder Whats-new-Befehlen kategorisiert werden. Bietet…
official
architecture
cloudflare
Verwenden beim ersten Navigieren durch die Codebasis, Hinzufügen einer neuen Client-Methode, Hinzufügen eines neuen Container-Handlers/-Dienstes oder Verstehen, wie eine Anfrage fließt…
official
changesets
cloudflare
Verwenden beim Erstellen eines Changesets, Vorbereiten eines Releases oder Aktualisieren von Versionen. Deckt ab, auf welche Pakete verwiesen werden soll, wie benutzerseitige Changeset-Beschreibungen verfasst werden,…
official