Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't propagate named loggers. #32

Merged
merged 3 commits into from
May 2, 2024
Merged

Conversation

JoeZiminski
Copy link
Member

@JoeZiminski JoeZiminski commented May 1, 2024

This PR sets propagate=False to any created name logger. Due to the insane way that python's logging module works it means that messages are printed to stdout even if log_to_console=False.

This is because, as I understand it, all named loggers are created with root as their 'parent'. By default all child logger messages are propagated to their parent handlers. Therefore if the root logger is used, a StreamHandler is set on it and then the named logger starts logging to console. For example a MRE:

import logging

OUTPUT_FILEPATH = r"C:\Users\Joe\test_log1.log"

fh = logging.FileHandler(OUTPUT_FILEPATH, encoding="utf-8")
fh.setLevel("DEBUG")

logger = logging.getLogger("my_logger")
logger.setLevel("DEBUG")

logger.info("hello world 1")  # does not print to console as expected

# This seems to initialise a StreamHandler on the root
# logger, to which all child loggers are propagated to.
logging.info("log something on root")

logger.info("hello world 2")  # this is now printed to console

The tests are currently failing due to an issue with handlers that will be addressed in #33.

@JoeZiminski JoeZiminski requested a review from adamltyson May 1, 2024 11:39
@JoeZiminski JoeZiminski force-pushed the dont_propagate_named_loggers branch from 3202957 to 768d8c6 Compare May 1, 2024 17:42
@JoeZiminski
Copy link
Member Author

Rebased onto main and tests passing now, ready for review @adamltyson

@adamltyson adamltyson merged commit a860778 into master May 2, 2024
19 checks passed
@adamltyson adamltyson deleted the dont_propagate_named_loggers branch May 2, 2024 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants