-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging_config.py
More file actions
49 lines (39 loc) · 1.54 KB
/
Copy pathlogging_config.py
File metadata and controls
49 lines (39 loc) · 1.54 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
import logging
import json
import sys
from datetime import datetime
from typing import Any, Dict
class JSONFormatter(logging.Formatter):
"""Custom JSON formatter for structured logging"""
def format(self, record: logging.LogRecord) -> str:
record_dict: Dict[str, Any] = {
'timestamp': datetime.utcnow().isoformat(),
'level': record.levelname,
'message': record.getMessage(),
'module': record.module,
'function': record.funcName,
'line': record.lineno
}
# Add exception info if present
if record.exc_info:
record_dict['exception'] = self.formatException(record.exc_info)
# Add extra fields
if hasattr(record, 'extra_fields'):
record_dict.update(record.extra_fields)
return json.dumps(record_dict)
def setup_logging(service_name: str = 'analytics', level: str = 'INFO') -> None:
"""Setup structured logging"""
root_logger = logging.getLogger()
# Remove existing handlers
for handler in root_logger.handlers:
root_logger.removeHandler(handler)
# Create console handler with JSON formatter
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(JSONFormatter())
# Set logging level
root_logger.setLevel(level)
root_logger.addHandler(handler)
# Add service name to all logs
logging.LogRecord = type('LogRecord', (logging.LogRecord,), {
'extra_fields': {'service': service_name}
})