From 161e0e9628ac6c46c790c56726c635d8f6441dae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Sun, 4 Sep 2022 23:25:40 +0000 Subject: [PATCH] logging setup --- api/ovdashboard_api/__init__.py | 5 +++++ api/ovdashboard_api/config.py | 31 +++++++++++++++++++++++++++++ api/ovdashboard_api/dav_calendar.py | 3 +-- api/ovdashboard_api/dav_file.py | 3 +-- 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/api/ovdashboard_api/__init__.py b/api/ovdashboard_api/__init__.py index e69de29..5780659 100644 --- a/api/ovdashboard_api/__init__.py +++ b/api/ovdashboard_api/__init__.py @@ -0,0 +1,5 @@ +import logging.config + +from .config import LogConfig + +logging.config.dictConfig(LogConfig().dict()) diff --git a/api/ovdashboard_api/config.py b/api/ovdashboard_api/config.py index b4c1bc3..08a3638 100644 --- a/api/ovdashboard_api/config.py +++ b/api/ovdashboard_api/config.py @@ -11,6 +11,37 @@ from .dav_common import webdav_resource from .dav_file import DavFile +class LogConfig(BaseModel): + """ + https://stackoverflow.com/a/67937084 + Logging configuration to be set for the server + """ + + LOG_FORMAT: str = "%(levelprefix)s [%(asctime)s] %(name)s: %(message)s" + LOG_LEVEL: str = "DEBUG" + + # Logging config + version = 1 + disable_existing_loggers = False + formatters = { + "default": { + "()": "uvicorn.logging.DefaultFormatter", + "fmt": LOG_FORMAT, + "datefmt": "%Y-%m-%d %H:%M:%S", + }, + } + handlers = { + "default": { + "formatter": "default", + "class": "logging.StreamHandler", + "stream": "ext://sys.stderr", + }, + } + loggers = { + "ovdashboard_api": {"handlers": ["default"], "level": LOG_LEVEL}, + } + + class ImageConfig(BaseModel): mode: str = "RGB" save_params: dict[str, Any] = { diff --git a/api/ovdashboard_api/dav_calendar.py b/api/ovdashboard_api/dav_calendar.py index 8649665..a4f8566 100644 --- a/api/ovdashboard_api/dav_calendar.py +++ b/api/ovdashboard_api/dav_calendar.py @@ -78,8 +78,7 @@ async def _get_calendar_events( @run_in_executor def _inner() -> Iterator[VEvent]: - _logger.info(f"updating {calendar_name}") - print(f"updating {calendar_name}") + _logger.info(f"updating {calendar_name!r} ...") calendar = caldav_principal().calendar(calendar_name) diff --git a/api/ovdashboard_api/dav_file.py b/api/ovdashboard_api/dav_file.py index 28f59ff..f647a01 100644 --- a/api/ovdashboard_api/dav_file.py +++ b/api/ovdashboard_api/dav_file.py @@ -18,8 +18,7 @@ async def _get_buffer( @run_in_executor def _inner(resource: Resource) -> BytesIO: - _logger.info(f"updating {resource}") - print(f"updating {resource}") + _logger.info(f"updating {resource.urn.filename()!r} ...") buffer = BytesIO() resource.write_to(buffer) return buffer