diff --git a/src/main/settings.py b/src/main/settings.py index 0329534..c94fed5 100644 --- a/src/main/settings.py +++ b/src/main/settings.py @@ -1,7 +1,9 @@ +import json import os import sys from corsheaders.defaults import default_headers +from opencensus.trace import config_integration from .azure_settings import Azure @@ -157,6 +159,10 @@ STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, "..", "static")) # Django Logging settings +base_log_fmt = {"time": "%(asctime)s", "name": "%(name)s", "level": "%(levelname)s"} +log_fmt = base_log_fmt.copy() +log_fmt["message"] = "%(message)s" + LOGGING = { "version": 1, "disable_existing_loggers": False, @@ -165,25 +171,35 @@ "handlers": ["console"], }, "formatters": { - "console": {"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"}, + "json": {"format": json.dumps(log_fmt)}, }, "handlers": { "console": { "level": "INFO", "class": "logging.StreamHandler", - "formatter": "console", + "formatter": "json", }, }, "loggers": { - "bouwdossiers-metadata-server": { + "bag": { + "level": "WARNING", + "handlers": ["console"], + "propagate": False, + }, + "bouwdossiers": { + "level": "WARNING", + "handlers": ["console"], + "propagate": False, + }, + "importer": { "level": "WARNING", "handlers": ["console"], - "propagate": True, + "propagate": False, }, "main": { "level": "WARNING", "handlers": ["console"], - "propagate": True, + "propagate": False, }, "django": { "handlers": ["console"], @@ -198,9 +214,45 @@ "handlers": ["console"], "propagate": False, }, + "opencensus": { + "handlers": ["console"], + "level": "WARNING", + "propagate": False + }, + "azure.core.pipeline.policies.http_logging_policy": { + "handlers": ["console"], + "level": "WARNING", + "propagate": False, + }, }, } +APPLICATIONINSIGHTS_CONNECTION_STRING = os.getenv( + "APPLICATIONINSIGHTS_CONNECTION_STRING" +) + +if APPLICATIONINSIGHTS_CONNECTION_STRING: + MIDDLEWARE.append("opencensus.ext.django.middleware.OpencensusMiddleware") + OPENCENSUS = { + "TRACE": { + "SAMPLER": "opencensus.trace.samplers.ProbabilitySampler(rate=1)", + "EXPORTER": f"""opencensus.ext.azure.trace_exporter.AzureExporter( + connection_string='{APPLICATIONINSIGHTS_CONNECTION_STRING}', + service_name='app-iiif-metadata-server' + )""", + } + } + config_integration.trace_integrations(["logging"]) + LOGGING["handlers"]["azure"] = { + "level": "DEBUG", + "class": "opencensus.ext.azure.log_exporter.AzureLogHandler", + "connection_string": APPLICATIONINSIGHTS_CONNECTION_STRING, + "formatter": "json" + } + LOGGING["root"]["handlers"].append("azure") + for logger_name, logger_details in LOGGING["loggers"].items(): + LOGGING["loggers"][logger_name]["handlers"].append("azure") + OBJECTSTORE = dict( VERSION="2.0", diff --git a/src/main/uwsgi.ini b/src/main/uwsgi.ini index 8a62d27..0aae00c 100644 --- a/src/main/uwsgi.ini +++ b/src/main/uwsgi.ini @@ -14,4 +14,6 @@ wsgi-file = main/wsgi.py route = /iiif-metadata/static/(.*) static:/static/$1 buffer-size = 32768 -harakiri = 30 \ No newline at end of file +harakiri = 30 + +lazy-apps = true \ No newline at end of file