How to Push logs to Amazon Cloudwatch in a custom plugin in WordPress?
AWS
Amazon Web Services is a secure cloud services platform that offers database storage, compute power, content delivery, other storage & security functionalities and AWS services.
Cloudwatch
Amazon Cloudwatch is a monitoring and observability service that monitors the AWS resources and applications which run on AWS. It collects the operational and monitoring data in the form of logs, metrics, and events.
Hence, using Cloudwatch, we can observe the anomalous behavior of our resources/applications, set alarms, check logs/metrics and troubleshoot the issue so that our application runs smoothly.
Integrating cloud watch with WordPress
We need to follow these steps to integrate it with WordPress –
1. Install the plugin –
To use the Amazon SDK in a WordPress site we need to install the AWS plugin that is Amazon Web Services in our application’s admin dashboard.
2. AWS Settings –
When the AWS plugin gets successfully activated, it will create the AWS menu item in the dashboard.
On clicking this menu, we will be redirected to the Amazon access key page.
Here, we will provide our AWS account Access Key ID and Secret Access Key and Click on the Save Changes to save them.
3. Push the logs to the cloud watch –
Now we will push our WordPress application logs to the Amazon Cloudwatch. For that, we will use the CloudWatchLogsClient amazon service that needs a Log Group and Log stream to push the logs.
Note: We can also create multiple streams for a single application but there must be a single group for a single application. So, before that simply include the service path at the top of the file in which we are going to use this service.
use Aws\CloudWatchLogs\CloudWatchLogsClient;
We will need to create a client object using CloudWatchLogsClient class, provide the region parameter value(search the list of regions used in AWS) and credentials(that we have submitted in step 2)
$client = CloudWatchLogsClient::factory(array(
'region' => 'us-east-1',
'credentials' => array(
'key' => "******************",
'secret' => ""**********************************",
)
));
There are two options to create log group and streams on AWS:
- These can be created manually on AWS.
We can create the log groups and streams manually from the AWS dashboard. In the AWS cloud watch dashboard, a table containing all the log groups will be displayed, and there is “Actions” dropdown on the top of the table, select “Create log group” from it and give a proper group name and create it.
After creating a log group, simply select it from the log groups table and you will see the log streams table and few buttons on top of it(check screenshot), manage the log streams using the given buttons.
- using the code – in this case, that code snippet should run only once in the application that will create a group and the stream.
$client->createLogGroup(array(
'logGroupName' => 'test_log_group',
));
$client->createLogStream(array(
'logGroupName' => 'test_log_group',
'logStreamName' => 'Testing error log stream',
));
This will create a log group “test_log_group” and a log stream “Testing error log stream” inside it.
- Put log messages in the cloudwatch
$awsresult = $client->putLogEvents(array(
'logGroupName' => 'test_log_group',
'logStreamName' => 'Testing error log stream',
'logEvents' => array(
array(
'timestamp' => round(microtime(true) * 1000),
'message' => 'This is a testing watchlog message.',
),
),
)); - To put the logs in the same stream we will need the Sequence Token of the previous log event.
$result = $client->describeLogStreams(array(
'logGroupName' => 'test_log_group',
));
$awsresult = $client->putLogEvents(array(
'logGroupName' => 'test_log_group',
'logStreamName' => 'Testing error log stream',
'logEvents' => array(
array(
'timestamp' => round(microtime(true) * 1000),
'message' => 'This is third testing watchlog error messages.',
),
),
'sequenceToken' => $result['logStreams'][0]['uploadSequenceToken'],
));
And these will be written inside the same stream of that particular log group if you will not create a new one.
- Put log messages in the cloudwatch
In this way, the WordPress application logs can be written in the Amazon Cloudwatch and hence one can easily monitor the application without any hassle.