99 ENV_ID: Unique identifier for this test run
1010"""
1111
12+ import atexit
1213import json
1314import logging
1415import os
2122
2223BASE_LOGZIO_API_URL = os .getenv ("LOGZIO_API_URL" , "https://api.logz.io/v1" )
2324
25+ # Track handlers to close them properly at exit
26+ _handlers_to_close = []
27+
28+
29+ def _cleanup_handlers ():
30+ """Close all handlers at exit to prevent logging errors."""
31+ for handler in _handlers_to_close :
32+ try :
33+ handler .close ()
34+ except Exception :
35+ pass
36+
37+
38+ atexit .register (_cleanup_handlers )
39+
2440
2541def get_env_or_fail (var_name : str ) -> str :
2642 """Get environment variable or fail test if not set."""
@@ -78,6 +94,7 @@ def send_test_log(token: str, env_id: str, message: str):
7894 debug = True ,
7995 backup_logs = False
8096 )
97+ _handlers_to_close .append (handler )
8198
8299 logger = logging .getLogger (f"e2e-test-{ env_id } " )
83100 logger .setLevel (logging .INFO )
@@ -87,7 +104,7 @@ def send_test_log(token: str, env_id: str, message: str):
87104 logger .info (message , extra = {"env_id" : env_id , "test_source" : "python-handler-e2e" })
88105
89106 handler .flush ()
90- time .sleep (3 )
107+ time .sleep (3 )
91108
92109
93110class TestLogzioLogs :
@@ -106,7 +123,7 @@ def test_logs_received(self):
106123 send_test_log (self .token , self .env_id , test_message )
107124
108125 print ("Waiting for log ingestion..." )
109- time .sleep (180 )
126+ time .sleep (240 )
110127
111128 query = f"env_id:{ self .env_id } AND type:{ self .env_id } "
112129 response = fetch_logs (self .api_key , query )
@@ -135,7 +152,7 @@ def test_log_content_matches(self):
135152 send_test_log (self .token , self .env_id , test_message )
136153
137154 print ("Waiting for log ingestion..." )
138- time .sleep (180 )
155+ time .sleep (240 )
139156
140157 query = f"env_id:{ self .env_id } "
141158 response = fetch_logs (self .api_key , query )
@@ -155,6 +172,9 @@ def test_log_content_matches(self):
155172 break
156173
157174 if not matching_log :
175+ print ("Available logs:" )
176+ for hit in hits [:5 ]:
177+ print (f" - { hit .get ('_source' , {}).get ('message' , 'N/A' )} " )
158178 pytest .fail ("Test log with 'Content validation test' not found in message field" )
159179
160180 assert matching_log .get ("env_id" ) == self .env_id , "env_id mismatch"
0 commit comments