Passed
Branch master (5c167b)
by smiley
02:26
created

Log   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Test Coverage

Coverage 6.38%

Importance

Changes 0
Metric Value
wmc 19
dl 0
loc 201
ccs 3
cts 47
cp 0.0638
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A alert() 0 2 1
A addInstance() 0 4 1
A error() 0 2 1
A warning() 0 2 1
A critical() 0 2 1
A debug() 0 2 1
A info() 0 2 1
A __destruct() 0 2 1
A log() 0 4 2
A close() 0 9 3
A removeInstance() 0 9 2
A emergency() 0 2 1
A notice() 0 2 1
A getInstance() 0 7 2
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, LogLevel};
17
18
/**
19
 */
20
class Log implements LoggerInterface{
21
22
	/**
23
	 * @var \chillerlan\Logger\LogOptions
24
	 */
25
	protected $options;
26
27
	/**
28
	 * @var \chillerlan\Logger\Output\LogOutputInterface[]
29
	 */
30
	protected $instances = [];
31
32
	/**
33
	 *
34
	 */
35
	public function __destruct(){
36
		$this->close();
37
	}
38
39
	/**
40
	 * closes all remaining logger instances
41
	 *
42
	 * @return \chillerlan\Logger\Log
43
	 */
44
	public function close():Log{
45
46
		if(!empty($this->instances)){
47
			foreach($this->instances as $instanceName => $instance){
48
				unset($this->instances[$instanceName]);
49
			}
50
		}
51
52
		return $this;
53
	}
54
55
	/**
56
	 * @param \chillerlan\Logger\Output\LogOutputInterface $logger
57
	 * @param string                                       $instanceName
58
	 *
59
	 * @return \chillerlan\Logger\Log
60
	 */
61 1
	public function addInstance(LogOutputInterface $logger, string $instanceName = null):Log{
62 1
		$this->instances[$instanceName ?? 'default'] = $logger;
63
64 1
		return $this;
65
	}
66
67
	/**
68
	 * @param string $instanceName
69
	 *
70
	 * @return \chillerlan\Logger\Output\LogOutputInterface
71
	 * @throws \chillerlan\Logger\LogException
72
	 */
73
	public function getInstance(string $instanceName):LogOutputInterface{
74
75
		if(!array_key_exists($instanceName, $this->instances)){
76
			throw new LogException('invalid logger instance: '.$instanceName);
77
		}
78
79
		return $this->instances[$instanceName];
80
	}
81
82
	/**
83
	 * @param string $instanceName
84
	 *
85
	 * @return \chillerlan\Logger\Log
86
	 * @throws \chillerlan\Logger\LogException
87
	 */
88
	public function removeInstance(string $instanceName):Log{
89
90
		if(!array_key_exists($instanceName, $this->instances)){
91
			throw new LogException('invalid logger instance: '.$instanceName);
92
		}
93
94
		unset($this->instances[$instanceName]);
95
96
		return $this;
97
	}
98
99
	/**
100
	 * Logs with an arbitrary level.
101
	 *
102
	 * @param mixed  $level
103
	 * @param string $message
104
	 * @param array  $context
105
	 *
106
	 * @return void
107
	 */
108
	public function log($level, $message, array $context = []){
109
110
		foreach($this->instances as $logger){
111
			$logger->log($level, $message, $context);
112
		}
113
114
	}
115
116
	/**
117
	 * System is unusable.
118
	 *
119
	 * @param string $message
120
	 * @param array  $context
121
	 *
122
	 * @return void
123
	 */
124
	public function emergency($message, array $context = []){
125
		$this->log(LogLevel::EMERGENCY, $message, $context);
126
	}
127
128
	/**
129
	 * Action must be taken immediately.
130
	 *
131
	 * Example: Entire website down, database unavailable, etc. This should
132
	 * trigger the SMS alerts and wake you up.
133
	 *
134
	 * @param string $message
135
	 * @param array  $context
136
	 *
137
	 * @return void
138
	 */
139
	public function alert($message, array $context = []){
140
		$this->log(LogLevel::ALERT, $message, $context);
141
	}
142
143
	/**
144
	 * Critical conditions.
145
	 *
146
	 * Example: Application component unavailable, unexpected exception.
147
	 *
148
	 * @param string $message
149
	 * @param array  $context
150
	 *
151
	 * @return void
152
	 */
153
	public function critical($message, array $context = []){
154
		$this->log(LogLevel::CRITICAL, $message, $context);
155
	}
156
157
	/**
158
	 * Runtime errors that do not require immediate action but should typically
159
	 * be logged and monitored.
160
	 *
161
	 * @param string $message
162
	 * @param array  $context
163
	 *
164
	 * @return void
165
	 */
166
	public function error($message, array $context = []){
167
		$this->log(LogLevel::ERROR, $message, $context);
168
	}
169
170
	/**
171
	 * Exceptional occurrences that are not errors.
172
	 *
173
	 * Example: Use of deprecated APIs, poor use of an API, undesirable things
174
	 * that are not necessarily wrong.
175
	 *
176
	 * @param string $message
177
	 * @param array  $context
178
	 *
179
	 * @return void
180
	 */
181
	public function warning($message, array $context = []){
182
		$this->log(LogLevel::WARNING, $message, $context);
183
	}
184
185
	/**
186
	 * Normal but significant events.
187
	 *
188
	 * @param string $message
189
	 * @param array  $context
190
	 *
191
	 * @return void
192
	 */
193
	public function notice($message, array $context = []){
194
		$this->log(LogLevel::NOTICE, $message, $context);
195
	}
196
197
	/**
198
	 * Interesting events.
199
	 *
200
	 * Example: User logs in, SQL logs.
201
	 *
202
	 * @param string $message
203
	 * @param array  $context
204
	 *
205
	 * @return void
206
	 */
207
	public function info($message, array $context = []){
208
		$this->log(LogLevel::INFO, $message, $context);
209
	}
210
211
	/**
212
	 * Detailed debug information.
213
	 *
214
	 * @param string $message
215
	 * @param array  $context
216
	 *
217
	 * @return void
218
	 */
219
	public function debug($message, array $context = []){
220
		$this->log(LogLevel::DEBUG, $message, $context);
221
	}
222
223
}
224