Skip to content

Log the source of every configuration key - #1389

Open
NoiceHax wants to merge 1 commit into
mlco2:masterfrom
NoiceHax:fix/issue-312
Open

Log the source of every configuration key#1389
NoiceHax wants to merge 1 commit into
mlco2:masterfrom
NoiceHax:fix/issue-312

Conversation

@NoiceHax

Copy link
Copy Markdown

Description

get_hierarchical_config only logged which .codecarbon.config files it had read, so there was still no way to tell which layer set a given key. It now reads the global file, the local file and the CODECARBON_ environment variables into separate dicts, merges them in the same order as before, and keeps a key to source map next to the result. The two file level messages are replaced by one line naming the source of each resolved key, with environment variables shown as CODECARBON_<KEY>.

Only key names and their source are logged, never the values, because api_key and experiment_id live in this config space.

Example output:

[codecarbon INFO @ 09:30:00] Codecarbon configuration: api_key from /home/victor/.codecarbon.config, experiment_id from CODECARBON_EXPERIMENT_ID, measure_power_secs from /home/victor/.codecarbon.config

Related Issue

Closes #312

Motivation and Context

Config comes from three places that silently override each other. When a value was not what you expected, nothing in the log pointed at the file or the variable responsible. That is what the issue asks for, for example knowing where experiment_id was picked up from.

How Has This Been Tested?

Two tests added to tests/test_config.py. The first checks the key to source map for a key set only in the global file, a key overridden by the local file, and a key overridden by an environment variable. The second checks the log line names the sources and does not contain the value of api_key.

Ran CODECARBON_ALLOW_MULTIPLE_RUNS=True pytest tests/test_config.py tests/test_resource_tracker.py tests/cli, 114 passed. The full package suite gives 627 passed with one failure in test_task_energy_with_live_update_interference, which fails the same way on master on my machine, so it does not come from this change.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

AI Usage Disclosure

  • 🟥 AI-vibecoded: You cannot explain the logic. Car analogy : the car drive by itself, you are outside it and just tell it where to go.
  • 🟠 AI-generated: Car analogy : the car drive by itself, you are inside and give instructions.
  • â­� AI-assisted. Car analogy : you drive the car, AI help you find your way.
  • â™»ï¸� No AI used. Car analogy : you drive the car.

I used an AI coding agent to write this patch and this description from an approach I decided on, and I reviewed the code and ran the tests locally before opening the PR.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the docs/how-to/contributing.md document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

get_hierarchical_config only logged which .codecarbon.config files were
read, so there was no way to tell which layer had set a given key.

It now reads each layer on its own and keeps a key to source map while
merging the global file, the local file and the CODECARBON_ environment
variables. The two file level messages are replaced by a single line
naming the source of every resolved key, with environment variables
shown as CODECARBON_<KEY>. Values are never logged, as api_key and
experiment_id live in this config space.
@NoiceHax
NoiceHax requested a review from a team as a code owner August 15, 2026 11:52
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.

Add log of config and where they come from

1 participant