MonologHandler.php 1.1 KB
<?php

namespace Lackoxygen\ExceptionPush\Channels;

use Illuminate\Support\Arr;
use Lackoxygen\ExceptionPush\Dispatcher;
use Lackoxygen\ExceptionPush\ExceptionPush;
use Monolog\Handler\AbstractSyslogHandler;
use Monolog\Logger;

/**
 *    'papertrail' => [
 *          'driver' => 'monolog',
 *          'level' => env('LOG_LEVEL', 'debug'),
 *          'handler' => SyslogUdpHandler::class,
 *          'handler_with' => [
 *              'channels' => [Wx::class, Ding::class]
 *          ],
 *  ],
 */
class MonologHandler extends AbstractSyslogHandler
{
    protected \Closure $dispatcher;

    protected array $channels = [];

    public function __construct(
        array $channels,
        $facility = LOG_USER,
        $level = Logger::DEBUG,
        bool $bubble = true
    ) {
        parent::__construct($facility, $level, $bubble);

        $this->dispatcher = (new Dispatcher)->default();

        $this->channels = $channels;
    }

    protected function write(array $record): void
    {
        $agents = Arr::only(ExceptionPush::config('agents'), $this->channels);
        call_user_func($this->dispatcher, $agents, $record);
    }
}