add structured logging

This commit is contained in:
D. Scott Boggs 2025-05-31 09:10:17 -04:00
parent f7b7918660
commit ae5893c22f
3 changed files with 39 additions and 1 deletions

View file

@ -9,8 +9,15 @@ description = "Temporary placeholder for fnb web site"
readme = "README.md"
requires-python = ">=3.11" # Self type added
license = "AGPL-3.0-only"
dependencies = ["flask", "gunicorn"]
dependencies = [
"flask",
"gunicorn",
"structlog",
]
dynamic = ["version"]
[project.scripts]
# Put scripts here
[tool.yapf]
based_on_style = "facebook"

1
roc_fnb/util/__init__.py Normal file
View file

@ -0,0 +1 @@
from roc_fnb.util.logging import log

30
roc_fnb/util/logging.py Normal file
View file

@ -0,0 +1,30 @@
import logging
from os import environ
from structlog.processors import JSONRenderer, TimeStamper
from structlog.dev import ConsoleRenderer
import structlog
if not structlog.is_configured():
if (env := environ.get('ENV_MODE')) and env == 'production':
timestamper = TimeStamper(fmt="%Y-%m-%d %H:%M:%S", utc=True)
renderer: JSONRenderer | ConsoleRenderer = JSONRenderer()
else:
timestamper = TimeStamper(fmt="%Y-%m-%d %H:%M:%S", utc=False)
renderer = ConsoleRenderer()
structlog.configure(
processors=[
structlog.contextvars.merge_contextvars,
structlog.processors.add_log_level,
structlog.processors.StackInfoRenderer(),
structlog.dev.set_exc_info,
timestamper,
renderer,
],
wrapper_class=structlog.make_filtering_bound_logger(logging.NOTSET),
context_class=dict,
logger_factory=structlog.PrintLoggerFactory(),
cache_logger_on_first_use=False
)
log = structlog.get_logger()