Skip to content

Commit 1e93358

Browse files
optional json struct log
1 parent ffa087a commit 1e93358

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

pyproject.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "nypl_py_utils"
7-
version = "1.7.0"
7+
version = "1.8.0"
88
authors = [
99
{ name="Aaron Friedman", email="aaronfriedman@nypl.org" },
1010
]
@@ -38,6 +38,9 @@ kms-client = [
3838
"boto3>=1.26.5",
3939
"botocore>=1.29.5"
4040
]
41+
log_helper = [
42+
"structlog>=25.4.0"
43+
]
4144
mysql-client = [
4245
"mysql-connector-python>=8.0.32"
4346
]
@@ -78,7 +81,7 @@ research-catalog-identifier-helper = [
7881
"requests>=2.28.1"
7982
]
8083
development = [
81-
"nypl_py_utils[avro-client,kinesis-client,kms-client,mysql-client,oauth2-api-client,postgresql-client,redshift-client,s3-client,secrets-manager-client,sftp-client,config-helper,obfuscation-helper,patron-data-helper,research-catalog-identifier-helper]",
84+
"nypl_py_utils[avro-client,kinesis-client,kms-client,mysql-client,oauth2-api-client,postgresql-client,redshift-client,s3-client,secrets-manager-client,sftp-client,config-helper,obfuscation-helper,patron-data-helper,research-catalog-identifier-helper,log_helper]",
8285
"flake8>=6.0.0",
8386
"freezegun>=1.2.2",
8487
"mock>=4.0.3",

src/nypl_py_utils/functions/log_helper.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import structlog
2+
13
import logging
24
import os
35
import sys
@@ -10,8 +12,37 @@
1012
'critical': logging.CRITICAL
1113
}
1214

15+
# Configure structlog to be machine-readable first and foremost
16+
# while still making it easy for humans to parse
17+
# End result (without additional bindings) is JSON like this:
18+
#
19+
# { "logger": "module param"
20+
# "message": "this is a test log event",
21+
# "level": "info",
22+
# "timestamp": "2023-11-01 18:50:47"}
23+
#
24+
def get_structlog(module):
25+
structlog.configure(
26+
processors=[
27+
structlog.processors.add_log_level,
28+
structlog.processors.TimeStamper(fmt="iso"),
29+
structlog.processors.EventRenamer("message"),
30+
structlog.processors.JSONRenderer(),
31+
],
32+
context_class=dict,
33+
logger_factory=structlog.PrintLoggerFactory(),
34+
)
35+
36+
# Import this to get an immutable common logger with the above format- you can further use
37+
#
38+
# custom_logger = common_logger.bind(some_key=some_value)
39+
#
40+
# to create your own logger with custom fields persisted on top of common ones.
41+
#
42+
# See https://www.structlog.org/en/stable/bound-loggers.html
43+
return structlog.get_logger(module)
1344

14-
def create_log(module):
45+
def standard_logger (module):
1546
logger = logging.getLogger(module)
1647
if logger.hasHandlers():
1748
logger.handlers = []
@@ -28,5 +59,10 @@ def create_log(module):
2859
console_log.setFormatter(formatter)
2960

3061
logger.addHandler(console_log)
31-
3262
return logger
63+
64+
def create_log(module, json=False):
65+
if(json):
66+
return get_structlog()
67+
else:
68+
return standard_logger(module)

0 commit comments

Comments
 (0)