district5/mondoc-log-handler

适用于Mondoc库的Monolog兼容日志处理程序。

1.0.1 2024-07-25 07:02 UTC

This package is auto-updated.

Last update: 2024-09-25 07:15:59 UTC


README

CI

此库是一个Monolog处理程序,它使用Mondoc库将日志记录到MongoDB集合。它通过提供使用Mondoc将日志记录到MongoDB集合的处理程序来补充Monolog库。

安装...

composer require district5/mondoc-log-handler

用法...

<?php
use District5\MondocLogHandler\Handler\MondocLogHandler;
use District5\MondocLogHandler\MondocLogConfig;
use Monolog\Level;
use Monolog\Logger;

// Set up MondocConfig
$client = new \MongoDB\Client('mongodb://:27017');
\District5\Mondoc\MondocConfig::getInstance()->addDatabase(
    $client->selectDatabase('my_database'),
    'default' // This is the default connection id
)

// Set up MondocLogConfig
$config = MondocLogConfig::getInstance()->setConnectionId(
    'default' // This is the default connection id
)->setCollectionName(
    'mondoc_log' // This is the default collection name
);

// Set up the logger
$logger = new Logger('my_app');

$handler = new MondocLogHandler(
    $level = Level::Debug,
    $bubble = true
);
$handler->setFormatter(
    new LineFormatter('%message%') // This is the default formatter and format
);
$logger->pushHandler($handler);
$logger->info('A test log from MondocLogHandler');
$lastLogModel = $handler->getLastLog();

echo $lastLogModel->getMessage(); // 'A test log from MondocLogHandler'

测试...

composer install
./vendor/bin/phpunit