Skip to content

fix(emissions_tracker): warn and skip LOGGER output when logging_logger is missing (#1412) - #1416

Open
Prasanthmax wants to merge 2 commits into
mlco2:masterfrom
Prasanthmax:fix/logger-issue-1412
Open

Prasanthmax wants to merge 2 commits into
mlco2:masterfrom
Prasanthmax:fix/logger-issue-1412

Conversation

@Prasanthmax

@Prasanthmax Prasanthmax commented Sep 18, 2026 •

Copy link
Copy Markdown

Closes #1412

Root cause

_init_output_methods() appended self._logging_logger to the handlers list
when OutputMethod.LOGGER was requested, even when it was None. The
resulting AttributeError was swallowed by a suppress() block in
_persist_data(), so tracker.stop() silently produced no output.

Fix

When OutputMethod.LOGGER is requested without a logging_logger, log a
warning and skip the handler. The tracker keeps its other outputs.

My first version raised a ValueError in the constructor. @davidberenstein1957
pointed out in review that this would break users who enable save_to_logger
through the config file or an environment variable: they cannot supply a
logger object from there, so a raise would turn a setting that works today
into a crash. Warning and skipping covers that case.

Testing

  • Replaced the raising test with
    test_logger_output_warns_and_skips_without_logging_logger, which checks
    the warning and that no None ends up in _output_handlers.
  • Kept test_logger_output_succeeds_with_logging_logger.
  • Reproduced the config/env case by hand: with CODECARBON_SAVE_TO_LOGGER=true
    and no logger passed, the tracker is created, warns, and keeps CSV output.
  • test_emissions_tracker.py and test_custom_handler.py pass locally. The
    test_custom_handler.py change from the first version was reverted.

Thanks @sohammishra864-wq for reporting this.

@Prasanthmax
Prasanthmax requested a review from a team as a code owner September 18, 2026 15:32

@davidberenstein1957 davidberenstein1957 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, the test and the test_custom_handler.py update are good. One design point before this merges: raising ValueError in the constructor is a breaking change for anyone who sets save_to_logger=True from the environment or config file without passing logging_logger. Today they get a silently missing handler, with this PR they get a crash at tracker creation.

I would rather log a warning and skip the LOGGER handler, so the tracker still records to its other outputs. Could you change the raise to a warning and adjust the test?

@Prasanthmax

Copy link
Copy Markdown
Author

Hi @davidberenstein1957, thanks for the review, and for flagging this. You’re right. I tested the explicit constructor path but not users who enable save_to_logger through the config file or an env var. They can’t pass a logging_logger object from there, so raising would break setups that work today. I’ll switch the raise to a warning that skips the LOGGER handler, update the tests, and revert my edits to test_decorator_flush and test_save_to_flags_map_to_output_methods_and_warn. Will push shortly.

…gging_logger

Fixes mlco2#1412. OutputMethod.LOGGER silently appended None to
_output_handlers when logging_logger wasn't provided, causing an
AttributeError inside a suppress() block later in flush()/stop() —
swallowed silently, giving no error and no output.

Now raises a clear ValueError at construction time instead.

Added test_logger_output_raises_without_logging_logger and
test_logger_output_succeeds_with_logging_logger in
test_emissions_tracker.py.

Updated two existing tests that relied on the old silent-None
behavior without ever supplying a real logger:
test_save_to_flags_map_to_output_methods_and_warn and
test_decorator_flush.
…er is missing

Raising in the constructor would break users who enable save_to_logger via
config file or env var, since they cannot supply a logger object from there.
Log a warning and skip the handler so the other outputs keep working.

Revert the test_custom_handler.py change, which is no longer needed.
@Prasanthmax
Prasanthmax force-pushed the fix/logger-issue-1412 branch from ad027f1 to fbba922 Compare October 13, 2026 05:45
@github-actions github-actions Bot added size/M and removed size/S labels Oct 13, 2026
@Prasanthmax Prasanthmax changed the title fix(emissions_tracker): raise ValueError for LOGGER output without logging_logger (#1412) fix(emissions_tracker): warn and skip LOGGER output when logging_logger is missing (#1412) Oct 13, 2026
@Prasanthmax Prasanthmax changed the title fix(emissions_tracker): warn and skip LOGGER output when logging_logger is missing (#1412) fix(emissions_tracker): warn and skip LOGGER output when logging_logger is missing (#1416) Oct 13, 2026
@Prasanthmax Prasanthmax changed the title fix(emissions_tracker): warn and skip LOGGER output when logging_logger is missing (#1416) fix(emissions_tracker): warn and skip LOGGER output when logging_logger is missing (#1412) Oct 13, 2026

This branch has not been deployed

No deployments
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.

OutputMethod.LOGGER without logging_logger appends None to handlers, crashes on flush()

2 participants