1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class Log |
4
|
|
|
* |
5
|
|
|
* @filesource Log.php |
6
|
|
|
* @created 04.01.2018 |
7
|
|
|
* @package chillerlan\Logger |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2018 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\Logger; |
14
|
|
|
|
15
|
|
|
use chillerlan\Logger\Output\LogOutputInterface; |
16
|
|
|
use Psr\Log\{LoggerInterface, LoggerTrait}; |
17
|
|
|
|
18
|
|
|
class Log implements LoggerInterface{ |
19
|
|
|
use LoggerTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \chillerlan\Logger\Output\LogOutputInterface[] |
23
|
|
|
*/ |
24
|
|
|
protected $logOutputInterfaces = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
public function __destruct(){ |
30
|
|
|
$this->close(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* closes all remaining logger instances |
35
|
|
|
* |
36
|
|
|
* @return \chillerlan\Logger\Log |
37
|
|
|
*/ |
38
|
|
|
public function close():Log{ |
39
|
|
|
|
40
|
|
|
if(!empty($this->logOutputInterfaces)){ |
41
|
|
|
foreach($this->logOutputInterfaces as $instanceName => $instance){ |
42
|
|
|
unset($this->logOutputInterfaces[$instanceName]); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param \chillerlan\Logger\Output\LogOutputInterface $logger |
51
|
|
|
* @param string $instanceName |
52
|
|
|
* |
53
|
|
|
* @return \chillerlan\Logger\Log |
54
|
|
|
*/ |
55
|
|
|
public function addInstance(LogOutputInterface $logger, string $instanceName = null):Log{ |
56
|
|
|
$this->logOutputInterfaces[$instanceName ?? 'default'] = $logger; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
/** |
62
|
1 |
|
* @param string $instanceName |
63
|
|
|
* |
64
|
1 |
|
* @return \chillerlan\Logger\Output\LogOutputInterface |
65
|
|
|
* @throws \chillerlan\Logger\LogException |
66
|
|
|
*/ |
67
|
|
|
public function getInstance(string $instanceName):LogOutputInterface{ |
68
|
|
|
|
69
|
|
|
if(!array_key_exists($instanceName, $this->logOutputInterfaces)){ |
70
|
|
|
throw new LogException('invalid logger instance: '.$instanceName); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->logOutputInterfaces[$instanceName]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $instanceName |
78
|
|
|
* |
79
|
|
|
* @return \chillerlan\Logger\Log |
80
|
|
|
* @throws \chillerlan\Logger\LogException |
81
|
|
|
*/ |
82
|
|
|
public function removeInstance(string $instanceName):Log{ |
83
|
|
|
|
84
|
|
|
if(!array_key_exists($instanceName, $this->logOutputInterfaces)){ |
85
|
|
|
throw new LogException('invalid logger instance: '.$instanceName); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
unset($this->logOutputInterfaces[$instanceName]); |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Logs with an arbitrary level. |
95
|
|
|
* |
96
|
|
|
* @param mixed $level |
97
|
|
|
* @param string $message |
98
|
|
|
* @param array $context |
99
|
|
|
* |
100
|
|
|
* @param string|null $instance |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
public function log($level, $message, array $context = [], string $instance = null):void{ |
105
|
|
|
|
106
|
|
|
foreach($this->logOutputInterfaces as $logger){ |
107
|
|
|
$logger->log($level, $message, $context); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|