Skip to content

Add configure_logging and a package NullHandler - #89

Open
namurphy wants to merge 13 commits into
PlasmaPy:mainfrom
namurphy:logging-infrastructure
Open

namurphy wants to merge 13 commits into
PlasmaPy:mainfrom
namurphy:logging-infrastructure

Conversation

@namurphy

Copy link
Copy Markdown
Member

This PR adds the logging infrastructure from #87 on its own, so that it can be reviewed separately from the changes to the decommutator. The decommutator does not use it yet, and its behavior is unchanged.

Changes

  • Add a NullHandler to the pyfaradaycup logger, so that messages aren't printed through Python's last resort handler when an application has not configured logging.
  • Add pyfaradaycup.configure_logging, which sends messages from pyfaradaycup to the console and/or a log file with UTC timestamps, and can optionally capture warnings. It returns a LoggingConfiguration that works as a context manager and removes the handlers on close.
  • Ban logging.basicConfig, logging.FileHandler, logging.StreamHandler, and logging.handlers with ruff outside of _logging.py and tests.
  • Add tests in tests/test_logging.py.

Testing

nox -s tests passes on Python 3.9 to 3.14, and pre-commit and nox -s docs also pass.

🤖 Generated with Claude Code

namurphy and others added 9 commits October 30, 2026 12:52
Provide the logging setup for the Python API and command line tools,
without yet using it in the decommutator. The NullHandler keeps
messages from being printed through Python's last resort handler when
an application has not configured logging.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The comments and docstrings assumed familiarity with the logging module.
Define terms like logger, level, handler, and propagation for readers
who have mainly used print statements.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replacing a configuration recorded the level set by the earlier call as
the one to restore, so the logger never returned to its original level.
Close the earlier configuration instead, which restores the level and
removes the need to find handlers by name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With no handlers, close could not tell that warnings had been captured
and left them captured for the rest of the program.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Setting propagate to False would also stop messages from reaching
Dagster's root-level capture and pytest's caplog, so describe the
behavior and how to avoid it instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Calling configure_logging inside a step whose logger Dagster manages
floods the event log and alters the log file, so describe the
recommended setup and the alternative for separate log files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A StreamHandler keeps the stream it was created with, so logging fails
with a closed file error after a tool like pytest replaces sys.stderr.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cover the fixes for restoring the level, capturing warnings without
handlers, and following sys.stderr, along with the two Dagster setups
described in the documentation. The Dagster tests are skipped when
Dagster is not installed, and they ignore warnings because Dagster and
its dependencies can issue them on import or during a run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/pyfaradaycup/_logging.py Resolved
namurphy and others added 4 commits October 30, 2026 14:26
This matches the call in LoggingConfiguration.close and removes the need
to ignore a lint rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comparing against the latest configuration states directly that closing
a replaced or closed configuration does nothing, instead of inferring
it from which loggers still have the handlers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant