Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HDX-10202 set log level from env vars #233

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docker/hapi_run
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
39 changes: 39 additions & 0 deletions docker/logging.conf.tpl
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import logging
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
Expand Down Expand Up @@ -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'))
Loading