micropython / micropython/micropython-lib

shutdown function of logging module does not empty _loggers

Open
#928 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.9k
Forks
1.1k
Avg merge
7d 6h
Merged PRs (30d)
3

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in logging.py around line 213 and reproduce the failure with the code in the issue. Check how logging.shutdown() handles _loggers and closed handlers, then verify that reusing the logger after shutdown can add a new FileHandler and emit a message without ValueError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.