forked from aws/aws-lambda-python-runtime-interface-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_runtime_log_utils.py
More file actions
139 lines (113 loc) · 3.77 KB
/
lambda_runtime_log_utils.py
File metadata and controls
139 lines (113 loc) · 3.77 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
"""
import json
import logging
import traceback
from enum import IntEnum
_DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
_RESERVED_FIELDS = {
"name",
"msg",
"args",
"levelname",
"levelno",
"pathname",
"filename",
"module",
"exc_info",
"exc_text",
"stack_info",
"lineno",
"funcName",
"created",
"msecs",
"relativeCreated",
"thread",
"threadName",
"processName",
"process",
"aws_request_id",
"tenant_id",
"_frame_type",
}
class LogFormat(IntEnum):
JSON = 0b0
TEXT = 0b1
@classmethod
def from_str(cls, value: str):
if value and value.upper() == "JSON":
return cls.JSON.value
return cls.TEXT.value
def _get_log_level_from_env_var(log_level):
return {None: "", "TRACE": "DEBUG"}.get(log_level, log_level).upper()
_JSON_FRAME_TYPES = {
logging.NOTSET: 0xA55A0002.to_bytes(4, "big"),
logging.DEBUG: 0xA55A000A.to_bytes(4, "big"),
logging.INFO: 0xA55A000E.to_bytes(4, "big"),
logging.WARNING: 0xA55A0012.to_bytes(4, "big"),
logging.ERROR: 0xA55A0016.to_bytes(4, "big"),
logging.CRITICAL: 0xA55A001A.to_bytes(4, "big"),
}
_TEXT_FRAME_TYPES = {
logging.NOTSET: 0xA55A0003.to_bytes(4, "big"),
logging.DEBUG: 0xA55A000B.to_bytes(4, "big"),
logging.INFO: 0xA55A000F.to_bytes(4, "big"),
logging.WARNING: 0xA55A0013.to_bytes(4, "big"),
logging.ERROR: 0xA55A0017.to_bytes(4, "big"),
logging.CRITICAL: 0xA55A001B.to_bytes(4, "big"),
}
_DEFAULT_FRAME_TYPE = _TEXT_FRAME_TYPES[logging.NOTSET]
_json_encoder = json.JSONEncoder(ensure_ascii=False)
_encode_json = _json_encoder.encode
def _format_log_level(record: logging.LogRecord) -> int:
return min(50, max(0, record.levelno)) // 10 * 10
class JsonFormatter(logging.Formatter):
def __init__(self):
super().__init__(datefmt=_DATETIME_FORMAT)
@staticmethod
def __format_stacktrace(exc_info):
if not exc_info:
return None
return traceback.format_tb(exc_info[2])
@staticmethod
def __format_exception_name(exc_info):
if not exc_info:
return None
return exc_info[0].__name__
@staticmethod
def __format_exception(exc_info):
if not exc_info:
return None
return str(exc_info[1])
@staticmethod
def __format_location(record: logging.LogRecord):
if not record.exc_info:
return None
return f"{record.pathname}:{record.funcName}:{record.lineno}"
def format(self, record: logging.LogRecord) -> str:
record.levelno = _format_log_level(record)
record.levelname = logging.getLevelName(record.levelno)
record._frame_type = _JSON_FRAME_TYPES.get(
record.levelno, _JSON_FRAME_TYPES[logging.NOTSET]
)
result = {
"timestamp": self.formatTime(record, self.datefmt),
"level": record.levelname,
"message": record.getMessage(),
"logger": record.name,
"stackTrace": self.__format_stacktrace(record.exc_info),
"errorType": self.__format_exception_name(record.exc_info),
"errorMessage": self.__format_exception(record.exc_info),
"requestId": getattr(record, "aws_request_id", None),
"location": self.__format_location(record),
}
if hasattr(record, "tenant_id") and record.tenant_id is not None:
result["tenantId"] = record.tenant_id
result.update(
(key, value)
for key, value in record.__dict__.items()
if key not in _RESERVED_FIELDS and key not in result
)
result = {k: v for k, v in result.items() if v is not None}
return _encode_json(result) + "\n"