|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Logfile\Monolog; |
|
4
|
|
|
|
|
5
|
|
|
use Logfile\Logfile; |
|
6
|
|
|
use Logfile\Payload; |
|
7
|
|
|
use Monolog\Handler\AbstractProcessingHandler; |
|
8
|
|
|
use Monolog\Logger; |
|
9
|
|
|
|
|
10
|
|
|
class LogfileHandler extends AbstractProcessingHandler |
|
11
|
|
|
{ |
|
12
|
|
|
protected $logfile; |
|
13
|
|
|
|
|
14
|
|
|
protected $levelMap = array( |
|
15
|
|
|
Logger::DEBUG => 'debug', |
|
16
|
|
|
Logger::INFO => 'info', |
|
17
|
|
|
Logger::NOTICE => 'info', |
|
18
|
|
|
Logger::WARNING => 'warning', |
|
19
|
|
|
Logger::ERROR => 'error', |
|
20
|
|
|
Logger::CRITICAL => 'critical', |
|
21
|
|
|
Logger::ALERT => 'critical', |
|
22
|
|
|
Logger::EMERGENCY => 'critical', |
|
23
|
|
|
); |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(Logfile $logfile, int $level = Logger::DEBUG, bool $bubble = true) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->logfile = $logfile; |
|
28
|
|
|
parent::__construct($level, $bubble); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected function write(array $record) |
|
32
|
|
|
{ |
|
33
|
|
|
$config = clone $this->logfile->getConfig(); |
|
34
|
|
|
|
|
35
|
|
|
$context = array_filter(array_merge( |
|
36
|
|
|
array_diff_key($record['context'], array_flip(['exception'])), |
|
37
|
|
|
$record['extra'], |
|
38
|
|
|
array_diff_key($record, array_flip(['context', 'extra', 'message', 'level', 'level_name', 'formatted', 'datetime'])) |
|
39
|
|
|
)); |
|
40
|
|
|
|
|
41
|
|
|
foreach ($context as $key => $value) { |
|
42
|
|
|
$config->addTag($key, $value); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (isset($record['context']['exception']) && ($record['context']['exception'] instanceof \Throwable)) { |
|
46
|
|
|
$payload = Payload::createFromException($record['context']['exception'], $config); |
|
47
|
|
|
} else { |
|
48
|
|
|
$payload = new Payload($record['message'], $config); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (isset($record['datetime'])) { |
|
52
|
|
|
$payload->setDateTime($record['datetime']); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (isset($record['level'])) { |
|
56
|
|
|
$payload->setSeverity($this->levelMap[$record['level']]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$this->logfile->log($payload); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|