I am using Php to log something over AWS cloudwatch my application is deployed over AWS Elastic Beanstalk. I have followed the Aws doc, and implemented almost every step they have defined in there example.
Still I am not getting success in logging anything over Cloudwatch | Beanstalk.
My code -
<pre><?php
use Aws\CloudWatchLogs\CloudWatchLogsClient;
use Maxbanton\Cwh\Handler\CloudWatch;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogHandler;
$logFile = "testapp_local.log";
$appName = "TestApp01";
$facility = "local0";
$awsCredentials = [
'region' => 'eu-east-2',
'version' => 'latest',
'credentials' => [
'key' => 'KEY',
'secret' => 'Secret KEY'
]
];
$cwClient = new CloudWatchLogsClient($awsCredentials);
$cwGroupName = 'php-applog-test#--1';
$cwStreamNameInstance = 'some-stream-name-EC2-instance-1';
$cwStreamNameApp = "TestAuthenticationApp";
$cwRetentionDays = 90;
$cwHandlerInstanceNotice = new CloudWatch($cwClient, $cwGroupName, $cwStreamNameInstance, $cwRetentionDays, 10000, [ 'application' => 'DemoFormTwo' ],Logger::NOTICE);
$cwHandlerInstanceError = new CloudWatch($cwClient, $cwGroupName, $cwStreamNameInstance, $cwRetentionDays, 10000, [ 'application' => 'DemoFormTwo' ],Logger::ERROR);
$cwHandlerAppNotice = new CloudWatch($cwClient, $cwGroupName, $cwStreamNameApp, $cwRetentionDays, 10000, [ 'application' => 'DemoFormTwo' ],Logger::NOTICE);
$logger = new Logger('PHP Logging');
$formatter = new LineFormatter(null, null, false, true);
$syslogFormatter = new LineFormatter("%channel%: %level_name%: %message% %context% %extra%",null,false,true);
$infoHandler = new StreamHandler(__DIR__."/".$logFile, Logger::INFO);
$infoHandler->setFormatter($formatter);
$warnHandler = new SyslogHandler($appName, $facility, Logger::WARNING);
$warnHandler->setFormatter($syslogFormatter);
$cwHandlerInstanceNotice->setFormatter($formatter);
$cwHandlerInstanceError->setFormatter($formatter);
$cwHandlerAppNotice->setFormatter($formatter);
$logger->pushHandler($warnHandler);
$logger->pushHandler($infoHandler);
$logger->pushHandler($cwHandlerInstanceNotice);
$logger->pushHandler($cwHandlerInstanceError);
$logger->pushHandler($cwHandlerAppNotice);
$logger->info('Initial test of application logging.');
$logger->warn('Test of the warning system logging.');
$logger->notice('Application Auth Event: ',[ 'function'=>'login-action','result'=>'login-success' ]);
$logger->notice('Application Auth Event: ',[ 'function'=>'login-action','result'=>'login-failure' ]);
$logger->error('Application ERROR: System Error');
?>
I have also Installed all the given dependencies into the Beanstalk using PUTTY
Dependencies I have installed are -
curl -sS https://getcomposer.org/installer | php
php composer.phar require aws/aws-sdk-php
php composer.phar require monolog/monolog
php composer.phar require maxbanton/cwh:^1.0
docs I followed -
PHP application logging with Amazon CloudWatch Logs and Monolog | AWS Developer Blog[
^]
Anyone Please help me with this Thanks.
What I have tried:
I have tried to follow the docs, the logs are getting generated in my local machine but not on the cloudwatch. What should I do ?