-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_log.py
More file actions
27 lines (24 loc) · 783 Bytes
/
setup_log.py
File metadata and controls
27 lines (24 loc) · 783 Bytes
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
import os
import logging
import json
class JsonFormatter(logging.Formatter):
""" Log Formatter for JSON output
"""
def format(self, record):
# Create a log record in JSON format
log_record = {
'timestamp': self.formatTime(record, self.datefmt),
'name': record.name,
'level': record.levelname,
'message': record.getMessage(),
}
# Include exception info if present
if record.exc_info:
log_record['exc_info'] = self.formatException(record.exc_info)
return json.dumps(log_record)
# Set up logging
logger = logging.getLogger("jsonLogger")
# Set Custom log formatter
handler = logging.StreamHandler()
handler.setFormatter(JsonFormatter())
logger.addHandler(handler)