From fa952000cce935b9319731130cb835e515e7bbfa Mon Sep 17 00:00:00 2001 From: Stefan Hynek <stefan.hynek@uni-goettingen.de> Date: Thu, 11 May 2023 13:00:45 +0200 Subject: [PATCH 1/3] fix(config): make logging logging configurable with REPDAV_LOG_LEVEL environment variable also, add SENTRY_ENV for configuring the sentry environment; refactor AppConfig class for more failsafety; rename TEXTGRID_HOST to TEXTGRID_URL close #10 --- src/main.py | 14 +++++++----- src/repdav/config.py | 52 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/src/main.py b/src/main.py index 3bf7cd8..87411da 100644 --- a/src/main.py +++ b/src/main.py @@ -12,18 +12,22 @@ from repdav.config import AppConfig app_config = AppConfig() if app_config.dsn: - sentry_sdk.init(app_config.dsn, traces_sample_rate=1.0) + sentry_sdk.init( + dsn=app_config.dsn, + environment=app_config.environment, + traces_sample_rate=1.0, + ) logging.basicConfig( - level=logging.DEBUG, format="%(name)s %(levelname)s %(asctime)s %(message)s" + level=app_config.log_level, format="%(name)s %(levelname)s %(asctime)s %(message)s" ) _logger = logging.getLogger(__name__) _logger.propagate = True # configure the "wsgidav" named logger -tmp_logger = logging.getLogger("wsgidav") -tmp_logger.propagate = True -tmp_logger.setLevel(logging.DEBUG) +wsgidav_logger = logging.getLogger("wsgidav") +wsgidav_logger.propagate = True +wsgidav_logger.setLevel(app_config.log_level) # Configuration of the WsgiDAVApp. # https://wsgidav.readthedocs.io/en/latest/user_guide_configure.html diff --git a/src/repdav/config.py b/src/repdav/config.py index b3329bf..6bb35a4 100644 --- a/src/repdav/config.py +++ b/src/repdav/config.py @@ -3,51 +3,79 @@ # SPDX-License-Identifier: CC0-1.0 import os +import logging from typing import Optional from urllib.parse import urlparse +_logger = logging.getLogger(__name__) -def check_url(url): + +def check_url(url: str): """Check if `url` can be parsed.""" - if url: - result = urlparse(url) - if result.scheme and result.netloc: - return url - raise ValueError(f"{url} is not a valid URL.") + result = urlparse(url) + if result.scheme and result.netloc: + return url + raise ValueError(f"{url} is not a valid URL.") -def check_port(port): +def check_port(port: int): """Check if port is set correctly to a non-privileged value.""" if 1000 <= port <= 65535: return port raise ValueError(f"{port} is not a valid Port.") +def check_log_level(level: str): + """Check if log level exists.""" + if level in logging._nameToLevel: # pylint: disable=protected-access + return level + _logger.warning("%s is not a valid Logging Level. Falling back to WARNING.", level) + return "WARNING" + + class AppConfig: """Settings that can be be configured from the environment.""" def __init__(self) -> None: - self._dsn = check_url(os.getenv("SENTRY_DSN")) - self._host = os.getenv("REPDAV_HOST") or "localhost" - self._port = check_port(int(os.getenv("REPDAV_PORT") or 8080)) - self._tg_host = check_url(os.getenv("TEXTGRID_HOST")) + _dsn = os.getenv("SENTRY_DSN") + self._dsn = check_url(_dsn) if _dsn else None + _environment = os.getenv("SENTRY_ENV") + self._environment = _environment if _environment else "production" + _host = os.getenv("REPDAV_HOST") + self._host = _host if _host else "localhost" + _log_level = os.getenv("REPDAV_LOG_LEVEL") + self._log_level = check_log_level(_log_level) if _log_level else "WARNING" + _port = os.getenv("REPDAV_PORT") + self._port = check_port(int(_port)) if _port else 8080 + _tg_host = os.getenv("TEXTGRID_URL") + self._tg_host = check_url(_tg_host) if _tg_host else "https://textgridlab.org" @property def dsn(self) -> Optional[str]: """Sentry DSN.""" return self._dsn + @property + def environment(self) -> str: + """Sentry Environment.""" + return self._environment + @property def host(self) -> str: """Service hostname.""" return self._host + @property + def log_level(self) -> str: + """Service log level.""" + return self._log_level + @property def port(self) -> int: """Service port.""" return self._port @property - def tg_host(self) -> Optional[str]: + def tg_host(self) -> str: """Textgrid host URL.""" return self._tg_host -- GitLab From 56ddf9eacf002fc52ed4679ea7f4302ed367e58d Mon Sep 17 00:00:00 2001 From: Stefan Hynek <stefan.hynek@uni-goettingen.de> Date: Thu, 11 May 2023 13:01:22 +0200 Subject: [PATCH 2/3] docs(readme): explain how to configure settings --- README.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c40ad6d..eafed1c 100644 --- a/README.md +++ b/README.md @@ -27,11 +27,16 @@ SPDX-License-Identifier: CC0-1.0 pip install -r requirements.txt -r requirements.dev.txt ``` -1. Configure the service by setting the following variables either in your environment or in the `docker-compose.yml` file. - - host - - port - - tg_auth_wsdl - - tg_auth_address +1. Configure the service by setting the following variables in your environment. + + Name | Usage / Default + ------|----------------- + **TEXTGRID_URL** | Textgrid repository service URL. Default: **<https://textgridlab.org>** + **REPDAV_HOST** | Service hostname. Default: **localhost** + **REPDAV_LOG_LEVEL** | Service logging level. Default: **WARNING** + **REPDAV_PORT** | Service port. Default: **8080** + **SENTRY_DSN** | Sentry Data Source Name for the Service. If unset, disables Sentry Error Tracking. Default: ***None*** + **SENTRY_ENV** | Sentry environment. Default: **production** 1. Start the service. -- GitLab From 2599b51cc8831f2ed748200ae979172477a2af57 Mon Sep 17 00:00:00 2001 From: Stefan Hynek <stefan.hynek@uni-goettingen.de> Date: Thu, 11 May 2023 13:01:59 +0200 Subject: [PATCH 3/3] build(compose): update environment variables --- docker-compose.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index d997a5d..3bb7fd6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,6 +16,7 @@ services: REPDAV_HOST: ${REPDAV_HOST:-0.0.0.0} REPDAV_PORT: ${REPDAV_PORT:-8080} SENTRY_DSN: ${SENTRY_DSN} - TEXTGRID_HOST: ${TEXTGRID_HOST} + TEXTGRID_URL: ${TEXTGRID_URL} + REPDAV_LOG_LEVEL: ${REPDAV_LOG_LEVEL} volumes: - $PWD/src:/app -- GitLab