CloudWatchLogs client

This page contains examples with the CloudWatchLogs client. See the client introduction for a more detailed description how to use a client. You may also want to consider the authentication documentation to understand the many ways you can authenticate with AWS.

The CloudWatchLogs package could be installed with Composer.

composer require async-aws/cloud-watch-logs

A new client object may be instantiated by:

use AsyncAws\CloudWatchLogs\CloudWatchLogsClient; $cloudWatchLogs = new CloudWatchLogsClient();

The authentication parameters is read from the environment by default. You can also specify a AWS access id and secret:

use AsyncAws\CloudWatchLogs\CloudWatchLogsClient; $cloudWatchLogs = new CloudWatchLogsClient([ 'accessKeyId' => 'my_access_key', 'accessKeySecret' => 'my_access_secret', 'region' => 'eu-central-1', ]);

For all available options, see the configuration reference.

Usage

List log streams

use AsyncAws\CloudWatchLogs\CloudWatchLogsClient; use AsyncAws\CloudWatchLogs\Input\DescribeLogStreamsRequest; $cloudWatchLogs = new CloudWatchLogsClient(); $result = $cloudWatchLogs->describeLogStreams(new DescribeLogStreamsRequest([ 'logGroupName' => 'company-website', ])); foreach ($result->getLogStreams() as $stream) { echo '-'.$stream->getLogStreamName().PHP_EOL; }

Push logs

use AsyncAws\CloudWatchLogs\CloudWatchLogsClient; use AsyncAws\CloudWatchLogs\Input\PutLogEventsRequest; use AsyncAws\CloudWatchLogs\ValueObject\InputLogEvent; $cloudWatchLogs = new CloudWatchLogsClient(); // sequenceToken is not required for a new stream $currentSequenceToken = null; $result = $cloudWatchLogs->putLogEvents(new PutLogEventsRequest([ 'logGroupName' => 'company-website', 'logStreamName' => 'frontend-api', 'logEvents' => [ new InputLogEvent([ 'timestamp' => date('U.u') * 1000, 'message' => 'an error occurred', ]), ], 'sequenceToken' => $currentSequenceToken, ])); $currentSequenceToken = $result->getNextSequenceToken();

The source code to this page is found on GitHub.