1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace leinonen\Yii2Monolog; |
6
|
|
|
|
7
|
|
|
use Monolog\Logger; |
8
|
|
|
use yii\log\Target; |
9
|
|
|
use leinonen\Yii2Monolog\Factories\MonologFactory; |
10
|
|
|
|
11
|
|
|
class MonologTarget extends Target |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Logger |
15
|
|
|
*/ |
16
|
|
|
private $logger; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $handlers = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $processors = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $channel = 'main'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var MonologFactory |
35
|
|
|
*/ |
36
|
|
|
private $monologFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Initializes a new MonologTarget. |
40
|
|
|
* |
41
|
|
|
* @param MonologFactory $monologFactory |
42
|
|
|
* @param array $config |
43
|
|
|
*/ |
44
|
1 |
|
public function __construct(MonologFactory $monologFactory, $config = []) |
45
|
|
|
{ |
46
|
1 |
|
$this->monologFactory = $monologFactory; |
47
|
1 |
|
parent::__construct($config); |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
* @throws \InvalidArgumentException |
53
|
|
|
* @throws \yii\base\InvalidConfigException |
54
|
|
|
*/ |
55
|
1 |
|
public function init() |
56
|
|
|
{ |
57
|
1 |
|
$this->logger = $this->monologFactory->make( |
58
|
1 |
|
$this->channel, |
59
|
1 |
|
$this->handlers, |
60
|
1 |
|
$this->processors |
61
|
|
|
); |
62
|
|
|
|
63
|
1 |
|
parent::init(); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
1 |
|
public function export() |
70
|
|
|
{ |
71
|
1 |
|
foreach ($this->getMessages() as $message) { |
72
|
1 |
|
$this->logger->log($message->getPsr3LogLevel(), $message->getMessage(), $message->getContext()); |
73
|
|
|
} |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param array $handlers |
78
|
|
|
*/ |
79
|
1 |
|
public function setHandlers(array $handlers): void |
80
|
|
|
{ |
81
|
1 |
|
$this->handlers = $handlers; |
82
|
1 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param array $processors |
86
|
|
|
*/ |
87
|
1 |
|
public function setProcessors(array $processors): void |
88
|
|
|
{ |
89
|
1 |
|
$this->processors = $processors; |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $channel |
94
|
|
|
*/ |
95
|
1 |
|
public function setChannel(string $channel): void |
96
|
|
|
{ |
97
|
1 |
|
$this->channel = $channel; |
98
|
1 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns the messages from the Target wrapped in Yii2LogMessage. |
102
|
|
|
* |
103
|
|
|
* @return Yii2LogMessage[]|array |
104
|
|
|
*/ |
105
|
1 |
|
private function getMessages(): array |
106
|
|
|
{ |
107
|
1 |
|
return \array_map( |
108
|
1 |
|
function ($message) { |
109
|
1 |
|
return new Yii2LogMessage($message); |
110
|
1 |
|
}, |
111
|
1 |
|
$this->messages |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|