diff --git a/giges/app.py b/giges/app.py index 2181b43..dcf98d5 100644 --- a/giges/app.py +++ b/giges/app.py @@ -1,7 +1,10 @@ +import logging import os +import sys from typing import Any, Optional import connexion +import structlog from connexion.apps.flask_app import FlaskApp from connexion.resolver import RestyResolver from flask import Flask @@ -16,6 +19,30 @@ def create_app(self) -> Flask: return app +def configure_logging() -> None: + structlog.configure( + processors=[ + structlog.stdlib.filter_by_level, + structlog.processors.TimeStamper(fmt="iso"), + structlog.stdlib.add_logger_name, + structlog.stdlib.add_log_level, + structlog.stdlib.PositionalArgumentsFormatter(), + structlog.processors.StackInfoRenderer(), + structlog.processors.format_exc_info, + structlog.processors.UnicodeDecoder(), + structlog.processors.JSONRenderer(), + ], + context_class=structlog.threadlocal.wrap_dict(dict), + logger_factory=structlog.stdlib.LoggerFactory(), + wrapper_class=structlog.stdlib.BoundLogger, + cache_logger_on_first_use=True, + ) + + logging.basicConfig( + format="%(message)s", stream=sys.stdout, level=logging.INFO + ) + + def create_connexion_app( settings_object: Optional[Any] = None, **kwargs: int ) -> connexion.App: @@ -48,6 +75,8 @@ def create_connexion_app( Migrate(flask_app, db) + configure_logging() + return connexion_app diff --git a/giges/handlers/asana.py b/giges/handlers/asana.py index 228235e..508227c 100644 --- a/giges/handlers/asana.py +++ b/giges/handlers/asana.py @@ -1,8 +1,10 @@ -import logging from typing import Dict, Tuple, Union +import structlog from connexion import request +logger = structlog.get_logger(__name__) + def projects() -> Union[Tuple[Dict, int], Tuple[Dict, int, Dict[str, str]]]: """ @@ -24,5 +26,5 @@ def projects() -> Union[Tuple[Dict, int], Tuple[Dict, int, Dict[str, str]]]: if request.headers.get("X-Hook-Secret"): return {}, 204, {"X-Hook-Secret": request.headers["X-Hook-Secret"]} - logging.info(request.json["events"]) + logger.info(request.json["events"]) return {}, 204 diff --git a/requirements.in b/requirements.in index 03f1a99..1b0fabd 100644 --- a/requirements.in +++ b/requirements.in @@ -4,6 +4,7 @@ flask-sqlalchemy psycopg2 zappa sqlalchemy +structlog # All the package restrictions are due to zappa :( troposphere<3 diff --git a/requirements.txt b/requirements.txt index 16f560d..4025cc9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,11 +10,11 @@ argcomplete==1.12.3 # via zappa attrs==21.2.0 # via jsonschema -boto3==1.18.14 +boto3==1.18.22 # via # kappa # zappa -botocore==1.21.14 +botocore==1.21.22 # via # boto3 # s3transfer @@ -53,7 +53,7 @@ flask-sqlalchemy==2.5.1 # flask-migrate future==0.18.2 # via zappa -greenlet==1.1.0 +greenlet==1.1.1 # via sqlalchemy hjson==3.0.2 # via zappa @@ -138,15 +138,17 @@ sqlalchemy==1.4.22 # alembic # dictalchemy # flask-sqlalchemy +structlog==21.1.0 + # via -r requirements.in swagger-ui-bundle==0.0.8 # via connexion text-unidecode==1.3 # via python-slugify toml==0.10.2 # via zappa -tomli==1.2.0 +tomli==1.2.1 # via pep517 -tqdm==4.62.0 +tqdm==4.62.1 # via zappa troposphere==2.7.1 # via @@ -160,7 +162,7 @@ werkzeug==0.16.1 # via # flask # zappa -wheel==0.36.2 +wheel==0.37.0 # via # pip-tools # zappa