Skip to content

Commit

Permalink
Only propagate logger in pytest
Browse files Browse the repository at this point in the history
Pytest hooks into the root logger, which breaks the use case here where
we only want a single handler to emit a log record. Create a special
carve out for pytest and put it in `get_script_logger` to make testing
logging results of any function easy.
  • Loading branch information
TimothyWillard committed Jan 7, 2025
1 parent 71e2f47 commit e090873
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
5 changes: 4 additions & 1 deletion flepimop/gempyor_pkg/src/gempyor/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@


import logging
import os
import sys
from typing import Any, IO

import click
Expand Down Expand Up @@ -119,7 +121,8 @@ def get_script_logger(
logger.removeHandler(old_handler)
handler.setFormatter(log_formatter)
logger.addHandler(handler)
logger.propagate = False
# pytest-dev/pytest#3697
logger.propagate = os.path.basename(sys.argv[0]) == "pytest" if sys.argv else False
return logger


Expand Down
3 changes: 0 additions & 3 deletions flepimop/gempyor_pkg/tests/logging/test_get_script_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ def test_get_script_logger(
name, verbosity, handler=handler, log_format=log_format
)

# pytest-dev/pytest#3697
logger.propagate = True

assert isinstance(logger, logging.Logger)
assert logger.name == name
assert logger.level == _get_logging_level(verbosity)
Expand Down

0 comments on commit e090873

Please sign in to comment.