Clean up sanic logging

This commit is contained in:
Paul Bienkowski 2022-09-16 11:47:15 +02:00
parent 78dca1477c
commit b9aaf23e0a
2 changed files with 27 additions and 2 deletions

View file

@ -26,11 +26,36 @@ from sqlalchemy.util import asyncio
log = logging.getLogger(__name__)
class SanicAccessMessageFilter(logging.Filter):
"""
A filter that modifies the log message of a sanic.access log entry to
include useful information.
"""
def filter(self, record):
record.msg = f"{record.request} -> {record.status}"
return True
def configure_sanic_logging():
for logger_name in ["sanic.root", "sanic.access", "sanic.error"]:
logger = logging.getLogger(logger_name)
for handler in logger.handlers:
logger.removeHandler(handler)
logger = logging.getLogger("sanic.access")
for filter_ in logger.filters:
logger.removeFilter(filter_)
logger.addFilter(SanicAccessMessageFilter())
logging.getLogger("sanic.root").setLevel(logging.WARNING)
app = Sanic(
"openbikesensor-api",
env_prefix="OBS_",
log_config={},
)
configure_sanic_logging()
if isfile("./config.py"):
app.update_config("./config.py")

View file

@ -58,7 +58,7 @@ def main():
port=app.config.PORT,
debug=debug,
auto_reload=app.config.get("AUTO_RELOAD", debug),
# access_log=False,
access_log=True,
)