@@ -88,26 +88,8 @@ def search_cloudwatch_logs(
8888 events = []
8989 for event in response .get ("events" , []):
9090 message = event ["message" ]
91-
92- # Filter out non-error logs when searching for error patterns
93- if filter_pattern in [
94- "[ERROR]" ,
95- "[WARN]" ,
96- "ERROR:" ,
97- "WARN:" ,
98- "Exception" ,
99- "Failed" ,
100- ]:
101- # Skip INFO logs
102- if message .strip ().startswith ("[INFO]" ):
103- continue
104- # Skip Lambda system logs
105- if any (
106- message .strip ().startswith (prefix )
107- for prefix in ["INIT_START" , "START" , "END" , "REPORT" ]
108- ):
109- continue
110-
91+ if _should_exclude_log_event (message , filter_pattern ):
92+ continue
11193 events .append (
11294 {
11395 "timestamp" : datetime .fromtimestamp (
@@ -117,7 +99,6 @@ def search_cloudwatch_logs(
11799 "log_stream" : event .get ("logStreamName" , "" ),
118100 }
119101 )
120-
121102 # Stop when we have enough actual error events
122103 if len (events ) >= max_events :
123104 break
@@ -936,3 +917,55 @@ def cloudwatch_stack_logs(
936917 except Exception as e :
937918 logger .error (f"Stack log search failed for '{ stack_name } ': { e } " )
938919 return create_error_response (str (e ), stack_name = stack_name , events_found = 0 )
920+
921+
922+ def _should_exclude_log_event (message : str , filter_pattern : str = "" ) -> bool :
923+ """
924+ Consolidated log filtering - combines all exclusion logic.
925+ Filters out noise from LLM context while preserving relevant error information.
926+
927+ Args:
928+ message: Log message to evaluate
929+ filter_pattern: CloudWatch filter pattern being used
930+
931+ Returns:
932+ True if message should be excluded from LLM context
933+ """
934+ # Skip INFO logs when searching for error patterns
935+ if filter_pattern in [
936+ "[ERROR]" ,
937+ "[WARN]" ,
938+ "ERROR:" ,
939+ "WARN:" ,
940+ "Exception" ,
941+ "Failed" ,
942+ ]:
943+ if message .strip ().startswith ("[INFO]" ):
944+ return True
945+ # Skip Lambda system logs
946+ if any (
947+ message .strip ().startswith (prefix )
948+ for prefix in ["INIT_START" , "START" , "END" , "REPORT" ]
949+ ):
950+ return True
951+
952+ # Exclude content patterns that add no value for error analysis
953+ EXCLUDE_CONTENT = [
954+ "Config:" , # Configuration dumps
955+ '"sample_json"' , # Config JSON structures
956+ "Processing event:" , # Generic event processing logs
957+ "Initialized" , # Initialization messages
958+ "Starting" , # Startup messages
959+ "Debug:" , # Debug information
960+ "Trace:" , # Trace logs
961+ ]
962+
963+ # Skip if contains excluded content
964+ if any (exclude in message for exclude in EXCLUDE_CONTENT ):
965+ return True
966+
967+ # Skip very long messages (likely config dumps or verbose logs)
968+ if len (message ) > 1000 :
969+ return True
970+
971+ return False
0 commit comments