@@ -1179,7 +1179,6 @@ def test_log_level(self) -> None:
11791179 (LogFormat .JSON , "WARN" , logging .WARNING ),
11801180 (LogFormat .JSON , "ERROR" , logging .ERROR ),
11811181 (LogFormat .JSON , "FATAL" , logging .CRITICAL ),
1182- # Log level is set only for Json format
11831182 (LogFormat .TEXT , "TRACE" , logging .DEBUG ),
11841183 (LogFormat .TEXT , "DEBUG" , logging .DEBUG ),
11851184 (LogFormat .TEXT , "INFO" , logging .INFO ),
@@ -1205,6 +1204,58 @@ def test_log_level(self) -> None:
12051204 self .assertEqual (expected_level , logging .getLogger ().level )
12061205
12071206
1207+ class TestLambdaLoggerHandlerSetup (unittest .TestCase ):
1208+ @classmethod
1209+ def tearDownClass (cls ):
1210+ importlib .reload (bootstrap )
1211+ logging .getLogger ().handlers .clear ()
1212+ logging .getLogger ().level = logging .NOTSET
1213+
1214+ def test_handler_setup (self , * _ ):
1215+ test_cases = [
1216+ (62 , 0xA55A0003 , 46 , {}),
1217+ (133 , 0xA55A001A , 117 , {"AWS_LAMBDA_LOG_FORMAT" : "JSON" }),
1218+ (62 , 0xA55A001B , 46 , {"AWS_LAMBDA_LOG_LEVEL" : "INFO" }),
1219+ ]
1220+
1221+ for total_length , header , message_length , env_vars in test_cases :
1222+ with patch .dict (
1223+ os .environ , env_vars , clear = True
1224+ ), NamedTemporaryFile () as temp_file :
1225+ importlib .reload (bootstrap )
1226+ logging .getLogger ().handlers .clear ()
1227+ logging .getLogger ().level = logging .NOTSET
1228+
1229+ before = int (time .time_ns () / 1000 )
1230+ with bootstrap .FramedTelemetryLogSink (
1231+ os .open (temp_file .name , os .O_CREAT | os .O_RDWR )
1232+ ) as ls :
1233+ bootstrap ._setup_logging (
1234+ bootstrap ._AWS_LAMBDA_LOG_FORMAT ,
1235+ bootstrap ._AWS_LAMBDA_LOG_LEVEL ,
1236+ ls ,
1237+ )
1238+ logger = logging .getLogger ()
1239+ logger .critical ("critical" )
1240+ after = int (time .time_ns () / 1000 )
1241+
1242+ content = open (temp_file .name , "rb" ).read ()
1243+ self .assertEqual (len (content ), total_length )
1244+
1245+ pos = 0
1246+ frame_type = int .from_bytes (content [pos : pos + 4 ], "big" )
1247+ self .assertEqual (frame_type , header )
1248+ pos += 4
1249+
1250+ length = int .from_bytes (content [pos : pos + 4 ], "big" )
1251+ self .assertEqual (length , message_length )
1252+ pos += 4
1253+
1254+ timestamp = int .from_bytes (content [pos : pos + 8 ], "big" )
1255+ self .assertTrue (before <= timestamp )
1256+ self .assertTrue (timestamp <= after )
1257+
1258+
12081259class TestLogging (unittest .TestCase ):
12091260 @classmethod
12101261 def setUpClass (cls ) -> None :
0 commit comments