Skip to content

feat(logger): add global --log-format json option#2372

Merged
Drarig29 merged 5 commits into
masterfrom
corentin.girard/log-format-foundation
Jun 23, 2026
Merged

feat(logger): add global --log-format json option#2372
Drarig29 merged 5 commits into
masterfrom
corentin.girard/log-format-foundation

Conversation

@Drarig29

Copy link
Copy Markdown
Contributor

What and why?

First step toward structured (JSONL) logging across the whole CLI, addressing #1991 (distinguish info/warn/error in machine-readable output).

Adds a single global --log-format option (text default | json, env DD_LOG_FORMAT) that every command inherits without redeclaring it. In json mode, logs are emitted one JSON object per line with the level preserved, so log pipelines no longer see every line as info.

This is the foundation PR; per-plugin migrations and the synthetics reporter follow in later PRs (see plans/json-logging-migration.md). The flag is hidden until coverage is broad enough.

How?

  • BaseCommand exposes the inherited --log-format option (validated with typanion t.isEnum, env/precedence handled by clipanion's {env}) and a lazy, centralized this.logger getter — the single sanctioned sink that writes to the stream.
  • Migrated the commands that declared their own Logger onto the inherited getter (forced by the type-level collision with the new getter).
  • ESLint: forbid direct this.context.stdout/stderr and process.stdout/stderr, scoped to an allowlist that grows as commands migrate.
  • createMockContext defaults env to a defined empty object so clipanion's {env} option never reads undefined.
  • The datadog-ci vX.Y.Z banner now flows through the resolved command's logger, so it becomes a JSON line under --log-format json (and is skipped for --help/version); stdout stays pure JSONL.
  • Added plans/json-logging-migration.md tracking the multi-PR rollout.

Review checklist

  • Feature or bugfix MUST have appropriate tests (unit, integration)

Drarig29 and others added 4 commits June 21, 2026 17:37
Introduce a hidden, inherited `--log-format` (text|json, env DD_LOG_FORMAT)
option on BaseCommand backed by a shared `this.logger` getter, so any command
can emit JSONL logs with the level preserved (see #1991) without redeclaring
the option or instantiating its own logger.

- BaseCommand exposes a lazy `logger` getter (the single sanctioned stream
  sink) wired to the resolved log format; input validated via typanion
  `t.isEnum`, resolution order handled by clipanion's `{env}`.
- Migrate the commands that declared their own Logger onto the inherited
  getter (forced by the type-level collision with the new getter).
- ESLint: forbid direct this.context.stdout/stderr and process.stdout/stderr,
  scoped to a migrated-files allowlist that grows per PR.
- createMockContext defaults `env` to a defined empty object so clipanion's
  `{env}` option does not crash on undefined.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Track the multi-PR migration to JSONL logging with a checklist; PR 1
(foundation) is complete.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The entrypoint printed the `datadog-ci v…` banner directly to stdout before the
command ran, producing a non-JSON line that broke JSONL parsing. Parse argv
first (`cli.process`), then emit the banner via the resolved command's logger
(falling back to a default text logger for builtins / parse errors), so it
becomes a JSON line under `--log-format json` and stays dim text otherwise.

- Logger: add `isJsonOutput()` accessor.
- BaseCommand: expose `logger` publicly so the entrypoint can reuse it.
- printVersion now takes a Logger.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@datadog-official

This comment has been minimized.

@Drarig29 Drarig29 changed the title [feat] Add global --log-format (JSONL logging) option feat(logger): add global --log-format json option Jun 21, 2026
@Drarig29 Drarig29 added the datadog-ci For PRs spanning multiple commands, and repo-wide changes label Jun 21, 2026
@Drarig29
Drarig29 marked this pull request as ready for review June 22, 2026 08:18
@Drarig29
Drarig29 requested review from a team as code owners June 22, 2026 08:18

@GabrielAnca GabrielAnca left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great 🚀

@Drarig29
Drarig29 removed the request for review from a team June 22, 2026 08:46
@Drarig29
Drarig29 merged commit 426c548 into master Jun 23, 2026
33 checks passed
@Drarig29
Drarig29 deleted the corentin.girard/log-format-foundation branch June 23, 2026 08:39
@ava-silver ava-silver mentioned this pull request Jun 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

datadog-ci For PRs spanning multiple commands, and repo-wide changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants