Laravel 5.6 aws cloudwatch 日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50814388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.6 aws cloudwatch log
提问by Alex Spring
Upgraded laravel from 5.4 to 5.6. Laravel removed $app->configureMonologUsing since version 5.6
将 Laravel 从 5.4 升级到 5.6。Laravel 从 5.6 版本开始移除 $app->configureMonologUsing
the tutorial from aws not applicable anymore. https://aws.amazon.com/tw/blogs/developer/php-application-logging-with-amazon-cloudwatch-logs-and-monolog/
aws 的教程不再适用。 https://aws.amazon.com/tw/blogs/developer/php-application-logging-with-amazon-cloudwatch-logs-and-monolog/
anyone can advise me where to migrate the logic inside $app->configureMonologUsing ?
任何人都可以建议我将 $app->configureMonologUsing 中的逻辑迁移到哪里?
thanks
谢谢
回答by Ignacio Alles
Install the latest version of CloudWatch handler library with:
使用以下命令安装最新版本的 CloudWatch 处理程序库:
composer require maxbanton/cwh
You can add a custom
channel in config/logging.php
like:
您可以在以下位置添加custom
频道config/logging.php
:
'cloudwatch' => [
'driver' => 'custom',
'via' => \App\Logging\CloudWatchLoggerFactory::class,
'sdk' => [
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'version' => 'latest',
'credentials' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY')
]
],
'retention' => env('CLOUDWATCH_LOG_RETENTION',7),
'level' => env('CLOUDWATCH_LOG_LEVEL','error')
],
and a factory class App/Logging/CloudWatchLoggerFactory.php
as:
和一个工厂类App/Logging/CloudWatchLoggerFactory.php
:
<?php
namespace App\Logging;
use Aws\CloudWatchLogs\CloudWatchLogsClient;
use Maxbanton\Cwh\Handler\CloudWatch;
use Monolog\Logger;
class CloudWatchLoggerFactory
{
/**
* Create a custom Monolog instance.
*
* @param array $config
* @return \Monolog\Logger
*/
public function __invoke(array $config)
{
$sdkParams = $config["sdk"];
$tags = $config["tags"] ?? [ ];
$name = $config["name"] ?? 'cloudwatch';
// Instantiate AWS SDK CloudWatch Logs Client
$client = new CloudWatchLogsClient($sdkParams);
// Log group name, will be created if none
$groupName = config('app.name') . '-' . config('app.env');
// Log stream name, will be created if none
$streamName = config('app.hostname');
// Days to keep logs, 14 by default. Set to `null` to allow indefinite retention.
$retentionDays = $config["retention"];
// Instantiate handler (tags are optional)
$handler = new CloudWatch($client, $groupName, $streamName, $retentionDays, 10000, $tags);
// Create a log channel
$logger = new Logger($name);
// Set handler
$logger->pushHandler($handler);
return $logger;
}
}
回答by Bira
IF you are using Laravel on AWS ECS.
如果您在 AWS ECS 上使用 Laravel。
Inside .env
file add this LOG_CHANNEL=stderr
内部.env
文件添加这个LOG_CHANNEL=stderr
It will write logs to CloudWatch when you configure the Task Definition.
当您配置任务定义时,它会将日志写入 CloudWatch。
回答by Juha Vehnia
If you are running on AWS EC2 instances and log a lot of info / debug messages sending logs real time can slow down your application response times. Instead you can have CloudWatch agent watching your laravel.log to send new log entries in i.e. every 5 seconds. CloudWatch agent can ship all your system logs and any system metrics like CPU so you can create cloud watch alerts.
如果您在 AWS EC2 实例上运行并记录大量信息/调试消息,则实时发送日志会减慢您的应用程序响应时间。相反,您可以让 CloudWatch 代理监视您的 laravel.log 以每 5 秒发送一次新的日志条目。CloudWatch 代理可以传送您的所有系统日志和任何系统指标(例如 CPU),以便您可以创建云监视警报。