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