-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtester.py
More file actions
60 lines (51 loc) · 1.89 KB
/
Copy pathtester.py
File metadata and controls
60 lines (51 loc) · 1.89 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
# logging_config.py
import logging
import json
import sys
from datetime import datetime
from typing import Any, Dict
class JSONFormatter(logging.Formatter):
def format(self, record: logging.LogRecord) -> str:
log_entry = {
'@timestamp': datetime.utcnow().isoformat(),
'service': 'analytics',
'level': record.levelname,
'message': record.getMessage(),
'logger_name': record.name,
'module': record.module,
'function': record.funcName,
'line_number': record.lineno,
'path': record.pathname
}
# Add extra fields if they exist
print(record.__dict__, "hereee")
# if hasattr(record, 'extra'):
# log_entry.update(record.extra)
extra_fields = {
key: value
for key, value in record.__dict__.items()
if not (key.startswith('_') or key in ['msg', 'args', 'exc_info', 'exc_text', 'name',
'levelno', 'levelname', 'pathname', 'filename', 'module', 'lineno',
'funcName', 'created', 'msecs', 'relativeCreated', 'thread', 'threading',
'processName', 'process'])
}
log_entry.update(extra_fields)
# Add exception info if present
if record.exc_info:
log_entry['exception'] = self.formatException(record.exc_info)
return json.dumps(log_entry)
def setup_logging():
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# Console handler with JSON formatting
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(JSONFormatter())
logger.addHandler(handler)
return logger
# Example usage in your service
logger = setup_logging()
# Add some test logs
logger.info("Test log message", extra={
'test_id': 123,
'environment': 'development'
})