Skip to content

logging.handlers.MemoryHandler seems to be unconditionally flushed on process exit #95804

Closed
@rascalking

Description

@rascalking

Bug report

The documentation for MemoryHandler claims: If flushOnClose is specified as False, then the buffer is not flushed when the handler is closed.

However, as this minimal test shows, it's not always respected. Running this snippet does print "this should not print to the console" to the console.

import logging, logging.handlers
logging.root.addHandler(
    logging.handlers.MemoryHandler(
        capacity=10,
        flushLevel=logging.ERROR,
        target=logging.StreamHandler(),
        flushOnClose=False))
logging.warning('this should not print to the console')

It looks like it's the shutdown handler in Lib/logging/__init__.py that's doing the flushing.

If you explicitly close the handler before the process exits, you get the expected behavior...the log message isn't printed to the console. Which is why the unit test passes, that's what it's testing.

Your environment

I initially noticed this in python 3.8.13 on MacOS 12.5 (21G72). I can repro in the other python versions I have installed on that same macbook, 3.9.2 and 3.10.4.

Metadata

Metadata

Assignees

Labels

stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions