Skip to content

fix(logging): write plain output to the current sys.stderr - #111

Open
timtreis wants to merge 3 commits into
scverse:mainfrom
timtreis:fix/logging-live-stderr
Open

timtreis wants to merge 3 commits into
scverse:mainfrom
timtreis:fix/logging-live-stderr

Conversation

@timtreis

Copy link
Copy Markdown
Member

Summary: The plain log handler (used without rich) now writes to whatever sys.stderr is when a record is emitted, so contextlib.redirect_stderr and pytest's capsys and capfd capture its output.

Before this change, _make_handler created logging.StreamHandler(), which stores the sys.stderr object that exists when the handler is made. When scverse_misc.logging was imported before a redirect or before pytest swapped sys.stderr, log records went to the old stream and the capture missed them. The rich handler already followed the current sys.stderr, so the two paths behaved differently.

Changes:

  • The plain handler writes through a small object whose write and flush forward to the current sys.stderr.
  • A test logs inside redirect_stderr and checks that the message lands in the buffer. It fails on main.

Found while moving mantispy and squidpy onto scverse_misc.logging.

@codecov

codecov Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.45%. Comparing base (055df50) to head (280ab6e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #111      +/-   ##
==========================================
+ Coverage   94.31%   94.45%   +0.13%     
==========================================
  Files          13       13              
  Lines         845      865      +20     
==========================================
+ Hits          797      817      +20     
  Misses         48       48              
Files with missing lines Coverage Δ
src/scverse_misc/logging.py 96.96% <100.00%> (+0.41%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread tests/test_logging.py
Comment on lines +259 to +263
class ToLogger(io.StringIO):
def write(self, s: str) -> int:
get_logger("selftest.stderr").warning(s)
return len(s)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that a valid usecase? I'd argue that if someone wants to simply print to stderr we shouldn't force any additional formatting or infrastructure on him. If we discard that usecase, this entire PR can be simplified to handler = logging._stdErrHandler().

I'd strongly argue that this is something that we shouldn't be concerned about, since this test would fail even using plain stdlib logging (which falls back to logging._stdErrHandler if nothing else is defined):

import sys
import logging
import io

class ToLogger(io.StringIO):
    def write(self, s: str) -> int:
        logging.getLogger("selftest.stderr").warning(s)
        return len(s)

sys.stderr = ToLogger()
logging.getLogger("selftest").warning("hello")

also fails with a RecursionError.

@ilia-kats

ilia-kats commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

In addtion to my comment above, I'd actually argue that it's the behavior of the RichLogger that should be changed, since it differs from stdlib logging, while the logging.StreamHandler is in line with what people expect. The RichLogger can be fixed by simply using Console(file=sys.stderr)) instead of Console(stderr=True).

If pytest is the only problem, one can simply set config.verbosity = config.verbosity) after pytest swaps out stderr. Alternatively, LogConfig can have another attribute stream, which defaults to sys.stderr but can be set by user code and which both handlers write to. None can be used to mean "whatever the current sys.stderr points to".

This branch has not been deployed

No deployments
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.

2 participants