From bb80bb953c84779e5e4c6195043395ce1992f370 Mon Sep 17 00:00:00 2001 From: Ignace Date: Sun, 30 Aug 2026 12:11:57 +0200 Subject: [PATCH] added console logging --- .env.example | 1 + README.md | 4 +++- netatmo_service/app.py | 16 +++++++++++++--- netatmo_service/config.py | 1 + 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index adcb033..181e932 100644 --- a/.env.example +++ b/.env.example @@ -18,6 +18,7 @@ LOG_FILE=/var/lib/rrd/netatmo_service.log LOG_LEVEL=INFO LOG_MAX_BYTES=5242880 LOG_BACKUP_COUNT=5 +LOG_CONSOLE=true POLL_INTERVAL=600 START_COLLECTOR=true diff --git a/README.md b/README.md index a0803fa..aa608b2 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,9 @@ open instead. failure, HTTP status, endpoint, and duration without credentials. Collector, Flask, uncaught main-thread, and uncaught worker-thread exceptions are written to the same log. `LOG_MAX_BYTES` defaults to 5 MiB and `LOG_BACKUP_COUNT` to five. -The service account must have write permission on the log directory. +The service account must have write permission on the log directory. Console +logging remains enabled by default so `python wsgi.py` displays its listening +address; set `LOG_CONSOLE=false` to use only the logfile. ## HTTP API diff --git a/netatmo_service/app.py b/netatmo_service/app.py index fb7f61c..eb038e5 100644 --- a/netatmo_service/app.py +++ b/netatmo_service/app.py @@ -35,15 +35,16 @@ class PrefixMiddleware: def configure_logging(app: Flask) -> None: log_path = app.config["LOG_FILE"] log_path.parent.mkdir(parents=True, exist_ok=True) + formatter = logging.Formatter( + "%(asctime)s %(levelname)s %(name)s [%(threadName)s] %(message)s" + ) handler = RotatingFileHandler( log_path, maxBytes=app.config["LOG_MAX_BYTES"], backupCount=app.config["LOG_BACKUP_COUNT"], encoding="utf-8", ) - handler.setFormatter(logging.Formatter( - "%(asctime)s %(levelname)s %(name)s [%(threadName)s] %(message)s" - )) + handler.setFormatter(formatter) root = logging.getLogger() root.setLevel(app.config["LOG_LEVEL"]) # Avoid duplicate handlers when an app factory is called repeatedly in tests. @@ -57,6 +58,15 @@ def configure_logging(app: Flask) -> None: else: handler.close() + if app.config["LOG_CONSOLE"] and not any( + isinstance(item, logging.StreamHandler) + and not isinstance(item, logging.FileHandler) + for item in root.handlers + ): + console = logging.StreamHandler() + console.setFormatter(formatter) + root.addHandler(console) + def uncaught_exception(exception_type, exception, traceback): if issubclass(exception_type, KeyboardInterrupt): return sys.__excepthook__(exception_type, exception, traceback) diff --git a/netatmo_service/config.py b/netatmo_service/config.py index b51dfde..dd4f6e8 100644 --- a/netatmo_service/config.py +++ b/netatmo_service/config.py @@ -25,6 +25,7 @@ class Config: LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper() LOG_MAX_BYTES = int(os.getenv("LOG_MAX_BYTES", str(5 * 1024 * 1024))) LOG_BACKUP_COUNT = int(os.getenv("LOG_BACKUP_COUNT", "5")) + LOG_CONSOLE = _bool("LOG_CONSOLE", True) NETATMO_TOKEN_FILE = Path( os.getenv("NETATMO_TOKEN_FILE", str(RRD_FOLDER / "netatmo_tokens.json")) ).expanduser().resolve()