Added json logger
All checks were successful
Build & Publish to GHCR / build (push) Successful in 1m2s

This commit is contained in:
florian 2025-10-21 20:15:59 +02:00
parent 6abbddf4a6
commit 9f76821ca8
2 changed files with 16 additions and 4 deletions

View File

@ -17,6 +17,7 @@ propcache==0.4.1
pycparser==2.23
pydantic==2.12.0
pydantic_core==2.41.1
python-json-logger==4.0.0
sniffio==1.3.1
starlette==0.48.0
typing-inspection==0.4.2

View File

@ -1,5 +1,11 @@
import logging
import os
import logging
try:
from pythonjsonlogger import jsonlogger
JSON_LOGGING = True
except ImportError:
JSON_LOGGING = False
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
if LOG_LEVEL not in {"ERROR", "DEBUG", "INFO", "WARNING", "CRITICAL"}:
@ -9,8 +15,13 @@ def setup_logger(name: str) -> logging.Logger:
logger = logging.getLogger(name)
if not logger.handlers:
handler = logging.StreamHandler()
if JSON_LOGGING:
formatter = jsonlogger.JsonFormatter(
"%(asctime)s %(name)s %(levelname)s %(message)s"
)
else:
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
handler.setFormatter(formatter)
logger.addHandler(handler)