added console logging

This commit is contained in:
2026-08-30 12:11:57 +02:00
parent c1d731f85d
commit bb80bb953c
4 changed files with 18 additions and 4 deletions
+1
View File
@@ -18,6 +18,7 @@ LOG_FILE=/var/lib/rrd/netatmo_service.log
LOG_LEVEL=INFO LOG_LEVEL=INFO
LOG_MAX_BYTES=5242880 LOG_MAX_BYTES=5242880
LOG_BACKUP_COUNT=5 LOG_BACKUP_COUNT=5
LOG_CONSOLE=true
POLL_INTERVAL=600 POLL_INTERVAL=600
START_COLLECTOR=true START_COLLECTOR=true
+3 -1
View File
@@ -79,7 +79,9 @@ open <http://localhost:5000/w/> instead.
failure, HTTP status, endpoint, and duration without credentials. Collector, failure, HTTP status, endpoint, and duration without credentials. Collector,
Flask, uncaught main-thread, and uncaught worker-thread exceptions are written to 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 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 ## HTTP API
+13 -3
View File
@@ -35,15 +35,16 @@ class PrefixMiddleware:
def configure_logging(app: Flask) -> None: def configure_logging(app: Flask) -> None:
log_path = app.config["LOG_FILE"] log_path = app.config["LOG_FILE"]
log_path.parent.mkdir(parents=True, exist_ok=True) log_path.parent.mkdir(parents=True, exist_ok=True)
formatter = logging.Formatter(
"%(asctime)s %(levelname)s %(name)s [%(threadName)s] %(message)s"
)
handler = RotatingFileHandler( handler = RotatingFileHandler(
log_path, log_path,
maxBytes=app.config["LOG_MAX_BYTES"], maxBytes=app.config["LOG_MAX_BYTES"],
backupCount=app.config["LOG_BACKUP_COUNT"], backupCount=app.config["LOG_BACKUP_COUNT"],
encoding="utf-8", encoding="utf-8",
) )
handler.setFormatter(logging.Formatter( handler.setFormatter(formatter)
"%(asctime)s %(levelname)s %(name)s [%(threadName)s] %(message)s"
))
root = logging.getLogger() root = logging.getLogger()
root.setLevel(app.config["LOG_LEVEL"]) root.setLevel(app.config["LOG_LEVEL"])
# Avoid duplicate handlers when an app factory is called repeatedly in tests. # Avoid duplicate handlers when an app factory is called repeatedly in tests.
@@ -57,6 +58,15 @@ def configure_logging(app: Flask) -> None:
else: else:
handler.close() 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): def uncaught_exception(exception_type, exception, traceback):
if issubclass(exception_type, KeyboardInterrupt): if issubclass(exception_type, KeyboardInterrupt):
return sys.__excepthook__(exception_type, exception, traceback) return sys.__excepthook__(exception_type, exception, traceback)
+1
View File
@@ -25,6 +25,7 @@ class Config:
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper() LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
LOG_MAX_BYTES = int(os.getenv("LOG_MAX_BYTES", str(5 * 1024 * 1024))) LOG_MAX_BYTES = int(os.getenv("LOG_MAX_BYTES", str(5 * 1024 * 1024)))
LOG_BACKUP_COUNT = int(os.getenv("LOG_BACKUP_COUNT", "5")) LOG_BACKUP_COUNT = int(os.getenv("LOG_BACKUP_COUNT", "5"))
LOG_CONSOLE = _bool("LOG_CONSOLE", True)
NETATMO_TOKEN_FILE = Path( NETATMO_TOKEN_FILE = Path(
os.getenv("NETATMO_TOKEN_FILE", str(RRD_FOLDER / "netatmo_tokens.json")) os.getenv("NETATMO_TOKEN_FILE", str(RRD_FOLDER / "netatmo_tokens.json"))
).expanduser().resolve() ).expanduser().resolve()