Skip to content

logging.config.dictConfig() with "disable_existing_loggers=False" closes existing handlers #127567

@aparshin

Description

@aparshin

Bug report

Bug description:

Invoking logging.config.dictConfig() with "disable_existing_loggers": False breaks already added handlers, even if the config is empty. Specifically, it calls flush() and close() of all existing handlers.

Test script:

import logging
import logging.config

# test setup: add a FileHandler as the only handler of the root logger
fileHandler = logging.FileHandler("./debug_log.txt", mode="w")
fileHandler.setFormatter(logging.Formatter("%(message)s"))
root_logger = logging.getLogger()
root_logger.addHandler(fileHandler)

# this works fine - the message is added to the file
logging.warning("test message 1")

# set a trivial configuration: no formatters, handlers or loggers.
# "disable_existing_loggers" is set to False
logging.config.dictConfig({"version": 1, "disable_existing_loggers": False})

# the FileHandler doesn't work anymore - this message won't be added to the file
logging.warning("test message 2")

# the handler is in inconsistent state - it's still added to the logger,
# but it's already flush()'ed and close()'d
assert fileHandler in root_logger.handlers
assert fileHandler._closed == True

The content of the debug_log.txt after running this script is the following:

test message 1

But it supposed to be

test message 1
test message 2

Maybe issue #123239 is somehow related.

CPython versions tested on:

3.13

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedstdlibPython modules in the Lib dir

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions