FTS18 opened a new pull request, #66223:
URL: https://github.com/apache/airflow/pull/66223

   ### 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 `StreamHandler`s 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**
   - closes #66139


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to