diff --git a/pyproject.toml b/pyproject.toml index b5d3445..46f89b2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,7 @@ license = "AGPL-3.0-only" dependencies = [ "flask", "gunicorn", + "structlog", "pymongo", "scrypt", "pyjwt", @@ -29,3 +30,6 @@ packages = ["roc_fnb"] [project.scripts] # Put scripts here bootstrap-first-admin = "roc_fnb.scripts.bootstrap_first_admin:bootstrap_first_admin" + +[tool.yapf] +based_on_style = "facebook" diff --git a/roc_fnb/util/__init__.py b/roc_fnb/util/__init__.py index e69de29..0be7677 100644 --- a/roc_fnb/util/__init__.py +++ b/roc_fnb/util/__init__.py @@ -0,0 +1 @@ +from roc_fnb.util.logging import log \ No newline at end of file diff --git a/roc_fnb/util/logging.py b/roc_fnb/util/logging.py new file mode 100644 index 0000000..b98508a --- /dev/null +++ b/roc_fnb/util/logging.py @@ -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()