Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shutdown function of logging module does not empty _loggers #928

Open
xlla opened this issue Oct 7, 2024 · 0 comments
Open

shutdown function of logging module does not empty _loggers #928

xlla opened this issue Oct 7, 2024 · 0 comments

Comments

@xlla
Copy link

xlla commented Oct 7, 2024

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

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: 

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.

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)

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

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

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

No branches or pull requests

1 participant