-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlogging_config.py
More file actions
168 lines (140 loc) · 5.75 KB
/
Copy pathlogging_config.py
File metadata and controls
168 lines (140 loc) · 5.75 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""Logging configuration and utilities for the Memory Tracker API."""
import logging
import json
import time
from contextvars import ContextVar
from typing import Optional
# Context variables for per-request logging
request_id_var: ContextVar[Optional[str]] = ContextVar("request_id", default=None)
user_id_var: ContextVar[Optional[str]] = ContextVar("user_id", default=None)
request_start_time_var: ContextVar[Optional[float]] = ContextVar(
"request_start_time", default=None
)
class LoggingManager:
"""Manages logging configuration and formatters."""
def __init__(self, settings):
self.settings = settings
self._configured = False
def configure_logging(self):
"""Configure logging based on the settings."""
if self._configured:
return
# Clear existing handlers to avoid conflicts
logging.root.handlers.clear()
if self.settings.log_format == "json":
self._configure_json_logging()
else:
self._configure_text_logging()
self._configured = True
def _configure_json_logging(self):
"""Configure JSON structured logging."""
logging.basicConfig(
level=getattr(logging, self.settings.log_level.upper()),
handlers=[logging.StreamHandler()],
format="%(message)s",
force=True,
)
class JsonFormatter(logging.Formatter):
def format(self, record):
log_entry = {
"timestamp": self.formatTime(record, "%Y-%m-%dT%H:%M:%S.%fZ"),
"level": record.levelname,
"logger": record.name,
"message": record.getMessage(),
"module": record.module,
"function": record.funcName,
"line": record.lineno,
}
# Add context variables
request_id = request_id_var.get()
if request_id:
log_entry["request_id"] = request_id
user_id = user_id_var.get()
if user_id:
log_entry["user_id"] = user_id
start_time = request_start_time_var.get()
if start_time:
log_entry["request_duration_ms"] = int(
(time.time() - start_time) * 1000
)
# Add explicit fields from record
for field in [
"duration_ms",
"status_code",
"error",
"method",
"path",
"query_params",
]:
if hasattr(record, field):
log_entry[field] = getattr(record, field)
return json.dumps(log_entry)
for handler in logging.root.handlers:
handler.setFormatter(JsonFormatter())
def _configure_text_logging(self):
"""Configure human-readable text logging."""
class ContextFormatter(logging.Formatter):
def format(self, record):
# Add context to the record
request_id = request_id_var.get()
if request_id:
record.request_id = request_id[:8]
else:
record.request_id = "N/A"
user_id = user_id_var.get()
if user_id:
record.user_id = user_id
else:
record.user_id = "N/A"
# Format the base message
base_msg = super().format(record)
# Add extra fields if they exist
extra_fields = []
for field in [
"skip",
"limit",
"count",
"sha",
"author",
"message_length",
"method",
"path",
"status_code",
"duration_ms",
"error",
"query_params",
"user_agent",
"client_ip",
]:
if hasattr(record, field) and getattr(record, field) is not None:
value = getattr(record, field)
if field == "query_params" and value:
extra_fields.append(f"params={value}")
elif field == "duration_ms":
extra_fields.append(f"took={value}ms")
elif field == "user_agent":
ua = (
str(value)[:50] + "..."
if len(str(value)) > 50
else str(value)
)
extra_fields.append(f"ua={ua}")
elif field == "error":
extra_fields.append(f"error={value}")
else:
extra_fields.append(f"{field}={value}")
if extra_fields:
return f"{base_msg} | {' '.join(extra_fields)}"
else:
return base_msg
logging.basicConfig(
level=getattr(logging, self.settings.log_level.upper()),
format="%(asctime)s - %(name)s - %(levelname)s - [%(request_id)s|%(user_id)s] - [%(funcName)s:%(lineno)d] - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
force=True,
)
for handler in logging.root.handlers:
handler.setFormatter(ContextFormatter())
def get_logger(name: str) -> logging.Logger:
"""Get a logger with automatic context injection."""
return logging.getLogger(name)