首页 > 解决方案 > 无法使用 PHP 登录 AWS Cloudwatch?

问题描述

我正在使用 PHP 通过 cloudwatch 记录某些内容,我的应用程序部署在 AWS Elastic Beanstalk 上。我遵循了 AWS Dev Doc,我尝试实现那里定义的所有内容,但我仍然没有在 cloudwatch 上记录任何内容,也没有在 beanstalk 日志上记录任何内容。日志文件正在我的本地系统中创建,其中包含所有日志,那里有什么问题?有人请帮助我。

文档 -链接

代码 -

<?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);
// Log group name, will be created if none
$cwGroupName = 'php-applog-test#--1';

// Log stream name, will be created if none
$cwStreamNameInstance = 'some-stream-name-EC2-instance-1';
// Instance ID as log stream name
$cwStreamNameApp = "TestAuthenticationApp";
// Days to keep logs, 14 by default
$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');
?>

我已经安装

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

这些依赖于beantalk。我正在使用文档示例中给出的独白(作曲家)。这里的问题是日志只在我的本地系统中生成,而不是在 Cloudwatch 中我怎样才能让它们出现在 CW 上?

标签: phpamazon-web-servicesamazon-ec2amazon-elastic-beanstalkaws-php-sdk

解决方案


推荐阅读