Repository navigation
Python 3.12 breaks backwards compatibility for logging configuration #111615
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Nov 1, 2023 - addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Nov 1, 2023 Thanks for the report. This is due to a fix for gh-93162 , which allows proper configuration of a
QueueListenerandQueueHandlertogether. I'm sorry about the breakage - it appears not enough consideration was given to configuring aQueuehandlerwithout configuring aQueueListener. This was of course the default case before the change went in, and the cookbook example needs to be updated. How are you configuring theQueueListenerin your environment?- added3.12only security fixesonly security fixes3.13only security fixesonly security fixes
on Nov 2, 2023 I'm using
QueueListener/QueueHandlerw/multiprocessingto centralize logging in the parent process logger, which I think is common advise on the net. The complete setup is:@contextmanager def configure_logging_in_child(queue): config = { 'version': 1, 'handlers': { 'sink': { 'class': 'logging.handlers.QueueHandler', 'queue': queue, }, }, 'root': { 'handlers': ['sink'], 'level': 'DEBUG', }, } logging.config.dictConfig(config) try: yield except Exception as e: logging.exception(e) def child_main(log_queue): with configure_logging_in_child(log_queue): # Do some actual work. ... def main(): ... ctx = multiprocessing.get_context('spawn') queue = ctx.Queue() listener = logging.handlers.QueueListener(queue, logging.getLogger()) listener.start() # There's actually a list of child processes. args = (queue,) child = ctx.Process(target=child_main, args=args) child.start() child.join() ...
To be honest, I don't really understand how the queue & listener can be configured in one call, since I configure them separately in the parent & child processes.
- added a commit that references this issue
on Nov 2, 2023 To be honest, I don't really understand how the queue & listener can be configured in one call
Well, they can be if you're using threads rather than processes.
Well, they can be if you're using threads rather than processes.
I understand. I cannot use Python threads though, since I need actual parallel execution (for a number of child processes).
I use a similar pattern in a couple of other projects, where I also must use multiprocessing, since the child process needs to drop permissions, execute as a different user, etc.
Well, the change in the linked PR should resolve this, but it will have to wait for the next release of Python 3.12.
Well, the change in the linked PR should resolve this, but it will have to wait for the next release of Python 3.12.
Thanks, sounds good!
- added a commit that references this issue
on Dec 27, 2023
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Bug report
Bug description:
This worked fine on previous versions:
With Python 3.12, it drops an error:
More than that, even the example in the Logging Cookbook is broken now: https://docs.python.org/3/howto/logging-cookbook.html#a-more-elaborate-multiprocessing-example (fails with the same error).
Version info:
CPython versions tested on:
3.12
Operating systems tested on:
Linux
Linked PRs