Logging with loguru (#708)

* logging

* requirements

* add loguru dependency

* restore it

* add loguru

* set log level in .env file

* remove service fee print

* set log level

* more logging

* more logging

* more logging

* pyament.checking_id

* fix
This commit is contained in:
calle
2022-07-07 14:30:16 +02:00
committed by GitHub
parent 847fd18796
commit 089313f613
40 changed files with 202 additions and 81 deletions
+21 -8
View File
@@ -3,6 +3,9 @@ import importlib
import sys
import traceback
import warnings
from loguru import logger
from http import HTTPStatus
from fastapi import FastAPI, Request
@@ -41,6 +44,8 @@ def create_app(config_object="lnbits.settings") -> FastAPI:
"""Create application factory.
:param config_object: The configuration object to use.
"""
set_logging_level()
app = FastAPI()
app.mount("/static", StaticFiles(directory="lnbits/static"), name="static")
app.mount(
@@ -94,14 +99,14 @@ def check_funding_source(app: FastAPI) -> None:
error_message, balance = await WALLET.status()
if not error_message:
break
warnings.warn(
f" × The backend for {WALLET.__class__.__name__} isn't working properly: '{error_message}'",
logger.error(
f"The backend for {WALLET.__class__.__name__} isn't working properly: '{error_message}'",
RuntimeWarning,
)
print("Retrying connection to backend in 5 seconds...")
logger.info("Retrying connection to backend in 5 seconds...")
await asyncio.sleep(5)
print(
f" ✔️ {WALLET.__class__.__name__} seems to be connected and with a balance of {balance} msat."
logger.info(
f"✔️ Backend {WALLET.__class__.__name__} connected and with a balance of {balance} msat."
)
@@ -124,9 +129,10 @@ def register_routes(app: FastAPI) -> None:
for s in ext_statics:
app.mount(s["path"], s["app"], s["name"])
logger.trace(f"adding route for extension {ext_module}")
app.include_router(ext_route)
except Exception as e:
print(str(e))
logger.error(str(e))
raise ImportError(
f"Please make sure that the extension `{ext.code}` follows conventions."
)
@@ -173,8 +179,8 @@ def register_async_tasks(app):
def register_exception_handlers(app: FastAPI):
@app.exception_handler(Exception)
async def basic_error(request: Request, err):
print("handled error", traceback.format_exc())
print("ERROR:", err)
logger.error("handled error", traceback.format_exc())
logger.error("ERROR:", err)
etype, _, tb = sys.exc_info()
traceback.print_exception(etype, err, tb)
exc = traceback.format_exc()
@@ -188,3 +194,10 @@ def register_exception_handlers(app: FastAPI):
status_code=HTTPStatus.NO_CONTENT,
content={"detail": err},
)
def set_logging_level() -> None:
"""Set the logging level for the application."""
logger.remove()
log_level: str = "DEBUG" if lnbits.settings.DEBUG else "INFO"
logger.add(sys.stderr, level=log_level)