← index #691PR #806
Duplicate · high · value 5.391
QUERY · ISSUE

logging.StreamHandler should call super().__init__()

openby uldaopened 2023-06-22updated 2023-08-05

logging.StreamHandler inherits from logging.Handler but does not call super().init()
so it misses to initialize level and formatter instance variables

by the way, logging.Handler should initialize formatter to a working formatter, not None.
e.g. if you create and add a new StreamHandler, it has no formatter set by default and any logging will generate an exception.

1 comment
colin-nolan · 2023-08-05

I also encountered this issue, and have added a PR to fix in #710.

CANDIDATE · PULL REQUEST

logging.py: CPython-compatible logging improvements.

openby ned-pcsopened 2024-02-15updated 2024-03-21

This PR allows for logging handlers to be added to the root logger and then used by non-root loggers that don't have their own handlers.

It replaces #750 (in which I forgot a period at the end of a commit message title).

It also adds the (CPython-compatible) handlers argument to logging.basicConfig() to ease this initialization:

    import logging
    sh = logging.StreamHandler()
    fh = logging.FileHandler("my.log", mode="a")
    logging.basicConfig(handlers=[sh, fh])

    root_logger = logging.getLogger() # uses sh and fh
    another_logger = logging.getLogger("another") # inherits handlers

It also adds the Logger.removeHandler() method and avoids repeated handler addition.

It also adds the flush() method to StreamHandler and its subclasses.

It also correctly calls the superclass constructor from the StreamHandler constructor and uses a default formatter if a Handler has none set (as in PR #710).

2 comments
ned-pcs · 2024-02-15

build failure is due to an unrelated manifest.py issue.

projectgus · 2024-02-19

@ned-pcs If you rebase on master then the build should be fixed now.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied