From 5da99bec71e1ee22e333f7d57c37fe6bb523bd3c Mon Sep 17 00:00:00 2001 From: Dan Mihaila Date: Tue, 15 Oct 2024 15:39:45 +0300 Subject: [PATCH 1/2] HDX-10202 set log level from env vars --- docker/hapi_run | 10 ++++++++++ docker/logging.conf.tpl | 39 +++++++++++++++++++++++++++++++++++++++ main.py | 4 ++-- 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 docker/logging.conf.tpl diff --git a/docker/hapi_run b/docker/hapi_run index 70cc019f..2393d073 100644 --- a/docker/hapi_run +++ b/docker/hapi_run @@ -19,6 +19,16 @@ else envsubst < /srv/hapi/docker/unit.json.tpl > /var/lib/unit/conf.json fi +# LOGGING_CONF_FILE needs to be set before substituting app.conf.tpl +export LOGGING_CONF_FILE=/srv/logging.conf + +# regenerate logging.conf +export LOG_LEVEL="${LOG_LEVEL:-INFO}" +[ ! -z "${LOG_LEVEL_CONSOLE}" ] || export LOG_LEVEL_CONSOLE=${LOG_LEVEL} +[ ! -z "${LOG_LEVEL_JSON}" ] || export LOG_LEVEL_JSON=${LOG_LEVEL} +[ ! -z "${LOG_LEVEL_TXT}" ] || export LOG_LEVEL_TXT=${LOG_LEVEL} +envsubst < /srv/hapi/docker/logging.conf.tpl > /srv/logging.conf + chmod 600 /var/lib/unit/conf.json chown -R unit /var/log/hapi diff --git a/docker/logging.conf.tpl b/docker/logging.conf.tpl new file mode 100644 index 00000000..48b7a66d --- /dev/null +++ b/docker/logging.conf.tpl @@ -0,0 +1,39 @@ +[loggers] +keys=root + +[handlers] +keys=consoleHandler, fileHandler, jsonFileHandler + +[formatters] +keys=simpleFormatter, jsonFormatter + +[logger_root] +level=${LOG_LEVEL} +handlers=consoleHandler, fileHandler, jsonFileHandler + + +[handler_consoleHandler] +class=StreamHandler +level=${LOG_LEVEL_CONSOLE} +formatter=simpleFormatter +args=(sys.stdout,) + +[handler_fileHandler] +class = FileHandler +args = ('/var/log/hapi/hapi.log','a') +level = ${LOG_LEVEL_TXT} +formatter = simpleFormatter + +[handler_jsonFileHandler] +class = FileHandler +args = ('/var/log/hapi/hapi-json.log','a') +level = ${LOG_LEVEL_JSON} +formatter = jsonFormatter + +[formatter_simpleFormatter] +format=[%(process)d - %(thread)d] %(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)d] %(message)s +datefmt= + +[formatter_jsonFormatter] +format = %(process)d %(thread)d %(asctime)s %(levelname) %(threadName)s %(name)s %(lineno)d %(message)s %(funcName)s +class = pythonjsonlogger.jsonlogger.JsonFormatter diff --git a/main.py b/main.py index 90da208d..f3b8cae5 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ import logging import logging.config +import os logging.config.fileConfig('logging.conf') @@ -153,6 +154,5 @@ def home(): async def resp_validation_exception_handler(request: Request, exc: ResponseValidationError): return await response_validation_error_handler(request, exc) - if __name__ == '__main__': - uvicorn.run(app, host='0.0.0.0', port=8844, log_config='logging.conf') + uvicorn.run(app, host='0.0.0.0', port=8844, log_config=os.getenv('LOGGING_CONF_FILE','logging.conf')) From 6acf04615914139962e080d562dc7974a64055ad Mon Sep 17 00:00:00 2001 From: Dan Mihaila Date: Mon, 21 Oct 2024 11:27:01 +0300 Subject: [PATCH 2/2] HDX-10202 set logging conf file path --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index f3b8cae5..aebfcb16 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,7 @@ import logging.config import os -logging.config.fileConfig('logging.conf') +logging.config.fileConfig(os.getenv('LOGGING_CONF_FILE','logging.conf')) import uvicorn # noqa from fastapi import FastAPI, Request # noqa