![](/img/trans.png)
[英]Query Python logs in Glue Query with CloudWatch Insights
[英]Amazon CloudWatch Insights Query
我有這樣的日志:
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)
因此,正如您在消息中看到的那樣,它始終遵循以下模式:已完成 [錯誤代碼] [錯誤消息] ...
我正在使用此查詢來檢索具有某些錯誤代碼的日志:
fields @timestamp, @message
| filter @message like /401/
| sort @timestamp desc
| limit 20
但是如何解析消息以獲取錯誤代碼和消息的單獨字段?
您可以使用正則表達式語法parse
function: https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax.ZFC35FDC70D2269D23EZ888A3
像這樣的例子:
parse @message /Completed (?<errorCode>\d+) (?<errorMessage>.+) in (?<timeMilis>\d+)ms /
| filter isPresent(errorCode)
結果將是這樣的
-------------------------------------------------
| errorCode | errorMessage | timeMilis |
|-----------|-----------------------|-----------|
| 500 | Internal Server Error | 7 |
| 401 | Unauthorized | 0 |
-------------------------------------------------
這只會過濾和提取字段,您可以從那里進行進一步處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.