1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Logger\Manager; |
4
|
|
|
|
5
|
|
|
use Monolog\Formatter\LineFormatter; |
6
|
|
|
use Monolog\Handler\FormattableHandlerInterface; |
7
|
|
|
use Monolog\Handler\HandlerInterface; |
8
|
|
|
use Monolog\Logger as Monolog; |
9
|
|
|
use Nip\Config\Config; |
10
|
|
|
use Psr\Log\InvalidArgumentException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class MonologWrappers |
14
|
|
|
* @package Nip\Logger\Manager |
15
|
|
|
*/ |
16
|
|
|
trait MonologWrappers |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Create an instance of any handler available in Monolog. |
20
|
|
|
* |
21
|
|
|
* @param array $config |
22
|
|
|
* @return \Psr\Log\LoggerInterface |
23
|
|
|
* |
24
|
|
|
* @throws \InvalidArgumentException |
25
|
|
|
*/ |
26
|
2 |
|
protected function createMonologDriver($config) |
27
|
|
|
{ |
28
|
2 |
|
$config = $config instanceof Config ? $config->toArray() : $config; |
|
|
|
|
29
|
|
|
|
30
|
2 |
|
if (!is_a($config['handler'], HandlerInterface::class, true)) { |
31
|
|
|
throw new InvalidArgumentException( |
32
|
|
|
$config['handler'].' must be an instance of '.HandlerInterface::class |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
$with = array_merge( |
37
|
2 |
|
['level' => $this->level($config)], |
|
|
|
|
38
|
2 |
|
$config['with'] ?? [], |
39
|
2 |
|
$config['handler_with'] ?? [] |
40
|
|
|
); |
41
|
|
|
|
42
|
2 |
|
return new Monolog($this->parseChannel($config), [ |
|
|
|
|
43
|
2 |
|
$this->prepareHandler( |
44
|
2 |
|
$this->getContainer()->get($config['handler'], $with), |
|
|
|
|
45
|
2 |
|
$config |
46
|
|
|
), |
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Prepare the handlers for usage by Monolog. |
52
|
|
|
* |
53
|
|
|
* @param array $handlers |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
protected function prepareHandlers(array $handlers) |
57
|
|
|
{ |
58
|
|
|
foreach ($handlers as $key => $handler) { |
59
|
|
|
$handlers[$key] = $this->prepareHandler($handler); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $handlers; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Prepare the handler for usage by Monolog. |
67
|
|
|
* |
68
|
|
|
* @param \Monolog\Handler\HandlerInterface $handler |
69
|
|
|
* @param array $config |
70
|
|
|
* @return \Monolog\Handler\HandlerInterface |
71
|
|
|
*/ |
72
|
4 |
|
protected function prepareHandler(HandlerInterface $handler, array $config = []) |
73
|
|
|
{ |
74
|
4 |
|
$isHandlerFormattable = false; |
75
|
|
|
|
76
|
4 |
|
if (Monolog::API === 1) { |
|
|
|
|
77
|
|
|
$isHandlerFormattable = true; |
78
|
4 |
|
} elseif (Monolog::API === 2 && $handler instanceof FormattableHandlerInterface) { |
79
|
4 |
|
$isHandlerFormattable = true; |
80
|
|
|
} |
81
|
|
|
|
82
|
4 |
|
if ($isHandlerFormattable && !isset($config['formatter'])) { |
83
|
4 |
|
$handler->setFormatter($this->formatter()); |
|
|
|
|
84
|
|
|
} elseif ($isHandlerFormattable && $config['formatter'] !== 'default') { |
85
|
|
|
$handler->setFormatter($this->app->make($config['formatter'], $config['formatter_with'] ?? [])); |
86
|
|
|
} |
87
|
|
|
|
88
|
4 |
|
return $handler; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get a Monolog formatter instance. |
93
|
|
|
* |
94
|
|
|
* @return \Monolog\Formatter\FormatterInterface |
95
|
|
|
*/ |
96
|
4 |
|
protected function formatter() |
97
|
|
|
{ |
98
|
4 |
|
$formatter = new LineFormatter(null, $this->dateFormat, true, true); |
99
|
4 |
|
$formatter->includeStacktraces(); |
100
|
|
|
|
101
|
4 |
|
return $formatter; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|