作者 竞泽

add:remote debug

1 <?php 1 <?php
2 2
3 -namespace Lackoxygen\ExceptionPush\Channels; 3 +namespace Lackoxygen\ExceptionPush\Handler;
4 4
5 use Illuminate\Support\Arr; 5 use Illuminate\Support\Arr;
6 use Lackoxygen\ExceptionPush\Dispatcher; 6 use Lackoxygen\ExceptionPush\Dispatcher;
7 use Lackoxygen\ExceptionPush\ExceptionPush; 7 use Lackoxygen\ExceptionPush\ExceptionPush;
  8 +use Monolog\Formatter\FormatterInterface;
  9 +use Monolog\Formatter\LineFormatter;
8 use Monolog\Handler\AbstractSyslogHandler; 10 use Monolog\Handler\AbstractSyslogHandler;
9 use Monolog\Logger; 11 use Monolog\Logger;
10 12
@@ -20,10 +22,22 @@ use Monolog\Logger; @@ -20,10 +22,22 @@ use Monolog\Logger;
20 */ 22 */
21 class MonologHandler extends AbstractSyslogHandler 23 class MonologHandler extends AbstractSyslogHandler
22 { 24 {
  25 + /**
  26 + * @var \Closure
  27 + */
23 protected \Closure $dispatcher; 28 protected \Closure $dispatcher;
24 29
  30 + /**
  31 + * @var array
  32 + */
25 protected array $channels = []; 33 protected array $channels = [];
26 34
  35 + /**
  36 + * @param array $channels
  37 + * @param $facility
  38 + * @param $level
  39 + * @param bool $bubble
  40 + */
27 public function __construct( 41 public function __construct(
28 array $channels, 42 array $channels,
29 $facility = LOG_USER, 43 $facility = LOG_USER,
@@ -32,14 +46,30 @@ class MonologHandler extends AbstractSyslogHandler @@ -32,14 +46,30 @@ class MonologHandler extends AbstractSyslogHandler
32 ) { 46 ) {
33 parent::__construct($facility, $level, $bubble); 47 parent::__construct($facility, $level, $bubble);
34 48
35 - $this->dispatcher = (new Dispatcher)->default(); 49 + $this->dispatcher = Dispatcher::callback();
36 50
37 $this->channels = $channels; 51 $this->channels = $channels;
38 } 52 }
39 53
  54 + /**
  55 + * @param array $record
  56 + *
  57 + * @return void
  58 + */
40 protected function write(array $record): void 59 protected function write(array $record): void
41 { 60 {
42 $agents = Arr::only(ExceptionPush::config('agents'), $this->channels); 61 $agents = Arr::only(ExceptionPush::config('agents'), $this->channels);
43 - call_user_func($this->dispatcher, $agents, $record); 62 +
  63 + $callback = $this->dispatcher;
  64 +
  65 + $callback(ExceptionPush::getAgents($agents), [$record['formatted'] ?? '']);
  66 + }
  67 +
  68 + /**
  69 + * {@inheritDoc}
  70 + */
  71 + protected function getDefaultFormatter(): FormatterInterface
  72 + {
  73 + return new LineFormatter('%channel%.%level_name%: %message% %context% %extra%');
44 } 74 }
45 } 75 }
@@ -2,6 +2,11 @@ @@ -2,6 +2,11 @@
2 2
3 namespace Lackoxygen\ExceptionPush; 3 namespace Lackoxygen\ExceptionPush;
4 4
  5 +use Lackoxygen\ExceptionPush\Agents\Ding;
  6 +use Lackoxygen\ExceptionPush\Agents\Wx;
  7 +use Lackoxygen\ExceptionPush\Handler\MonologHandler;
  8 +use Monolog\Logger;
  9 +
5 if (!function_exists('closure')) { 10 if (!function_exists('closure')) {
6 function closure($closure): ?\Closure 11 function closure($closure): ?\Closure
7 { 12 {
@@ -24,3 +29,13 @@ if (!function_exists('closure')) { @@ -24,3 +29,13 @@ if (!function_exists('closure')) {
24 return $closure instanceof \Closure ? $closure : null; 29 return $closure instanceof \Closure ? $closure : null;
25 } 30 }
26 } 31 }
  32 +
  33 +
  34 +if (!function_exists('notify')) {
  35 + function notify($message, $label = 'info')
  36 + {
  37 + $log = new Logger('notify');
  38 + $log->pushHandler(new MonologHandler([Ding::class, Wx::class]));
  39 + $log->{$label}($message);
  40 + }
  41 +}