Added json logger
All checks were successful
Build & Publish to GHCR / build (push) Successful in 1m1s
All checks were successful
Build & Publish to GHCR / build (push) Successful in 1m1s
This commit is contained in:
parent
378f8cf06f
commit
4a5ab492e2
@ -13,6 +13,7 @@ prometheus_client==0.23.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
|
||||
|
||||
@ -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,11 +15,16 @@ def setup_logger(name: str) -> logging.Logger:
|
||||
logger = logging.getLogger(name)
|
||||
if not logger.handlers:
|
||||
handler = logging.StreamHandler()
|
||||
formatter = logging.Formatter(
|
||||
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
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"
|
||||
)
|
||||
handler.setFormatter(formatter)
|
||||
logger.addHandler(handler)
|
||||
logger.setLevel(getattr(logging, LOG_LEVEL))
|
||||
logger.debug(f"Logger {name} initialized with level {LOG_LEVEL}")
|
||||
return logger
|
||||
return logger
|
||||
Loading…
x
Reference in New Issue
Block a user