Skip to content

Use cosmos.log.get_logger consistently#2670

Merged
pankajastro merged 1 commit into
mainfrom
use-get-logger-consistently
May 14, 2026
Merged

Use cosmos.log.get_logger consistently#2670
pankajastro merged 1 commit into
mainfrom
use-get-logger-consistently

Conversation

@pankajastro
Copy link
Copy Markdown
Contributor

@pankajastro pankajastro commented May 13, 2026

The rest of the codebase (31 modules) instantiates loggers via cosmos.log.get_logger so messages carry the (astronomer-cosmos) prefix when rich_logging is enabled. A few outliers still used the stdlib logger directly, breaking that branding and bypassing scoped log-level configuration:

  • cosmos/operators/_asynchronous/base.py: dropped an unused log = logging.getLogger(__name__) (no callers in-module or via imports).
  • cosmos/profiles/snowflake/user_privatekey_file.py: switched logger to get_logger; it is used for the passphrase warning.
  • cosmos/dbt/parser/output.py: replaced a module-level logging.error(...) call (which hit the root logger) with a proper logger.error(...) via a module-level get_logger instance.
  • cosmos/plugin/airflow3.py: replaced four module-level logging.exception(...) root-logger calls similarly.

closes: #1157

The rest of the codebase (31 modules) instantiates loggers via
cosmos.log.get_logger so messages carry the (astronomer-cosmos) prefix
when rich_logging is enabled. A few outliers still used the stdlib
logger directly, breaking that branding and bypassing scoped log-level
configuration:

- cosmos/operators/_asynchronous/base.py: dropped an unused
  `log = logging.getLogger(__name__)` (no callers in-module or via
  imports).
- cosmos/profiles/snowflake/user_privatekey_file.py: switched
  `logger` to get_logger; it is used for the passphrase warning.
- cosmos/dbt/parser/output.py: replaced a module-level
  `logging.error(...)` call (which hit the root logger) with a proper
  `logger.error(...)` via a module-level get_logger instance.
- cosmos/plugin/airflow3.py: replaced four module-level
  `logging.exception(...)` root-logger calls similarly.

No functional change beyond the log prefix and proper logger scoping.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings May 13, 2026 11:23
@pankajastro pankajastro requested review from a team, corsettigyg, dwreeves and jbandoro as code owners May 13, 2026 11:23
@pankajastro pankajastro requested review from pankajkoti and tatiana May 13, 2026 11:23
@pankajastro pankajastro changed the title Use cosmos.log.get_logger consistently Use cosmos.log.get_logger consistently May 13, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR standardizes logging across a few remaining outlier modules by switching from stdlib/root logger usage to cosmos.log.get_logger, ensuring log messages consistently include the (astronomer-cosmos) prefix when rich_logging is enabled and participate in Cosmos-scoped log configuration.

Changes:

  • Replace stdlib/root logger usage with get_logger(__name__) in modules that emit log messages.
  • Remove an unused logging import and unused module-level logger in the async operator factory module.
  • Convert module-level logging.exception/error(...) calls to use a module-level Cosmos logger instance.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
cosmos/profiles/snowflake/user_privatekey_file.py Switches module logger to get_logger for the passphrase warning.
cosmos/plugin/airflow3.py Introduces a module-level Cosmos logger and replaces root-logger exception calls with logger.exception(...).
cosmos/operators/_asynchronous/base.py Removes unused stdlib logger setup (logging import and unused log).
cosmos/dbt/parser/output.py Adds module-level Cosmos logger and replaces a root-logger error call with logger.error(...).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.02%. Comparing base (f02859b) to head (b0aff0e).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2670   +/-   ##
=======================================
  Coverage   98.02%   98.02%           
=======================================
  Files         105      105           
  Lines        7844     7844           
=======================================
  Hits         7689     7689           
  Misses        155      155           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Collaborator

@tatiana tatiana left a comment

Choose a reason for hiding this comment

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

Thanks for improving this, @pankajastro ! Would it be worth updating our agents' CLAUDE.md with recommendations on how to instantiate log, so we avoid similar patterns in the future? Happy for this to be addressed in a follow-up PR.

pankajastro added a commit that referenced this pull request May 14, 2026
Per PR #2670, all modules should instantiate loggers via
cosmos.log.get_logger so messages carry the (astronomer-cosmos)
prefix when rich_logging is on and pick up scoped log-level
configuration. Calling logging.getLogger or the root logger
directly bypasses both.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@pankajastro
Copy link
Copy Markdown
Contributor Author

Thanks for improving this, @pankajastro ! Would it be worth updating our agents' CLAUDE.md with recommendations on how to instantiate log, so we avoid similar patterns in the future? Happy for this to be addressed in a follow-up PR.

Created PR: #2679

@pankajastro pankajastro merged commit 888650f into main May 14, 2026
129 checks passed
@pankajastro pankajastro deleted the use-get-logger-consistently branch May 14, 2026 14:07
@pankajastro pankajastro added this to the Cosmos 1.15.0 milestone May 20, 2026
pankajastro added a commit that referenced this pull request Jun 3, 2026
The Logging section told contributors to "get loggers via
`cosmos.log.get_logger`" unconditionally, which would lead someone
working in an operator to add a module-level `get_logger` — the exact
inconsistency #1157 set out to fix.

Scope that guidance to library/module-level code and state the operator
rule: inside operators and hooks (anything with `LoggingMixin`), log via
`self.log` so messages land in the per-task-instance log shown in the
Airflow UI. Examples updated to show both cases.

This locks in the outcome of the logging cleanup (#2670, #2680, #2681)
so it doesn't silently regress.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make logging consistent from Cosmos operators

3 participants