-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging_config.py
More file actions
47 lines (39 loc) · 1.26 KB
/
logging_config.py
File metadata and controls
47 lines (39 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
Production logging configuration using structlog.
"""
import logging
import sys
from pathlib import Path
import structlog
from config.settings import get_settings
def setup_logging():
settings = get_settings()
# Ensure log directory exists
log_path = Path(settings.log_file)
log_path.parent.mkdir(parents=True, exist_ok=True)
# Configure standard library logging
logging.basicConfig(
format="%(message)s",
level=getattr(logging, settings.log_level.upper()),
handlers=[
logging.StreamHandler(sys.stdout),
logging.FileHandler(settings.log_file),
],
)
# Configure structlog
structlog.configure(
processors=[
structlog.contextvars.merge_contextvars,
structlog.processors.add_log_level,
structlog.processors.StackInfoRenderer(),
structlog.dev.set_exc_info,
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.JSONRenderer(),
],
wrapper_class=structlog.make_filtering_bound_logger(
getattr(logging, settings.log_level.upper())
),
context_class=dict,
logger_factory=structlog.PrintLoggerFactory(),
cache_logger_on_first_use=True,
)