← index #691Issue #928
Related · high · value 0.764
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 · ISSUE

shutdown function of logging module does not empty _loggers

openby xllaopened 2024-10-07updated 2024-10-07

if I want to reuse logger after call shutdown(), I can recreate log_a , add file_handler , but it will failure on log.xxx

<pre>
Traceback (most recent call last):
...
File "logging.py", line 141, in info
File "logging.py", line 135, in log
File "logging.py", line 71, in emit
ValueError:
</pre>

the root cause is, after execute logging.shutdown() , all logger remain in _loggers variables, so all handlers remain in logger.handlers, but the old file_handlers was closed, so emit method failure.

we can run follow code to verify it.

<pre>

import logging
log_a = logging.getLogger('a')
log_a.setLevel(logging.DEBUG)

file_handler = logging.FileHandler('log.txt')
file_handler.setLevel(logging.DEBUG)

formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
file_handler.setFormatter(formatter)
log_a.addHandler(file_handler)

log_a.debug('write to file')

print(logging._loggers)
print(log_a.handlers)
logging.shutdown()

log_a = logging.getLogger('a')
log_a.setLevel(logging.DEBUG)

file_handler = logging.FileHandler('log.txt')
file_handler.setLevel(logging.DEBUG)

formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
file_handler.setFormatter(formatter)
log_a.addHandler(file_handler)

log_a.debug('write to file')

print(logging._loggers)
print(log_a.handlers)

</pre>

to fixed it, pass name to pop method in line 213

logging._loggers.pop(logger.name, None)

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