15
15
from .lambda_runtime_exception import FaultException
16
16
from .lambda_runtime_log_utils import (
17
17
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 ,
22
22
)
23
23
from .lambda_runtime_marshaller import to_json
24
24
25
25
ERROR_LOG_LINE_TERMINATE = "\r "
26
26
ERROR_LOG_IDENT = "\u00a0 " # NO-BREAK SPACE U+00A0
27
27
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 (
29
29
os .environ .get ("AWS_LAMBDA_LOG_FORMAT" , "TEXT" ).upper ()
30
30
)
31
- AWS_LAMBDA_LOG_LEVEL = os .environ .get ("AWS_LAMBDA_LOG_LEVEL" )
31
+ _AWS_LAMBDA_LOG_LEVEL = os .environ .get ("AWS_LAMBDA_LOG_LEVEL" )
32
32
33
33
34
34
def _get_handler (handler ):
@@ -90,7 +90,7 @@ def make_error(
90
90
):
91
91
result = {
92
92
"timestamp" : time .strftime (
93
- DATETIME_FORMAT , logging .Formatter .converter (time .time ())
93
+ _DATETIME_FORMAT , logging .Formatter .converter (time .time ())
94
94
),
95
95
"log_level" : level ,
96
96
"logger" : RUNTIME_ERROR_LOGGER_NAME ,
@@ -152,7 +152,7 @@ def log_error_text(error_result, log_sink):
152
152
)
153
153
154
154
155
- if AWS_LAMBDA_LOG_FORMAT == JSON_FORMAT :
155
+ if _AWS_LAMBDA_LOG_FORMAT == _JSON_FORMAT :
156
156
log_error = log_error_json
157
157
else :
158
158
log_error = log_error_text
@@ -302,7 +302,7 @@ def emit(self, record):
302
302
self .log_sink .log (
303
303
msg ,
304
304
log_level = record .levelno ,
305
- log_format = getattr (record , "log_format" , TEXT_FORMAT ),
305
+ log_format = getattr (record , "log_format" , _TEXT_FORMAT ),
306
306
)
307
307
308
308
@@ -353,18 +353,18 @@ def log_error(self, message_lines, log_level=logging.ERROR):
353
353
354
354
355
355
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" ),
368
368
}
369
369
DEFAULT_FRAME_TYPE = 0xA55A0003 .to_bytes (4 , "big" )
370
370
@@ -395,7 +395,7 @@ def __enter__(self):
395
395
def __exit__ (self , exc_type , exc_value , exc_tb ):
396
396
self .file .close ()
397
397
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 ):
399
399
frame_type = FRAME_TYPES .get ((log_format , log_level ), DEFAULT_FRAME_TYPE )
400
400
encoded_msg = msg .encode ("utf8" )
401
401
@@ -439,7 +439,7 @@ def setup_logging(log_format, log_level, log_sink):
439
439
logging .Formatter .converter = time .gmtime
440
440
logger = logging .getLogger ()
441
441
logger_handler = LambdaLoggerHandler (log_sink )
442
- if log_format == JSON_FORMAT :
442
+ if log_format == _JSON_FORMAT :
443
443
logger_handler .setFormatter (JsonFormatter ())
444
444
else :
445
445
logger_handler .setFormatter (
@@ -464,7 +464,7 @@ def run(app_root, handler, lambda_runtime_api_addr):
464
464
lambda_runtime_client = LambdaRuntimeClient (lambda_runtime_api_addr )
465
465
466
466
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 )
468
468
global _GLOBAL_AWS_REQUEST_ID
469
469
470
470
request_handler = _get_handler (handler )
0 commit comments