|
| 1 | +""" |
| 2 | +Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +""" |
| 4 | + |
| 5 | +import logging |
| 6 | + |
| 7 | +from .lambda_runtime_marshaller import to_json |
| 8 | + |
| 9 | + |
| 10 | +RESERVED_FIELDS = { |
| 11 | + "name", |
| 12 | + "msg", |
| 13 | + "args", |
| 14 | + "levelname", |
| 15 | + "levelno", |
| 16 | + "pathname", |
| 17 | + "filename", |
| 18 | + "module", |
| 19 | + "exc_info", |
| 20 | + "exc_text", |
| 21 | + "stack_info", |
| 22 | + "lineno", |
| 23 | + "funcName", |
| 24 | + "created", |
| 25 | + "msecs", |
| 26 | + "relativeCreated", |
| 27 | + "thread", |
| 28 | + "threadName", |
| 29 | + "processName", |
| 30 | + "process", |
| 31 | + "aws_request_id", |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +class JsonFormatter(logging.Formatter): |
| 36 | + def __init__(self): |
| 37 | + super().__init__(datefmt="%Y-%m-%dT%H:%M:%SZ", validate=False) |
| 38 | + |
| 39 | + def format_stacktrace(self, exc_info): |
| 40 | + if not exc_info: |
| 41 | + return None |
| 42 | + return self.formatException(exc_info) |
| 43 | + |
| 44 | + @staticmethod |
| 45 | + def format_exception_name(exc_info): |
| 46 | + if not exc_info: |
| 47 | + return None |
| 48 | + |
| 49 | + return exc_info[0].__name__ |
| 50 | + |
| 51 | + @staticmethod |
| 52 | + def format_exception(exc_info): |
| 53 | + if not exc_info: |
| 54 | + return None |
| 55 | + |
| 56 | + return str(exc_info[1]) |
| 57 | + |
| 58 | + @staticmethod |
| 59 | + def format_location(record: logging.LogRecord): |
| 60 | + return f"{record.pathname}:{record.funcName}:{record.lineno}" |
| 61 | + |
| 62 | + def format(self, record: logging.LogRecord) -> str: |
| 63 | + result = { |
| 64 | + "timestamp": self.formatTime(record, self.datefmt), |
| 65 | + "level": record.levelname, |
| 66 | + "message": record.getMessage(), |
| 67 | + "logger": record.name, |
| 68 | + "stackTrace": self.format_stacktrace(record.exc_info), |
| 69 | + "errorType": self.format_exception_name(record.exc_info), |
| 70 | + "errorMessage": self.format_exception(record.exc_info), |
| 71 | + "requestId": getattr(record, "aws_request_id", None), |
| 72 | + "location": self.format_location(record), |
| 73 | + } |
| 74 | + result.update( |
| 75 | + (key, value) |
| 76 | + for key, value in record.__dict__.items() |
| 77 | + if key not in RESERVED_FIELDS and key not in result |
| 78 | + ) |
| 79 | + result.update(INSTANCE_LOGGING_CONTEXT) |
| 80 | + result.update(INVOCATION_LOGGING_CONTEXT) |
| 81 | + |
| 82 | + result = {k: v for k, v in result.items() if v is not None} |
| 83 | + |
| 84 | + return to_json(result) |
| 85 | + |
| 86 | + |
| 87 | +INSTANCE_LOGGING_CONTEXT = dict() |
| 88 | +INVOCATION_LOGGING_CONTEXT = dict() |
| 89 | + |
| 90 | + |
| 91 | +def lambda_instance_logs_update(key: str, value: str): |
| 92 | + INSTANCE_LOGGING_CONTEXT[str(key)] = str(value) |
| 93 | + |
| 94 | + |
| 95 | +def lambda_invocation_logs_update(key: str, value: str): |
| 96 | + INVOCATION_LOGGING_CONTEXT[str(key)] = str(value) |
0 commit comments