本文介绍了Laravel 5.6 AWS CloudWatch日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将幼虫从5.4升级到5.6.从5.6版开始,Laravel删除了$ app-> configureMonologUsing

Upgraded laravel from 5.4 to 5.6.Laravel removed $app->configureMonologUsing since version 5.6

aws的教程不再适用. https://aws.amazon.com/tw/blogs/developer/php-application-logging-with-amazon-cloudwatch-logs-and-monolog/

the tutorial from aws not applicable anymore.https://aws.amazon.com/tw/blogs/developer/php-application-logging-with-amazon-cloudwatch-logs-and-monolog/

任何人都可以建议我将$ app-> configureMonologUsing内部的逻辑迁移到哪里?

anyone can advise me where to migrate the logic inside $app->configureMonologUsing ?

谢谢

推荐答案

使用以下方法安装最新版本的CloudWatch处理程序库:

Install the latest version of CloudWatch handler library with:

composer require maxbanton/cwh

您可以在config/logging.php中添加custom频道,例如:

You can add a custom channel in config/logging.php like:

'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')
],

和工厂类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;
    }
}

这篇关于Laravel 5.6 AWS CloudWatch日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 20:02