From 4ec6a055d96ad09b203afdca20692ae4e839ff07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Thu, 12 Oct 2023 11:24:05 +0200 Subject: [PATCH] feat: add file logging (#2023) logs into `lnbits.log` with info loglevel and `debug.log` with DEBUG loglevel files will be saved into `LNBITS_DATA_DIR`. retentions is configurable but defaults to `90 days` * added log rotation aswell --------- Co-authored-by: Vlad Stan --- .env.example | 7 +++++++ lnbits/app.py | 17 +++++++++++++++++ lnbits/server.py | 1 + lnbits/settings.py | 3 +++ 4 files changed, 28 insertions(+) diff --git a/.env.example b/.env.example index 7d6716f06..0bdb78e23 100644 --- a/.env.example +++ b/.env.example @@ -8,6 +8,13 @@ PORT=5000 DEBUG=false +# logging into LNBITS_DATA_FOLDER/logs/ +ENABLE_LOG_TO_FILE=true + +# https://loguru.readthedocs.io/en/stable/api/logger.html#file +LOG_ROTATION="100 MB" +LOG_RETENTION="3 months" + # Server security, rate limiting ips, blocked ips, allowed ips LNBITS_RATE_LIMIT_NO="200" LNBITS_RATE_LIMIT_UNIT="minute" diff --git a/lnbits/app.py b/lnbits/app.py index 9112aebf3..a5ef2ee8f 100644 --- a/lnbits/app.py +++ b/lnbits/app.py @@ -526,6 +526,23 @@ def configure_logger() -> None: log_level: str = "DEBUG" if settings.debug else "INFO" formatter = Formatter() logger.add(sys.stdout, level=log_level, format=formatter.format) + + if settings.enable_log_to_file: + logger.add( + Path(settings.lnbits_data_folder, "logs", "lnbits.log"), + rotation=settings.log_rotation, + retention=settings.log_retention, + level="INFO", + format=formatter.format, + ) + logger.add( + Path(settings.lnbits_data_folder, "logs", "debug.log"), + rotation=settings.log_rotation, + retention=settings.log_retention, + level="DEBUG", + format=formatter.format, + ) + logging.getLogger("uvicorn").handlers = [InterceptHandler()] logging.getLogger("uvicorn.access").handlers = [InterceptHandler()] logging.getLogger("uvicorn.error").handlers = [InterceptHandler()] diff --git a/lnbits/server.py b/lnbits/server.py index 34e088b64..9f85abb50 100644 --- a/lnbits/server.py +++ b/lnbits/server.py @@ -37,6 +37,7 @@ def main( # create data dir if it does not exist Path(settings.lnbits_data_folder).mkdir(parents=True, exist_ok=True) + Path(settings.lnbits_data_folder, "logs").mkdir(parents=True, exist_ok=True) # create `extensions`` dir if it does not exist Path(settings.lnbits_extensions_path, "extensions").mkdir( diff --git a/lnbits/settings.py b/lnbits/settings.py index e671fbbd5..45e0d6374 100644 --- a/lnbits/settings.py +++ b/lnbits/settings.py @@ -296,6 +296,9 @@ class EnvSettings(LNbitsSettings): lnbits_extensions_path: str = Field(default="lnbits") super_user: str = Field(default="") version: str = Field(default="0.0.0") + enable_log_to_file: bool = Field(default=True) + log_rotation: str = Field(default="100 MB") + log_retention: str = Field(default="3 months") @property def has_default_extension_path(self) -> bool: