{"id":11858,"date":"2025-01-27T06:30:10","date_gmt":"2025-01-27T06:30:10","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=11858"},"modified":"2025-01-27T06:30:10","modified_gmt":"2025-01-27T06:30:10","slug":"use-string-contains-in-aws-log-insights-queries","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/cloud\/use-string-contains-in-aws-log-insights-queries","title":{"rendered":"AWS Log Insights Query with String Contains"},"content":{"rendered":"<h2>Querying AWS Log Insights for Specific Strings<\/h2>\n<p>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.<\/p>\n<h3>1. Basic Filtering with strcontains<\/h3>\n<p>The simplest way to find logs containing a specific word or phrase is the <strong>strcontains<\/strong> function. It checks if a specific substring exists in a field, such as <strong>@message<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nfields @timestamp, @message\r\n| filter strcontains(@message, \"Error\")\r\n| sort @timestamp desc\r\n| limit 50\r\n<\/pre>\n<p>This query filters logs for messages containing the word &#8220;Error,&#8221; showing the most recent 50 results.<\/p>\n<h3>2. Advanced Matching with LIKE<\/h3>\n<p>The LIKE operator enables pattern matching using regular expressions, making it highly flexible for queries:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nfields @timestamp, @message  \r\n| filter @message like \/timeout|failure\/  \r\n| sort @timestamp desc  \r\n| limit 30 \r\n<\/pre>\n<p>This example filters messages containing either &#8220;timeout&#8221; or &#8220;failure.&#8221; Regular expressions can handle variations in text format or multiple keywords.<\/p>\n<h3>3. Regex Matching with =~<\/h3>\n<p>Another method is the =~ operator for pattern-based searches:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nfields @timestamp, @message\r\n| filter @message =~ \/.*Critical Error.*\/\r\n| sort @timestamp desc\r\n| limit 25\r\n<\/pre>\n<p>This finds messages containing the phrase &#8220;Critical Error,&#8221; regardless of its position in the log.<\/p>\n<h3>4. Performance Optimization<\/h3>\n<p>To enhance query performance:<\/p>\n<ul>\n<li>Use indexed fields (like @message, @logStream) in filters.<\/li>\n<li>Reduce result sizes with limit and refine time ranges to narrow down searches.<\/li>\n<li>Use the fields command to include only relevant fields, reducing data processing overhead.<\/li>\n<\/ul>\n<h3>5. Combining Filters<\/h3>\n<p>You can combine filters for complex conditions:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nfields @timestamp, @message\r\n| filter strcontains(@message, \"Error\") and not strcontains(@message, \"Debug\")\r\n| sort @timestamp desc\r\n| limit 20\r\n<\/pre>\n<p>This query finds log entries with &#8220;Error&#8221; but excludes those containing &#8220;Debug.&#8221;<\/p>\n<h2>Practical Use Cases<\/h2>\n<ul>\n<li><strong>Error Detection:<\/strong> Quickly identify error messages or failed processes in your logs.<\/li>\n<li><strong>Pattern Matching:<\/strong> Use regex for sophisticated searches, like finding IP addresses or timestamps.<\/li>\n<li><strong>Exclusion Filters:<\/strong> Simplify data analysis by removing irrelevant entries.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>AWS Log Insights offers versatile ways to filter logs by content, from simple substring matching to advanced regex queries. Whether you\u2019re debugging issues or monitoring application performance, these methods ensure accurate and efficient log analysis.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Querying AWS Log Insights for Specific Strings 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. 1. Basic Filtering with strcontains The simplest way to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11859,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[17],"tags":[],"class_list":["post-11858","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11858"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=11858"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11858\/revisions"}],"predecessor-version":[{"id":11860,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11858\/revisions\/11860"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/11859"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=11858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=11858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=11858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}