Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws cloudwatch logs filter pattern include lines before and/or after matching pattern

Is there a way to include N lines before and/or after a matching pattern in AWS CloudWatch Logs?

Let's say I have this query and would like 3 lines before and after each match.

aws logs filter-log-events --log-group-name my-group --filter-pattern "mypattern"

The only work around I have at the moment is to remove the filter pattern and use grep:

aws logs filter-log-events --log-group-name my-group | grep -A 3 -B 3 mypattern

However, I would like to only stream the log events I need and do it as part of the aws log events query.

like image 900
haitham Avatar asked Dec 06 '19 16:12

haitham


People also ask

When creating a custom metric filter in CloudWatch logs what is filtered?

You can search and filter the log data coming into CloudWatch Logs by creating one or more metric filters. Metric filters define the terms and patterns to look for in log data as it is sent to CloudWatch Logs.

How do I filter CloudWatch logs by timestamp?

Filtering on timestamp is done with the range selector on the top right in the Logs Insights Console or with the startTime and endTime parameters on the StartQuery API.

How do you aggregate CloudWatch logs?

To run a query with an aggregation function Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/ . In the navigation pane, choose Logs, and then choose Logs Insights. In the Select log group(s) drop down, choose one or more log groups to query.


1 Answers

This is currently not supported by AWS (confirmed by a contributor), but there is an open GitHub issue for tracking the feature to potentially support this in the future.

In the meantime, you will have to resort to filtering after the logs are streamed as mentioned in the question.

aws logs filter-log-events --log-group-name my-group | grep -A 3 -B 3 mypattern
like image 153
haitham Avatar answered Sep 20 '22 19:09

haitham