diff --git a/.python-version b/.python-version index c10780c..24ee5b1 100644 --- a/.python-version +++ b/.python-version @@ -1 +1 @@ -3.13.1 +3.13 diff --git a/CHANGELOG.md b/CHANGELOG.md index ae38c6a..c3b60cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ # Changelog +## v1.10.2 4/27/26 +- Prevent double logging in pytest using structlog + ## v1.10.1 4/14/26 - Add optional timeout param for OAuth2Client diff --git a/pyproject.toml b/pyproject.toml index c7007e0..010f833 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "nypl_py_utils" -version = "1.10.1" +version = "1.10.2" authors = [ { name="Aaron Friedman", email="aaronfriedman@nypl.org" }, ] diff --git a/src/nypl_py_utils/functions/log_helper.py b/src/nypl_py_utils/functions/log_helper.py index f5fc16e..c298eb8 100644 --- a/src/nypl_py_utils/functions/log_helper.py +++ b/src/nypl_py_utils/functions/log_helper.py @@ -28,9 +28,13 @@ def get_structlog(module): without binding it to others, use `logger = logger.bind(contextvar=0)`. """ logger = logging.getLogger(module) - logger.addHandler(logging.StreamHandler(sys.stdout)) logger.setLevel(os.environ.get('LOG_LEVEL', 'INFO').upper()) - logger.propagate = False # Prevents double logging + + # We assume existing loggers will handle logging to stdout and that we + # don't need to do anything. Otherwise, manually add a stdout handler. + if not logger.hasHandlers(): + logger.propagate = False + logger.addHandler(logging.StreamHandler(sys.stdout)) return structlog.wrap_logger( logger, diff --git a/tests/test_log_helper.py b/tests/test_log_helper.py index 57d7bce..55f76bb 100644 --- a/tests/test_log_helper.py +++ b/tests/test_log_helper.py @@ -10,10 +10,10 @@ @freeze_time('2023-01-01 19:00:00') class TestLogHelper: - def test_json_logging(self, capsys): + def test_json_logging(self, caplog): logger = create_log('test_structlog', json=True) logger.info('test', some="json") - output = json.loads(capsys.readouterr().out) + output = json.loads(caplog.records[0].msg) assert output.get("message") == 'test' assert output.get("some") == 'json' assert output.get('level') == 'info'