mlco2 / mlco2/codecarbon

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

Open
#1,412 2 comments 0 reactions 1 assignee View on GitHub

@Prasanthmax is already working on this.

Since Sep 17, 2026.

Dominant language
Python
Stars
1.9k
Forks
323
Avg merge
1d 12h
Merged PRs (30d)
12

Description

Description

When output_methods=[OutputMethod.LOGGER] is passed without providing a logging_logger, None is appended to the output handlers list. This causes AttributeError: 'NoneType' object has no attribute 'out' on flush(), and silently swallows all output (including CSV) on stop() because @suppress(Exception) catches the error.

Reproduction

from codecarbon import EmissionsTracker
from codecarbon.output_methods.base_output import OutputMethod

tracker = EmissionsTracker(output_methods=[OutputMethod.LOGGER])
tracker.start()
tracker.flush()  # AttributeError: 'NoneType' object has no attribute 'out'

With stop() instead of flush(), the crash is silently swallowed by @suppress(Exception) — no error is raised, but ALL output handlers (including CSV) are skipped because the exception aborts the handler loop.

Root cause

In emissions_tracker.py line 638-639, _init_output_methods() does:

if OutputMethod.LOGGER in methods:
    self._output_handlers.append(self._logging_logger)

When no logging_logger is provided, self._logging_logger is None (set by _set_from_conf with no default). So None gets appended to _output_handlers. Later, iterating over handlers and calling .out() on None crashes.

Expected behavior

Either:

  • Validate that self._logging_logger is not None before appending, and log a warning / raise an error if LOGGER method was requested without a logger
  • Or skip the LOGGER method with a warning

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.