Use AWS_LAMBDA_LOG_LEVEL env var to set log level for text format by kotyara1005 · Pull Request #111 · aws/aws-lambda-python-runtime-interface-client · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions awslambdaric/bootstrap.py
26 changes: 19 additions & 7 deletions awslambdaric/lambda_runtime_log_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def from_str(cls, value: str):
return cls.TEXT.value


def _get_log_level_from_env_var(log_level):
Comment thread
alex-pewpew marked this conversation as resolved.
return {None: "", "TRACE": "DEBUG"}.get(log_level, log_level).upper()
Comment thread
alex-pewpew marked this conversation as resolved.


_JSON_FRAME_TYPES = {
logging.NOTSET: 0xA55A0002.to_bytes(4, "big"),
logging.DEBUG: 0xA55A000A.to_bytes(4, "big"),
Expand All @@ -53,12 +57,24 @@ def from_str(cls, value: str):
logging.ERROR: 0xA55A0016.to_bytes(4, "big"),
logging.CRITICAL: 0xA55A001A.to_bytes(4, "big"),
}
_DEFAULT_FRAME_TYPE = 0xA55A0003.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)
Expand Down Expand Up @@ -90,13 +106,9 @@ def __format_location(record: logging.LogRecord):

return f"{record.pathname}:{record.funcName}:{record.lineno}"

@staticmethod
def __format_log_level(record: logging.LogRecord):
record.levelno = min(50, max(0, record.levelno)) // 10 * 10
record.levelname = logging.getLevelName(record.levelno)

def format(self, record: logging.LogRecord) -> str:
self.__format_log_level(record)
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]
)
Expand Down
83 changes: 69 additions & 14 deletions tests/test_bootstrap.py