Log新增MongoDB驅動2

tanke發表於2020-09-10

由於之前的配置導致執行

php artisan config:cache

丟擲異常: LogicException : Your configuration files are not serializable.
稍加修改

namespace App\Utils;

use MongoDB\Client;
use MongoDB\Exception\InvalidArgumentException;
use Monolog\Handler\MongoDBHandler as MonologMongoDBHandler;
use Monolog\Logger;

class MongoDBHandler extends MonologMongoDBHandler
{
    public function __construct($uri, string $database, string $collection, $level = Logger::DEBUG, bool $bubble = true)
    {
        try {
            $client = new Client('mongodb://'.$uri);
        } catch (InvalidArgumentException $e) {
            throw new \InvalidArgumentException('MongoDB\Client or MongoDB\Driver\Manager instance required');
        }
        parent::__construct($client, $database, $collection, $level, $bubble);
    }
}

    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['mongodb','single','slack'],
            'ignore_exceptions' => false,
        ],
        ...
        'mongodb' => [
            'driver' => 'monolog',
            'handler' => App\Utils\MongoDBHandler::class,
            'formatter' => 'default',
            'handler_with' => [
                'uri' => new \MongoDB\Client("mongodb://<user>:<pwd>@<host>:<prot>"),
                'database' => "<庫>",
                'collection' => '<collection>',
            ],
        ],

再次執行

php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章