Skip to content

feat(logger): add jsonOutput to internal logger#2347

Merged
Drarig29 merged 1 commit into
DataDog:masterfrom
tsushanth:feat/issue-1991-json-logging
Jun 11, 2026
Merged

feat(logger): add jsonOutput to internal logger#2347
Drarig29 merged 1 commit into
DataDog:masterfrom
tsushanth:feat/issue-1991-json-logging

Conversation

@tsushanth

@tsushanth tsushanth commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Why

Related to #1991.

The existing Logger only emits ANSI-coloured text. When the CLI runs inside a log pipeline (Datadog itself, or any other ingestion path), every line is unstructured — error records show up as info because the consumer can't tell them apart without parsing the colour codes or prefixes.

What

  • Add a third constructor shape that accepts LoggerOptions ({ shouldIncludeTimestamp?, jsonOutput? }). The legacy boolean form (new Logger(write, INFO, true)) still works unchanged, so existing call sites compile without edits.

  • When jsonOutput is true, each log call writes a single-line JSON object:

    {"level":"error","message":"deploy failed","timestamp":"2026-06-10T15:09:00.123Z"}

    level is the canonical lowercase name. timestamp is only emitted when shouldIncludeTimestamp is also enabled (matching how the text-mode prefix works). ANSI colours are intentionally skipped so the line parses cleanly with JSON.parse.

  • Expose setJsonOutput(boolean) alongside the existing setShouldIncludeTime so plugins can flip the format at runtime (e.g. based on a CLI flag or env var).

Tests

packages/base/src/helpers/__tests__/logger.test.ts — 9 cases covering:

  • Text mode: trailing newline, level gating, ISO timestamp prefix (both legacy boolean and options-bag form).
  • JSON mode: level/message correctness across all four levels, no ANSI codes, timestamp inclusion vs omission, JSON→text runtime toggle.

yarn build, yarn lint, yarn test packages/base/src/helpers/__tests__/logger.test.ts — all clean.

Follow-up

Wiring a --json (or DATADOG_CI_JSON_LOG=1) toggle into the individual plugin commands would close the loop end-to-end, but I've left that out of this PR since it touches every plugin and the right pattern (per-plugin flag vs. global) is a separate design call. Happy to do it as a follow-up if you want — flagging here so reviewers know it's intentional scope, not an oversight.

Adds an opt-in `jsonOutput` mode that emits a single-line JSON object
per log call instead of ANSI-coloured text. Each entry carries the
`level` ("debug" | "info" | "warn" | "error") and `message`; when
`shouldIncludeTimestamp` is also set, an ISO `timestamp` field is
included alongside. This addresses the case in DataDog#1991 where a Datadog
ingestion pipeline (or any other log consumer) sees every line as
unstructured text and defaults `error` records to `info`.

The third constructor parameter is now `boolean | LoggerOptions`. The
boolean form preserves the existing call sites verbatim
(`new Logger(stdout, INFO, true)`); the options form unlocks the new
`jsonOutput` flag and remains forward-compatible. A `setJsonOutput`
runtime toggle is also exposed alongside the existing
`setShouldIncludeTime`.

Tests cover both modes — log-level gating, timestamp inclusion / omission,
ANSI-stripping in JSON mode, and a JSON→text toggle.
@tsushanth
tsushanth requested a review from a team as a code owner June 10, 2026 17:01
@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Jun 11, 2026

Copy link
Copy Markdown

Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 376c7b4 | Docs | Datadog PR Page | Give us feedback!

@Drarig29 Drarig29 added chores Related to the CI or developer experience datadog-ci For PRs spanning multiple commands, and repo-wide changes labels Jun 11, 2026
@Drarig29 Drarig29 changed the title feat(logger): support JSON log output feat(logger): add jsonOutput to internal logger Jun 11, 2026
@Drarig29 Drarig29 removed the chores Related to the CI or developer experience label Jun 11, 2026

@Drarig29 Drarig29 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.

LGTM. Will merge and think about the best way to support this in all commands. We already have internal conversations ongoing for a shared base command class.

@Drarig29
Drarig29 merged commit 95ead4d into DataDog:master Jun 11, 2026
35 of 36 checks passed
@GabrielAnca GabrielAnca mentioned this pull request Jun 17, 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.

2 participants