1

documentation使用例如直線,在lambda函數,我把:如何從LAMBDA過濾CloudWatch的日誌用JSON度量濾波器

console.log(
     { 
      "eventType": "UpdateTrail", 
      "sourceIPAddress": "111.111.111.111", 
      "arrayKey": [ 
       "value", 
       "another value" 
      ], 
      "objectList": [ 
       { 
       "name": "a", 
       "id": 1 
       }, 
       { 
       "name": "b", 
       "id": 2 
       } 
      ], 
      "SomeObject": null, 
      "ThisFlag": true 
     }) 

我然後創建CloudWatch的一個日誌度量濾波器與過濾模式爲在文檔例如規定:

{ $.eventType = "UpdateTrail" } 

過濾器不產生這樣的文檔度量說它應該 - 這裏是輸出:

2017-10-23T13:27:19.320Z 1143e2b0-eea6-4225-88c0-efcd79055f7b { eventType: 'UpdateTrail', 
sourceIPAddress: '111.111.111.111', 
arrayKey: [ 'value', 'another value' ], 
objectList: [ { name: 'a', id: 1 }, { name: 'b', id: 2 } ], 
SomeObject: null, 
ThisFlag: true } 

因此,您可以看到時間戳和標識符被預置爲JSON。

Amazon Cloudwatch log filtering - JSON syntax中的答案表示這是因爲Lambda將日誌轉換爲字符串。 How to parse mixed text and JSON log entries in AWS CloudWatch for Log Metric Filter也差不多。兩種情況都不提供解決方案。如何使用JSON度量標準過濾器從Lambda過濾CloudWatch日誌?

回答

2

看看日誌行實際是什麼樣子。如果你看到這樣的事情,它不是一個有效的JSON:

{ eventType: 'UpdateTrail', ... } 

你需要的是這樣的(注意引號):

{ "eventType": "UpdateTrail", ...} 

要得到的是,儘量包住你的對象JSON.stringify(),像這樣:

console.log(
     JSON.stringify(
      { 
       "eventType": "UpdateTrail", 
       "sourceIPAddress": "111.111.111.111", 
       "arrayKey": [ 
        "value", 
        "another value" 
       ], 
       "objectList": [ 
        { 
        "name": "a", 
        "id": 1 
        }, 
        { 
        "name": "b", 
        "id": 2 
        } 
       ], 
       "SomeObject": null, 
       "ThisFlag": true 
      } 
     ) 
    ) 
+0

就是這樣。我已將無效的JSON日誌輸出添加到問題描述中。我沒有發現它是無效的,需要進行串接。應該早點嘗試。謝謝。 – Dana

+0

https://stackoverflow.com/questions/33903735/amazon-cloudwatch-log-filtering-json-syntax確實有沒有stringify有效的JSON - 任何想法爲什麼區別?顯然,過濾器並沒有產生一個事件。 – Dana

+0

這個問題差不多2年,可能是CloudWatch Logs或Lambda在此期間發生了變化。 – Tartaglia