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