简体   繁体   English

无法使用 PHP 登录 AWS Cloudwatch?

[英]Can't able to log over AWS Cloudwatch using PHP?

I am using PHP to log something over cloudwatch, my application is deployed over AWS Elastic Beanstalk.我正在使用 PHP 通过 cloudwatch 记录某些内容,我的应用程序部署在 AWS Elastic Beanstalk 上。 I followed the AWS Dev Doc, I tried to implement all the things that have defined there but still I didn't get anything logged over cloudwatch neither on beanstalk logs.我遵循了 AWS Dev Doc,我尝试实现那里定义的所有内容,但我仍然没有在 cloudwatch 上记录任何内容,也没有在 beanstalk 日志上记录任何内容。 The log file are creating in my local system with all the logs in it, what is the issue there?日志文件正在我的本地系统中创建,其中包含所有日志,那里有什么问题? someone please help me.有人请帮助我。

Doc - LINK文档 - 链接

Code -代码 -

<?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');
?>

I have installed我已经安装

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

These dependencies on the beanstalk.这些依赖于beantalk。 I am using monolog (composer) as given in the doc example.我正在使用文档示例中给出的独白(作曲家)。 The issue here is the logs are generating just in my local system but not in the Cloudwatch how can I make them appear over CW ?这里的问题是日志只在我的本地系统中生成,而不是在 Cloudwatch 中我怎样才能让它们出现在 CW 上?

Looks like there is an issue with how credentials must be sent: if you are not using the profile key and also you don't have an AWS credential file in your $HOME the connection to CloudWatch is not made.看起来必须如何发送凭证存在问题:如果您没有使用profile密钥,并且您的$HOME中没有 AWS 凭证文件,则不会建立与 CloudWatch 的连接。

To avoid that validation try this:为避免该验证,请尝试以下操作:

$awsCredentials = [
'use_aws_shared_config_files' => false,
'region' => 'eu-east-2',
'version' => 'latest',
'credentials' => [
'key' => 'KEY',
'secret' => 'Secret KEY'
]
];

By the way, use_aws_shared_config_files works with true or false .顺便说一句, use_aws_shared_config_filestruefalse一起使用。

We have those versions on composer.json :我们在composer.json上有这些版本:

"aws/aws-sdk-php": "^3.24",
 "monolog/monolog": "^2.6",
 "maxbanton/cwh": "^2.0"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM