Repository navigation
fix(emissions_tracker): warn and skip LOGGER output when logging_logger is missing (#1412) - #1416
Prasanthmax wants to merge 2 commits into
Conversation
davidberenstein1957
left a comment
There was a problem hiding this comment.
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?
|
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.
ad027f1 to
fbba922
Compare
Closes #1412
Root cause
_init_output_methods()appendedself._logging_loggerto the handlers listwhen
OutputMethod.LOGGERwas requested, even when it wasNone. Theresulting
AttributeErrorwas swallowed by asuppress()block in_persist_data(), sotracker.stop()silently produced no output.Fix
When
OutputMethod.LOGGERis requested without alogging_logger, log awarning and skip the handler. The tracker keeps its other outputs.
My first version raised a
ValueErrorin the constructor. @davidberenstein1957pointed out in review that this would break users who enable
save_to_loggerthrough 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
test_logger_output_warns_and_skips_without_logging_logger, which checksthe warning and that no
Noneends up in_output_handlers.test_logger_output_succeeds_with_logging_logger.CODECARBON_SAVE_TO_LOGGER=trueand no logger passed, the tracker is created, warns, and keeps CSV output.
test_emissions_tracker.pyandtest_custom_handler.pypass locally. Thetest_custom_handler.pychange from the first version was reverted.Thanks @sohammishra864-wq for reporting this.