Amazon CloudWatch Log Insights is a powerful tool for analyzing logs generated by AWS services. One common task is filtering log messages to find entries containing a specific string. Various methods can be used, each suited to different needs.
The simplest way to find logs containing a specific word or phrase is the strcontains function. It checks if a specific substring exists in a field, such as @message:
fields @timestamp, @message | filter strcontains(@message, "Error") | sort @timestamp desc | limit 50
This query filters logs for messages containing the word “Error,” showing the most recent 50 results.
The LIKE operator enables pattern matching using regular expressions, making it highly flexible for queries:
fields @timestamp, @message | filter @message like /timeout|failure/ | sort @timestamp desc | limit 30
This example filters messages containing either “timeout” or “failure.” Regular expressions can handle variations in text format or multiple keywords.
Another method is the =~ operator for pattern-based searches:
fields @timestamp, @message | filter @message =~ /.*Critical Error.*/ | sort @timestamp desc | limit 25
This finds messages containing the phrase “Critical Error,” regardless of its position in the log.
To enhance query performance:
You can combine filters for complex conditions:
fields @timestamp, @message | filter strcontains(@message, "Error") and not strcontains(@message, "Debug") | sort @timestamp desc | limit 20
This query finds log entries with “Error” but excludes those containing “Debug.”
AWS Log Insights offers versatile ways to filter logs by content, from simple substring matching to advanced regex queries. Whether you’re debugging issues or monitoring application performance, these methods ensure accurate and efficient log analysis.