aws cloudwatch 日志过滤模式包括匹配模式之后 and/or 之前的行

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

有没有办法在 AWS CloudWatch Logs 中的匹配模式之后 and/or 之前包含 N 行?

假设我有这个查询并且希望在每场比赛前后有 3 行。

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

目前我唯一的解决方法是删除过滤器模式并使用 grep:

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

但是,我只想流式传输我需要的日志事件,并将其作为 aws 日志事件查询的一部分。

AWS 目前不支持此功能(confirmed by a contributor), but there is an open GitHub issue 跟踪该功能以便将来可能支持此功能。

同时,在问题中提到的流式传输日志后,您将不得不求助于过滤。

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

我遇到了完全相同的问题,发现 按时间戳搜索 在一定程度上解决了我的问题。

CloudWatch 具有“Logs Insights”,这是一个流畅的日志搜索器。

通过关键字搜索并获取时间戳,然后使用时间戳过滤附近的日志。