PHP 中的 Amazon CloudWatchLogs putLogEvents 给出错误 tooOldLogEventEndIndex

Amazon CloudWatchLogs putLogEvents in PHP gives error tooOldLogEventEndIndex

我正在尝试像这样将日志放在亚马逊 CloudWatchLogs 上:

$response2 = $amzonLoger->putLogEvents([

            'logGroupName' => 'myGroup',
            'logStreamName' => 'myStream',
            'logEvents' => [
                [
                    'timestamp' => time(),
                    'message' => 'message'
                ],
            ],
            'sequenceToken' => lastToken,
        ]);
        var_dump($response2);

但我总是得到这样的回应:

bject(Guzzle\Service\Resource\Model)#289 (2) { ["structure":protected]=> NULL ["data":protected]=> array(2) { ["nextSequenceToken"]=> string(56) "495401145812734324234234236420825819917076850" ["rejectedLogEventsInfo"]=> array(1) { ["tooOldLogEventEndIndex"]=> int(1) } } }

你能帮我理解一下 ["rejectedLogEventsInfo"]=> array(1) { ["tooOldLogEventEndIndex"]=> int(1) 是什么意思吗?

您的错误提示您使用的时间戳不正确。

http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html

来自文档:

None of the log events in the batch can be more than 2 hours in the future.
None of the log events in the batch can be older than 14 days or the retention period of the log group.

如果使用当前时间并且当前时间是正确的,则您可能处于不同的时区(比 UTC 早 2 个多小时)。使用 UTC 时间作为事件时间戳。

我找到了添加此行而不是 php 的 time() 函数的解决方案。在这个 example cloudWatchLogs.

之后
'timestamp' =>  round(microtime(true) * 1000),

希望对以后的人有所帮助。

Cloudwatch API 需要时间戳毫秒,而不是秒。我花了一段时间才弄明白。感谢上面的 PHP 片段。