Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
35 / 35 |
|
100.00% |
18 / 18 |
CRAP | |
100.00% |
1 / 1 |
Logger | |
100.00% |
35 / 35 |
|
100.00% |
18 / 18 |
27 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
addWriters | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
addWriter | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getWriters | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setLogLimit | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
setTimestampFormat | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getTimestampFormat | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLevel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
getLogLevel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
emergency | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
alert | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
critical | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
error | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
warning | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
notice | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
info | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
debug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
log | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Log; |
15 | |
16 | /** |
17 | * Logger class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Log |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 3.3.0 |
25 | */ |
26 | class Logger |
27 | { |
28 | |
29 | /** |
30 | * Constants for log levels |
31 | * @var int |
32 | */ |
33 | const EMERGENCY = 0; |
34 | const ALERT = 1; |
35 | const CRITICAL = 2; |
36 | const ERROR = 3; |
37 | const WARNING = 4; |
38 | const NOTICE = 5; |
39 | const INFO = 6; |
40 | const DEBUG = 7; |
41 | |
42 | /** |
43 | * Message level short codes |
44 | * @var array |
45 | */ |
46 | protected $levels = [ |
47 | 0 => 'EMERGENCY', |
48 | 1 => 'ALERT', |
49 | 2 => 'CRITICAL', |
50 | 3 => 'ERROR', |
51 | 4 => 'WARNING', |
52 | 5 => 'NOTICE', |
53 | 6 => 'INFO', |
54 | 7 => 'DEBUG' |
55 | ]; |
56 | |
57 | /** |
58 | * Log writers |
59 | * @var array |
60 | */ |
61 | protected $writers = []; |
62 | |
63 | /** |
64 | * Log timestamp format |
65 | * @var string |
66 | */ |
67 | protected $timestampFormat = 'Y-m-d H:i:s'; |
68 | |
69 | /** |
70 | * Constructor |
71 | * |
72 | * Instantiate the logger object |
73 | * |
74 | * @param Writer\WriterInterface|array $writer |
75 | * @param string $timestampFormat |
76 | */ |
77 | public function __construct($writer = null, $timestampFormat = 'Y-m-d H:i:s') |
78 | { |
79 | if (null !== $timestampFormat) { |
80 | $this->setTimestampFormat($timestampFormat); |
81 | } |
82 | |
83 | if (null !== $writer) { |
84 | if (is_array($writer)) { |
85 | $this->addWriters($writer); |
86 | } else { |
87 | $this->addWriter($writer); |
88 | } |
89 | } |
90 | } |
91 | |
92 | /** |
93 | * Add log writers |
94 | * |
95 | * @param array $writers |
96 | * @return Logger |
97 | */ |
98 | public function addWriters(array $writers) |
99 | { |
100 | foreach ($writers as $writer) { |
101 | $this->addWriter($writer); |
102 | } |
103 | return $this; |
104 | } |
105 | |
106 | /** |
107 | * Add a log writer |
108 | * |
109 | * @param Writer\WriterInterface $writer |
110 | * @return Logger |
111 | */ |
112 | public function addWriter(Writer\WriterInterface $writer) |
113 | { |
114 | $this->writers[] = $writer; |
115 | return $this; |
116 | } |
117 | |
118 | /** |
119 | * Get all log writers |
120 | * |
121 | * @return array |
122 | */ |
123 | public function getWriters() |
124 | { |
125 | return $this->writers; |
126 | } |
127 | |
128 | /** |
129 | * Set log level limit for all log writers |
130 | * |
131 | * @param int $level |
132 | * @return Logger |
133 | */ |
134 | public function setLogLimit($level) |
135 | { |
136 | foreach ($this->writers as $writer) { |
137 | $writer->setLogLimit($level); |
138 | } |
139 | return $this; |
140 | } |
141 | |
142 | /** |
143 | * Set timestamp format |
144 | * |
145 | * @param string $format |
146 | * @return Logger |
147 | */ |
148 | public function setTimestampFormat($format = 'Y-m-d H:i:s') |
149 | { |
150 | $this->timestampFormat = $format; |
151 | return $this; |
152 | } |
153 | |
154 | /** |
155 | * Get timestamp format |
156 | * |
157 | * @return string |
158 | */ |
159 | public function getTimestampFormat() |
160 | { |
161 | return $this->timestampFormat; |
162 | } |
163 | |
164 | /** |
165 | * Get level |
166 | * |
167 | * @param int $level |
168 | * @return string |
169 | */ |
170 | public function getLevel($level) |
171 | { |
172 | return (isset($this->levels[(int)$level])) ? $this->levels[(int)$level] : ''; |
173 | } |
174 | |
175 | /** |
176 | * Static method to get log level |
177 | * |
178 | * @param int $level |
179 | * @return string |
180 | */ |
181 | public static function getLogLevel($level) |
182 | { |
183 | return (new self())->getLevel($level); |
184 | } |
185 | |
186 | /** |
187 | * Add an EMERGENCY log entry |
188 | * |
189 | * @param mixed $message |
190 | * @param array $context |
191 | * @return Logger |
192 | */ |
193 | public function emergency($message, array $context = []) |
194 | { |
195 | return $this->log(self::EMERGENCY, $message, $context); |
196 | } |
197 | |
198 | /** |
199 | * Add an ALERT log entry |
200 | * |
201 | * @param mixed $message |
202 | * @param array $context |
203 | * @return Logger |
204 | */ |
205 | public function alert($message, array $context = []) |
206 | { |
207 | return $this->log(self::ALERT, $message, $context); |
208 | } |
209 | |
210 | /** |
211 | * Add a CRITICAL log entry |
212 | * |
213 | * @param mixed $message |
214 | * @param array $context |
215 | * @return Logger |
216 | */ |
217 | public function critical($message, array $context = []) |
218 | { |
219 | return $this->log(self::CRITICAL, $message, $context); |
220 | } |
221 | |
222 | /** |
223 | * Add an ERROR log entry |
224 | * |
225 | * @param mixed $message |
226 | * @param array $context |
227 | * @return Logger |
228 | */ |
229 | public function error($message, array $context = []) |
230 | { |
231 | return $this->log(self::ERROR, $message, $context); |
232 | } |
233 | |
234 | /** |
235 | * Add a WARNING log entry |
236 | * |
237 | * @param mixed $message |
238 | * @param array $context |
239 | * @return Logger |
240 | */ |
241 | public function warning($message, array $context = []) |
242 | { |
243 | return $this->log(self::WARNING, $message, $context); |
244 | } |
245 | |
246 | /** |
247 | * Add a NOTICE log entry |
248 | * |
249 | * @param mixed $message |
250 | * @param array $context |
251 | * @return Logger |
252 | */ |
253 | public function notice($message, array $context = []) |
254 | { |
255 | return $this->log(self::NOTICE, $message, $context); |
256 | } |
257 | |
258 | /** |
259 | * Add an INFO log entry |
260 | * |
261 | * @param mixed $message |
262 | * @param array $context |
263 | * @return Logger |
264 | */ |
265 | public function info($message, array $context = []) |
266 | { |
267 | return $this->log(self::INFO, $message, $context); |
268 | } |
269 | |
270 | /** |
271 | * Add a DEBUG log entry |
272 | * |
273 | * @param mixed $message |
274 | * @param array $context |
275 | * @return Logger |
276 | */ |
277 | public function debug($message, array $context = []) |
278 | { |
279 | return $this->log(self::DEBUG, $message, $context); |
280 | } |
281 | |
282 | /** |
283 | * Add a log entry |
284 | * |
285 | * @param mixed $level |
286 | * @param mixed $message |
287 | * @param array $context |
288 | * @return Logger |
289 | */ |
290 | public function log($level, $message, array $context = []) |
291 | { |
292 | if (!isset($context['timestamp'])) { |
293 | $context['timestamp'] = date($this->timestampFormat); |
294 | } |
295 | if (!isset($context['name'])) { |
296 | $context['name'] = $this->levels[$level]; |
297 | } |
298 | |
299 | foreach ($this->writers as $writer) { |
300 | $writer->writeLog($level, (string)$message, $context); |
301 | } |
302 | |
303 | return $this; |
304 | } |
305 | |
306 | } |