Вход Регистрация
Файл: vendor/monolog/monolog/src/Monolog/Handler/MongoDBHandler.php
Строк: 116
<?php declare(strict_types=1);

/*
 * This file is part of the Monolog package.
 *
 * (c) Jordi Boggiano <j.boggiano@seld.be>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace MonologHandler;

use 
MongoDBDriverBulkWrite;
use 
MongoDBDriverManager;
use 
MongoDBClient;
use 
MonologLevel;
use 
MonologFormatterFormatterInterface;
use 
MonologFormatterMongoDBFormatter;
use 
MonologLogRecord;

/**
 * Logs to a MongoDB database.
 *
 * Usage example:
 *
 *   $log = new MonologLogger('application');
 *   $client = new MongoDBClient('mongodb://localhost:27017');
 *   $mongodb = new MonologHandlerMongoDBHandler($client, 'logs', 'prod');
 *   $log->pushHandler($mongodb);
 *
 * The above examples uses the MongoDB PHP library's client class; however, the
 * MongoDBDriverManager class from ext-mongodb is also supported.
 */
class MongoDBHandler extends AbstractProcessingHandler
{
    private 
MongoDBCollection $collection;

    private 
Client|Manager $manager;

    private 
string|null $namespace null;

    
/**
     * Constructor.
     *
     * @param Client|Manager $mongodb    MongoDB library or driver client
     * @param string         $database   Database name
     * @param string         $collection Collection name
     */
    
public function __construct(Client|Manager $mongodbstring $databasestring $collectionint|string|Level $level Level::Debugbool $bubble true)
    {
        if (
$mongodb instanceof Client) {
            
$this->collection $mongodb->selectCollection($database$collection);
        } else {
            
$this->manager $mongodb;
            
$this->namespace $database '.' $collection;
        }

        
parent::__construct($level$bubble);
    }

    protected function 
write(LogRecord $record): void
    
{
        if (isset(
$this->collection)) {
            
$this->collection->insertOne($record->formatted);
        }

        if (isset(
$this->manager$this->namespace)) {
            
$bulk = new BulkWrite;
            
$bulk->insert($record->formatted);
            
$this->manager->executeBulkWrite($this->namespace$bulk);
        }
    }

    
/**
     * @inheritDoc
     */
    
protected function getDefaultFormatter(): FormatterInterface
    
{
        return new 
MongoDBFormatter;
    }
}
Онлайн: 1
Реклама