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()