作者 竞泽

add:handler

@@ -20,15 +20,18 @@ class ExceptionPush @@ -20,15 +20,18 @@ class ExceptionPush
20 { 20 {
21 $this->parser = new Parser; 21 $this->parser = new Parser;
22 22
23 - $this->agents = $this->getAgents(); 23 + $this->agents = static::getAgents();
24 } 24 }
25 25
  26 +
26 /** 27 /**
  28 + * @param $options
  29 + *
27 * @return array 30 * @return array
28 */ 31 */
29 - protected function getAgents(): array 32 + public static function getAgents($options = null): array
30 { 33 {
31 - $agentOpts = (array) static::config('agents'); 34 + $agentOpts = $options ? : (array) static::config('agents');
32 35
33 $agents = []; 36 $agents = [];
34 37
  1 +<?php
  2 +
  3 +namespace Lackoxygen\ExceptionPush\Channels;
  4 +
  5 +use Illuminate\Support\Arr;
  6 +use Lackoxygen\ExceptionPush\Dispatcher;
  7 +use Lackoxygen\ExceptionPush\ExceptionPush;
  8 +use Monolog\Handler\AbstractSyslogHandler;
  9 +use Monolog\Logger;
  10 +
  11 +/**
  12 + * 'papertrail' => [
  13 + * 'driver' => 'monolog',
  14 + * 'level' => env('LOG_LEVEL', 'debug'),
  15 + * 'handler' => SyslogUdpHandler::class,
  16 + * 'handler_with' => [
  17 + * 'channels' => [Wx::class, Ding::class]
  18 + * ],
  19 + * ],
  20 + */
  21 +class MonologHandler extends AbstractSyslogHandler
  22 +{
  23 + protected \Closure $dispatcher;
  24 +
  25 + protected array $channels = [];
  26 +
  27 + public function __construct(
  28 + array $channels,
  29 + $facility = LOG_USER,
  30 + $level = Logger::DEBUG,
  31 + bool $bubble = true
  32 + ) {
  33 + parent::__construct($facility, $level, $bubble);
  34 +
  35 + $this->dispatcher = (new Dispatcher)->default();
  36 +
  37 + $this->channels = $channels;
  38 + }
  39 +
  40 + protected function write(array $record): void
  41 + {
  42 + $agents = Arr::only(ExceptionPush::config('agents'), $this->channels);
  43 + call_user_func($this->dispatcher, $agents, $record);
  44 + }
  45 +}