Skip to content

Do not override an existing logging configuration - #85

Open
Madin-H23 wants to merge 1 commit into
jaraco:mainfrom
Madin-H23:fix/keep-existing-logging-config
Open

Madin-H23 wants to merge 1 commit into
jaraco:mainfrom
Madin-H23:fix/keep-existing-logging-config

Conversation

@Madin-H23

@Madin-H23 Madin-H23 commented Oct 21, 2026 •

Copy link
Copy Markdown

Fixes #13.

cssutils creates the CSSUTILS logger at import time, adds a StreamHandler to it and forces the level to INFO. When an application configured logging before importing cssutils, that configuration was overridden, and a second ErrorHandler added a second handler.

This change:

  • installs the default StreamHandler only when the logger has no handlers at all, so an application handler is left alone and no duplicate handler is added,
  • applies the default level only when neither the CSSUTILS logger nor the root logger has an explicit level, so a level chosen by the application is preserved.

For an unconfigured logger the behaviour is unchanged (handler on stderr, level INFO).

One limit worth stating: the root logger's default level (WARNING) cannot be told apart from a level an application chose explicitly, so an application that sets exactly WARNING on the root logger and installs no handler of its own still gets the INFO default.

Verified locally:

  • pytest: 629 passed, 43 xfailed
  • ruff check and ruff format --check (ruff 0.17.1): clean
  • mypy --config-file mypy.ini cssutils: no new findings (the 40 reported errors are pre-existing on main)

🤖 This contribution was drafted with AI assistance; the analysis, the change and the tests are described above so that they can be reviewed.

The CSSUTILS logger was created with a StreamHandler and the INFO
level every time an ErrorHandler was instantiated, overriding a
logging setup the application had already done, and adding a second
handler when one was already configured.

Install the default handler only when the logger has no handlers at
all, and apply the default level only when neither the logger nor the
root logger has one, so that a level chosen by the application is
preserved. An unconfigured logger keeps the previous behaviour.
@Madin-H23

Copy link
Copy Markdown
Author

The tests workflow on this fork PR is waiting in action_required (the first-time-contributor approval that fork PRs need). Could you approve it so the CI runs?

The change is small, and here is the local verification:

  • pytest: 629 passed, 43 xfailed
  • ruff check and ruff format --check (ruff 0.17.1): clean
  • mypy --config-file mypy.ini cssutils: no new findings (the 40 reported errors are pre-existing on main)

The regression is reproducible on main alone: with the tests of this branch applied to the unpatched sources, test_keeps_existing_handler, test_keeps_configured_level, test_keeps_root_level and test_keeps_root_configuration all fail, because the logger setup forces the level to INFO and adds a second handler.

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.

CSSUTILS logger overrides existing config on import

1 participant