Skip to content

Cli json pollution 66139 - #66223

Closed
FTS18 wants to merge 4 commits into
apache:mainfrom
FTS18:cli-json-pollution-66139
Closed

FTS18 wants to merge 4 commits into
apache:mainfrom
FTS18:cli-json-pollution-66139

Conversation

@FTS18

@FTS18 FTS18 commented May 1, 2026

Copy link
Copy Markdown

CLI: Eliminate JSON/YAML output pollution by redirecting logs to stderr (#66139)

This PR resolves the issue where machine-readable CLI outputs (JSON/YAML) were being polluted by log messages (e.g., Alembic migrations, import warnings), making them unparseable by tools like jq.

Problem

When running airflow dags list -o json, logs were printed to stdout before the JSON payload. This resulted in invalid JSON files and broken pipes when used in automation scripts.

Solution: Global Log Redirection

A global redirection strategy is implemented to address log pollution at the architectural level:

  1. Global Entry Point Injection: In airflow/__main__.py, the CLI detects machine-readable output flags (-o json, -o yaml) at the start of execution.
  2. Centralized Redirection Logic: Added _setup_cli_logging in airflow/utils/cli.py. This function identifies StreamHandlers pointing to sys.stdout and redirects them to sys.stderr.
  3. Execution Order Fix: Ensures log redirection occurs before command execution and database initialization.
  4. Consistency: Applied the behavior uniformly across CLI commands.

Regression Testing

Added airflow-core/tests/unit/cli/test_cli_output_pollution.py which:

  • Simulates full CLI execution via airflow.__main__.main
  • Injects logs during execution
  • Verifies stdout contains valid JSON only
  • Confirms logs are emitted to stderr
  • Ensures default (table) output remains unchanged

Closes / Related Issues

@bugraoz93

Copy link
Copy Markdown
Contributor

Can you check out my comment :) thanks for PR but soon we will already change these things.

#66139 (comment)

Copilot AI 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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@FTS18

FTS18 commented May 1, 2026 •

Copy link
Copy Markdown
Author

Can you check out my comment :) thanks for PR but soon we will already change these things.

#66139 (comment)

Thanks for the quick feedback, @bugraoz93!

Understood. If the transition to airflowctl is the priority and will solve this inherently, I'm happy to close this PR to avoid adding technical debt to the legacy CLI. Closing this now!

@FTS18 FTS18 closed this May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI JSON output is polluted by logs and cannot be parsed by jq

3 participants