Skip to content

Commit 18a2163

Browse files
author
Artem Krivonos
committed
make constants and utils private
1 parent b4fade9 commit 18a2163

File tree

3 files changed

+40
-40
lines changed

3 files changed

+40
-40
lines changed

awslambdaric/bootstrap.py

+24-24
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
from .lambda_runtime_exception import FaultException
1616
from .lambda_runtime_log_utils import (
1717
JsonFormatter,
18-
DATETIME_FORMAT,
19-
JSON_FORMAT,
20-
TEXT_FORMAT,
21-
get_log_format_from_str,
18+
_DATETIME_FORMAT,
19+
_JSON_FORMAT,
20+
_TEXT_FORMAT,
21+
_get_log_format_from_str,
2222
)
2323
from .lambda_runtime_marshaller import to_json
2424

2525
ERROR_LOG_LINE_TERMINATE = "\r"
2626
ERROR_LOG_IDENT = "\u00a0" # NO-BREAK SPACE U+00A0
2727
RUNTIME_ERROR_LOGGER_NAME = "system"
28-
AWS_LAMBDA_LOG_FORMAT = get_log_format_from_str(
28+
_AWS_LAMBDA_LOG_FORMAT = _get_log_format_from_str(
2929
os.environ.get("AWS_LAMBDA_LOG_FORMAT", "TEXT").upper()
3030
)
31-
AWS_LAMBDA_LOG_LEVEL = os.environ.get("AWS_LAMBDA_LOG_LEVEL")
31+
_AWS_LAMBDA_LOG_LEVEL = os.environ.get("AWS_LAMBDA_LOG_LEVEL")
3232

3333

3434
def _get_handler(handler):
@@ -90,7 +90,7 @@ def make_error(
9090
):
9191
result = {
9292
"timestamp": time.strftime(
93-
DATETIME_FORMAT, logging.Formatter.converter(time.time())
93+
_DATETIME_FORMAT, logging.Formatter.converter(time.time())
9494
),
9595
"log_level": level,
9696
"logger": RUNTIME_ERROR_LOGGER_NAME,
@@ -152,7 +152,7 @@ def log_error_text(error_result, log_sink):
152152
)
153153

154154

155-
if AWS_LAMBDA_LOG_FORMAT == JSON_FORMAT:
155+
if _AWS_LAMBDA_LOG_FORMAT == _JSON_FORMAT:
156156
log_error = log_error_json
157157
else:
158158
log_error = log_error_text
@@ -302,7 +302,7 @@ def emit(self, record):
302302
self.log_sink.log(
303303
msg,
304304
log_level=record.levelno,
305-
log_format=getattr(record, "log_format", TEXT_FORMAT),
305+
log_format=getattr(record, "log_format", _TEXT_FORMAT),
306306
)
307307

308308

@@ -353,18 +353,18 @@ def log_error(self, message_lines, log_level=logging.ERROR):
353353

354354

355355
FRAME_TYPES = {
356-
(JSON_FORMAT, logging.NOTSET): 0xA55A0002.to_bytes(4, "big"),
357-
(JSON_FORMAT, logging.DEBUG): 0xA55A000A.to_bytes(4, "big"),
358-
(JSON_FORMAT, logging.INFO): 0xA55A000E.to_bytes(4, "big"),
359-
(JSON_FORMAT, logging.WARNING): 0xA55A0012.to_bytes(4, "big"),
360-
(JSON_FORMAT, logging.ERROR): 0xA55A0016.to_bytes(4, "big"),
361-
(JSON_FORMAT, logging.CRITICAL): 0xA55A001A.to_bytes(4, "big"),
362-
(TEXT_FORMAT, logging.NOTSET): 0xA55A0003.to_bytes(4, "big"),
363-
(TEXT_FORMAT, logging.DEBUG): 0xA55A000B.to_bytes(4, "big"),
364-
(TEXT_FORMAT, logging.INFO): 0xA55A000F.to_bytes(4, "big"),
365-
(TEXT_FORMAT, logging.WARNING): 0xA55A0013.to_bytes(4, "big"),
366-
(TEXT_FORMAT, logging.ERROR): 0xA55A0017.to_bytes(4, "big"),
367-
(TEXT_FORMAT, logging.CRITICAL): 0xA55A001B.to_bytes(4, "big"),
356+
(_JSON_FORMAT, logging.NOTSET): 0xA55A0002.to_bytes(4, "big"),
357+
(_JSON_FORMAT, logging.DEBUG): 0xA55A000A.to_bytes(4, "big"),
358+
(_JSON_FORMAT, logging.INFO): 0xA55A000E.to_bytes(4, "big"),
359+
(_JSON_FORMAT, logging.WARNING): 0xA55A0012.to_bytes(4, "big"),
360+
(_JSON_FORMAT, logging.ERROR): 0xA55A0016.to_bytes(4, "big"),
361+
(_JSON_FORMAT, logging.CRITICAL): 0xA55A001A.to_bytes(4, "big"),
362+
(_TEXT_FORMAT, logging.NOTSET): 0xA55A0003.to_bytes(4, "big"),
363+
(_TEXT_FORMAT, logging.DEBUG): 0xA55A000B.to_bytes(4, "big"),
364+
(_TEXT_FORMAT, logging.INFO): 0xA55A000F.to_bytes(4, "big"),
365+
(_TEXT_FORMAT, logging.WARNING): 0xA55A0013.to_bytes(4, "big"),
366+
(_TEXT_FORMAT, logging.ERROR): 0xA55A0017.to_bytes(4, "big"),
367+
(_TEXT_FORMAT, logging.CRITICAL): 0xA55A001B.to_bytes(4, "big"),
368368
}
369369
DEFAULT_FRAME_TYPE = 0xA55A0003.to_bytes(4, "big")
370370

@@ -395,7 +395,7 @@ def __enter__(self):
395395
def __exit__(self, exc_type, exc_value, exc_tb):
396396
self.file.close()
397397

398-
def log(self, msg, log_level=logging.NOTSET, log_format: int = TEXT_FORMAT):
398+
def log(self, msg, log_level=logging.NOTSET, log_format: int = _TEXT_FORMAT):
399399
frame_type = FRAME_TYPES.get((log_format, log_level), DEFAULT_FRAME_TYPE)
400400
encoded_msg = msg.encode("utf8")
401401

@@ -439,7 +439,7 @@ def setup_logging(log_format, log_level, log_sink):
439439
logging.Formatter.converter = time.gmtime
440440
logger = logging.getLogger()
441441
logger_handler = LambdaLoggerHandler(log_sink)
442-
if log_format == JSON_FORMAT:
442+
if log_format == _JSON_FORMAT:
443443
logger_handler.setFormatter(JsonFormatter())
444444
else:
445445
logger_handler.setFormatter(
@@ -464,7 +464,7 @@ def run(app_root, handler, lambda_runtime_api_addr):
464464
lambda_runtime_client = LambdaRuntimeClient(lambda_runtime_api_addr)
465465

466466
try:
467-
setup_logging(AWS_LAMBDA_LOG_FORMAT, AWS_LAMBDA_LOG_LEVEL, log_sink)
467+
setup_logging(_AWS_LAMBDA_LOG_FORMAT, _AWS_LAMBDA_LOG_LEVEL, log_sink)
468468
global _GLOBAL_AWS_REQUEST_ID
469469

470470
request_handler = _get_handler(handler)

awslambdaric/lambda_runtime_log_utils.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
from .lambda_runtime_marshaller import to_json
99

10-
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
11-
RESERVED_FIELDS = {
10+
_DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
11+
_RESERVED_FIELDS = {
1212
"name",
1313
"msg",
1414
"args",
@@ -32,19 +32,19 @@
3232
"aws_request_id",
3333
"log_format",
3434
}
35-
JSON_FORMAT = 0b0
36-
TEXT_FORMAT = 0b1
35+
_JSON_FORMAT = 0b0
36+
_TEXT_FORMAT = 0b1
3737

3838

39-
def get_log_format_from_str(value: str):
39+
def _get_log_format_from_str(value: str):
4040
if value == "JSON":
41-
return JSON_FORMAT
42-
return TEXT_FORMAT
41+
return _JSON_FORMAT
42+
return _TEXT_FORMAT
4343

4444

4545
class JsonFormatter(logging.Formatter):
4646
def __init__(self):
47-
super().__init__(datefmt=DATETIME_FORMAT, validate=False)
47+
super().__init__(datefmt=_DATETIME_FORMAT, validate=False)
4848

4949
@staticmethod
5050
def format_stacktrace(exc_info):
@@ -80,7 +80,7 @@ def format_log_level(record: logging.LogRecord):
8080

8181
def format(self, record: logging.LogRecord) -> str:
8282
self.format_log_level(record)
83-
record.log_format = JSON_FORMAT
83+
record.log_format = _JSON_FORMAT
8484

8585
result = {
8686
"timestamp": self.formatTime(record, self.datefmt),
@@ -96,7 +96,7 @@ def format(self, record: logging.LogRecord) -> str:
9696
result.update(
9797
(key, value)
9898
for key, value in record.__dict__.items()
99-
if key not in RESERVED_FIELDS and key not in result
99+
if key not in _RESERVED_FIELDS and key not in result
100100
)
101101

102102
result = {k: v for k, v in result.items() if v is not None}

tests/test_bootstrap.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
from awslambdaric.lambda_runtime_exception import FaultException
2121
from awslambdaric.lambda_runtime_marshaller import LambdaMarshaller
2222
from awslambdaric.lambda_runtime_log_utils import (
23-
JSON_FORMAT,
24-
TEXT_FORMAT,
25-
get_log_format_from_str,
23+
_JSON_FORMAT,
24+
_TEXT_FORMAT,
25+
_get_log_format_from_str,
2626
)
2727

2828

@@ -1146,8 +1146,8 @@ def test_log_level_frame_type(self):
11461146

11471147
def test_log_format_frame_type(self):
11481148
test_cases = [
1149-
(TEXT_FORMAT, 0xA55A0003),
1150-
(JSON_FORMAT, 0xA55A0002),
1149+
(_TEXT_FORMAT, 0xA55A0003),
1150+
(_JSON_FORMAT, 0xA55A0002),
11511151
]
11521152

11531153
for fmt, expected_frame_type in test_cases:
@@ -1228,7 +1228,7 @@ class TestLogging(unittest.TestCase):
12281228
@classmethod
12291229
def setUpClass(cls) -> None:
12301230
bootstrap.setup_logging(
1231-
get_log_format_from_str("JSON"), "INFO", bootstrap.StandardLogSink()
1231+
_get_log_format_from_str("JSON"), "INFO", bootstrap.StandardLogSink()
12321232
)
12331233

12341234
@patch("sys.stderr", new_callable=StringIO)

0 commit comments

Comments
 (0)