Labsco
cloudflare logo

logging

✓ Official1,100

by cloudflare · part of cloudflare/sandbox-sdk

Use when adding logs, debugging, or working with the Logger across the SDK and container runtime. Covers the constructor-injection pattern, child loggers,…

🔥🔥✓ VerifiedFreeQuick setup
🔒 Repo-maintenance skill. It exists to help maintain cloudflare/sandbox-sdk itself — it's only useful if you contribute code to that project.

Use when adding logs, debugging, or working with the Logger across the SDK and container runtime. Covers the constructor-injection pattern, child loggers,…

Inspect the full instructions your agent will receiveExpand

This is the exact playbook injected into your agent when the skill activates — shown here so you can audit it before installing. You don't need to read it to use the skill.


name: logging description: Use when adding logs, debugging, or working with the Logger across the SDK and container runtime. Covers the constructor-injection pattern, child loggers, env-var configuration, and test mocking. (project)

Logging

Pattern: Explicit Constructor Injection

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

Copy & paste — that's it
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.

In Tests

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

Copy & paste — that's it
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