[a7c5ae4] | 1 | import json |
---|
| 2 | import logging |
---|
| 3 | |
---|
| 4 | class JsonFormatter(logging.Formatter): |
---|
| 5 | """ |
---|
| 6 | Formatter that outputs JSON strings after parsing the LogRecord. |
---|
| 7 | |
---|
| 8 | @param dict fmt_dict: Key: logging format attribute pairs. Defaults to {"message": "message"}. |
---|
| 9 | @param str time_format: time.strftime() format string. Default: "%Y-%m-%dT%H:%M:%S" |
---|
| 10 | @param str msec_format: Microsecond formatting. Appended at the end. Default: "%s.%03dZ" |
---|
| 11 | """ |
---|
| 12 | def __init__(self, fmt_dict: dict = None, time_format: str = "%Y-%m-%dT%H:%M:%S", msec_format: str = "%s.%03dZ"): |
---|
| 13 | self.fmt_dict = fmt_dict if fmt_dict is not None else {"message": "message"} |
---|
| 14 | self.default_time_format = time_format |
---|
| 15 | self.default_msec_format = msec_format |
---|
| 16 | self.datefmt = None |
---|
| 17 | |
---|
| 18 | def usesTime(self) -> bool: |
---|
| 19 | """ |
---|
| 20 | Overwritten to look for the attribute in the format dict values instead of the fmt string. |
---|
| 21 | """ |
---|
| 22 | return "asctime" in self.fmt_dict.values() |
---|
| 23 | |
---|
| 24 | def formatMessage(self, record) -> dict: |
---|
| 25 | """ |
---|
| 26 | Overwritten to return a dictionary of the relevant LogRecord attributes instead of a string. |
---|
| 27 | KeyError is raised if an unknown attribute is provided in the fmt_dict. |
---|
| 28 | """ |
---|
| 29 | return {fmt_key: record.__dict__[fmt_val] for fmt_key, fmt_val in self.fmt_dict.items()} |
---|
| 30 | |
---|
| 31 | def format(self, record) -> str: |
---|
| 32 | """ |
---|
| 33 | Mostly the same as the parent's class method, the difference being that a dict is manipulated and dumped as JSON |
---|
| 34 | instead of a string. |
---|
| 35 | """ |
---|
| 36 | record.message = record.getMessage() |
---|
| 37 | |
---|
| 38 | if self.usesTime(): |
---|
| 39 | record.asctime = self.formatTime(record, self.datefmt) |
---|
| 40 | |
---|
| 41 | message_dict = self.formatMessage(record) |
---|
| 42 | |
---|
| 43 | if record.exc_info: |
---|
| 44 | # Cache the traceback text to avoid converting it multiple times |
---|
| 45 | # (it's constant anyway) |
---|
| 46 | if not record.exc_text: |
---|
| 47 | record.exc_text = self.formatException(record.exc_info) |
---|
| 48 | |
---|
| 49 | if record.exc_text: |
---|
| 50 | message_dict["exc_info"] = record.exc_text |
---|
| 51 | |
---|
| 52 | if record.stack_info: |
---|
| 53 | message_dict["stack_info"] = self.formatStack(record.stack_info) |
---|
| 54 | |
---|
| 55 | return json.dumps(message_dict, default=str) |
---|