0

我有这样的日志:

I, [2020-06-17T09:32:48.100103 #9]  INFO -- : [54b35e04-9c19-443d-adff-b2c3192b5590] Completed 500 Internal Server Error in 7ms (ActiveRecord: 2.3ms | Allocations: 1705)

I, [2020-06-17T10:37:27.169909 #9]  INFO -- : [c800e9ce-fba3-4e1a-a19f-526f32746925] Completed 401 Unauthorized in 0ms (ActiveRecord: 0.0ms | Allocations: 115)

因此,正如您在消息中看到的那样,它始终遵循以下模式: Completed [THE ERROR CODE] [ERROR MESSAGE] ...

我正在使用此查询来检索具有某些错误代码的日志:

fields @timestamp, @message
| filter @message like /401/
| sort @timestamp desc
| limit 20

但是如何解析消息以获取错误代码和消息的单独字段?

4

1 回答 1

0

您可以使用parse具有正则表达式语法的函数:https ://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax.html

像这样的例子:

parse @message /Completed (?<errorCode>\d+) (?<errorMessage>.+) in (?<timeMilis>\d+)ms /
| filter isPresent(errorCode)

结果将是这样的

-------------------------------------------------
| errorCode |     errorMessage      | timeMilis |
|-----------|-----------------------|-----------|
| 500       | Internal Server Error | 7         |
| 401       | Unauthorized          | 0         |
-------------------------------------------------

这只会过滤和提取字段,您可以从那里进行进一步处理。

于 2020-06-24T16:49:15.177 回答