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

   <!-- copilot-assessment:v1 -->
   ### Agent assessment
   
   Self-reported assessment, not an independent review.
   
   Assessed revision: `47999a9cf1014d4103cc5759967e8e979e93c2c2`
   
   | Dimension | Assessment | Rationale |
   |---|---|---|
   | Change confidence | 8.5/10 | ↑ New parametrized test (text and JSON logs) 
fails without the change and passes with it.<br>↑ Clean 3.4.0 install patched 
with the change: `dags list -o json` output parses, with text and JSON logs; 
the warning moves to stderr.<br>↓ Loggers structlog cached before the redirect 
keep writing to stdout; none were seen in the CLI path.<br>↓ CI, Breeze and the 
full CLI suite were not run. |
   | Diagnosis confidence | 9.5/10 | ↑ The warning is emitted by a native 
structlog logger (`py.warnings`) whose logger factory has no stream, so it 
falls back to stdout and skips the root-logger handlers that #68598 redirects. |
   | Evidence fit | 9.5/10 | ↑ Reproduced the exact output from #66139 on clean 
3.4.0 and on main; the same command parses with the fix. |
   <!-- /copilot-assessment -->
   
   `airflow dags list -o json` (and other commands with `-o`) still prints the 
"Could not import graphviz" warning on stdout before the JSON when graphviz is 
not installed, so `airflow dags list -o json 2>/dev/null | python -m json.tool` 
fails. This still happens on 3.4.0 and main.
   
   #68598 moves root-logger `StreamHandler`s that write to stdout over to 
stderr for these commands. Captured Python warnings don't go through those 
handlers: Airflow's `warnings.showwarning` hook logs them with a native 
structlog logger (`py.warnings`), which writes through the structlog logger 
factory. That factory is configured without a stream, so it writes to stdout.
   
   This change makes `redirect_stdout_log_handlers_to_stderr()` also point the 
structlog logger factory at stderr when it has no stream set, which is what 
makes it fall back to stdout (text logs use `sys.stderr`, JSON logs use 
`sys.stderr.buffer`). A factory with an explicit stream is left alone.
   
   Another option was to stop warning about graphviz at import time and warn 
only when a command renders a graph. That would fix this one message, but any 
other warning raised while a command runs would still corrupt the output, so I 
fixed the routing instead.
   
   Reproduction on a clean install, without graphviz:
   
   ```console
   $ pip install "apache-airflow==3.4.0" --constraint 
https://raw.githubusercontent.com/apache/airflow/constraints-3.4.0/constraints-3.12.txt
   $ airflow db migrate
   $ airflow dags list -o json 2>/dev/null | python -m json.tool
   Extra data: line 1 column 5 (char 4)
   ```
   
   The first line on stdout was:
   
   ```
   <timestamp> [warning  ] Could not import graphviz. Rendering graph to the 
graphical format will not be possible. ... [py.warnings] category=UserWarning 
filename=.../airflow/cli/commands/dag_command.py lineno=57
   ```
   
   With this change the JSON parses and the warning is printed on stderr. I 
checked this with text logs and with `AIRFLOW__LOGGING__JSON_LOGS=True`.
   
   Tests run locally:
   
   - `pytest airflow-core/tests/unit/cli/test_utils.py`: 12 passed. With the 
fix reverted, the two new `test_redirect_structlog_logger_factory` cases fail 
(stdout holds the warning).
   - The same file on top of `v3-4-test` (cherry-picked): 12 passed.
   - `ruff format` and `ruff check` (0.17.3) on the changed files: clean.
   
   These commands are moving to airflowctl (AIP-94), but they still ship in 
3.x, and #68598 fixed the log-handler part in 3.3.1. Could a maintainer add 
this to the Airflow 3.4.1 milestone with the `backport-to-v3-4-test` label? The 
commit cherry-picks cleanly onto `v3-4-test`, and the tests pass there.
   
   closes: #66139
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   <!--
   If generative AI tooling has been used in the process of authoring this PR, 
please
   change below checkbox to `[X]` followed by the name of the tool, uncomment 
the "Generated-by".
   -->
   
   - [X] Yes (please specify the tool below)
   
   Created with help of GHCP (GitHub Copilot) following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   
   ---
   
   * Read the **[Pull Request 
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
 for more information. Note: commit author/co-author name and email in commits 
become permanently public when merged.
   * For fundamental code changes, an Airflow Improvement Proposal 
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
 is needed.
   * When adding dependency, check compliance with the [ASF 3rd Party License 
Policy](https://www.apache.org/legal/resolved.html#category-x).
   * For significant user-facing changes create newsfragment: 
`{pr_number}.significant.rst`, in 
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
 You can add this file in a follow-up commit after the PR is created so you 
know the PR number.
   


-- 
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