15

I am trying to put Log on amazon CloudWatchLogs like this:

$response2 = $amzonLoger->putLogEvents([

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

but always i've this response :

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

Can you help me understanding what does mean ["rejectedLogEventsInfo"]=> array(1) { ["tooOldLogEventEndIndex"]=> int(1)?

peterh
  • 11,875
  • 18
  • 85
  • 108
irvinstone
  • 593
  • 4
  • 10

3 Answers3

25

I found the solution adding this line instead of time() function of php . following this example cloudWatchLogs.

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

I hope that this can help someone on the future.

peterh
  • 11,875
  • 18
  • 85
  • 108
irvinstone
  • 593
  • 4
  • 10
7

Cloudwatch API expects timestamp in epoch milliseconds, not seconds. Took me a while to figure that out. Thanks to the PHP snippet above.

Luke
  • 22,826
  • 31
  • 110
  • 193
Heitor Morgado
  • 111
  • 1
  • 6
  • 5
    Actually, milliseconds -- but this comment is what worked it out for me. – Max Gasner Jun 03 '19 at 21:20
  • I corrected the answer to milliseconds in case anyone's confused about the above comment. For anyone interested, it's documented [here](https://docs.aws.amazon.com/cli/latest/reference/logs/put-log-events.html) that it's milliseconds – Luke Feb 16 '22 at 20:59
2

Your error is telling you that the timestamp you are using is not good.

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

From doc:

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.

If using current time and current time is correct you may be in a different time zone (that is more than 2 hours ahead of UTC). Use UTC time for events timestamp.

Mircea
  • 10,216
  • 2
  • 30
  • 46