Created
August 17, 2023 20:41
-
-
Save davetapley/e8d301d9c92fdb950d38ece367f8c5f8 to your computer and use it in GitHub Desktop.
NotADirectoryError: [WinError 267] The directory name is invalid unless explicit log close
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from logging.config import dictConfig | |
from logging import getLogger | |
from pathlib import Path | |
from tempfile import TemporaryDirectory | |
from threading import Thread | |
def config(path: Path): | |
return { | |
'version': 1, | |
'handlers': { | |
'file': { | |
'class': 'logging.handlers.TimedRotatingFileHandler', | |
'level': 'DEBUG', | |
'filename': path, | |
'when': 'midnight', | |
}, | |
}, | |
'root': { | |
'level': 'DEBUG', | |
'handlers': ['file',], | |
}, | |
'disable_existing_loggers': False | |
} | |
def run(path: Path): | |
dictConfig(config(path)) | |
logger = getLogger() | |
logger.info("Hello, world!") | |
# NotADirectoryError: [WinError 267] The directory name is invalid | |
# unless you: | |
# for h in logger.handlers: | |
# if isinstance(h, TimedRotatingFileHandler): | |
# h.close() | |
if __name__ == "__main__": | |
dir = TemporaryDirectory() | |
path = Path(dir.name) / "log.txt" | |
t = Thread(target=run, args=(path,)) | |
t.start() | |
t.join() | |
dir.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment