将Laravel日志推送到AWS CloudWatch不起作用

时间:2018-09-13 15:00:28

标签: php amazon-web-services laravel-5.4 amazon-cloudwatch

我正在尝试将laravel日志推送到Amazon CloudWatch中,我已经按照他们在以下文章中所说的去做: Big-O Notation

bootstrap / app.php 内部,我添加了以下代码:

$app->configureMonologUsing(function($monolog){
  try{  
    $options = [
      'region' => env('AWS_REGION'),
      'version' => 'latest',
      'credentials' => [
        'key' => env('AWS_KEY'),
        'secret' => env('AWS_SECRET')
      ]
    ];

    $cwClient = new CloudWatchLogsClient($options);
    $cwGroupName = $logGroupName;
    $cwStreamNameApp = $logStreamName;
    $cwRetentionDays = 90;
    $bubble = false;
    $cwHandlerApp = new CloudWatch($cwClient, $cwGroupName, $cwStreamNameApp, $cwRetentionDays, 10000, [], Logger::INFO, $bubble);
    $monolog->pushHandler($cwHandlerApp);
  }catch(\Exception $e){
    var_dump($e->getMessage());
  }
});

我还添加了一个测试路线,用于编写信息日志:

Route::get('/test', function () {
  Log::info('Clicking on test link!!!');
  return view('test');
});

但是没有任何反应,它甚至没有将日志写入laravel.logs文件或Amazon CloudWatch。

仅当我尝试通过以下代码将新日志手动写入Amazon CloudWatch时,它才起作用:

try {
  $options = [
    'region' => env('AWS_REGION'),
    'version' => 'latest',
    'credentials' => [
      'key' => env('AWS_KEY'),
      'secret' => env('AWS_SECRET')
    ]
  ];

  $client = new CloudWatchLogsClient($options);
  $result = $client->putLogEvents([
  'logGroupName' => $logGroupName,
  'logStreamName' => $logStreamName,
  'logEvents' => [
    [
      'message' => 'This is a test 1',
      'timestamp' => round(microtime(true) * 1000),
    ]
  ]
]);
} catch (\Aws\CloudWatchLogs\Exception\CloudWatchLogsException $e) {
  echo $e->getAwsErrorCode() . "\n";
  echo $e->getMessage() . "\n";
}

我的环境是Linux(ubuntu),使用php5.6,Laravel-5.4

谢谢!

-------更新-------

您可以在此处找到答案:

PHP application logging with Amazon CloudWatch Logs and Monolog

0 个答案:

没有答案