wip: add structlog

This commit is contained in:
grillazz
2025-06-18 08:49:32 +02:00
parent 3f09b5701e
commit c09c338b37
5 changed files with 72 additions and 80 deletions

View File

@@ -5,6 +5,12 @@ from rich.logging import RichHandler
from app.utils.singleton import SingletonMeta
import logging
import os
import orjson
import structlog
from whenever._whenever import Instant
from pathlib import Path
class AppLogger(metaclass=SingletonMeta):
_logger = None
@@ -22,3 +28,22 @@ class RichConsoleHandler(RichHandler):
console=Console(color_system="256", width=width, style=style, stderr=True),
**kwargs,
)
def setup_structlog() -> structlog.BoundLogger:
log_date = Instant.now().py_datetime().strftime("%Y%m%d")
log_path = Path(f"cuul_{log_date}_{os.getpid()}.log")
structlog.configure(
cache_logger_on_first_use=True,
wrapper_class=structlog.make_filtering_bound_logger(logging.INFO),
processors=[
structlog.contextvars.merge_contextvars,
structlog.processors.add_log_level,
structlog.processors.format_exc_info,
structlog.processors.TimeStamper(fmt="iso", utc=True),
structlog.processors.JSONRenderer(serializer=orjson.dumps),
],
logger_factory=structlog.BytesLoggerFactory(
file=log_path.open("wb")
)
)
return structlog.get_logger()