Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.13.1
3.13
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
]
Expand Down
8 changes: 6 additions & 2 deletions src/nypl_py_utils/functions/log_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_log_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Loading