1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Kernel\Services; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Kernel\DataStructures\Config; |
19
|
|
|
use Psr\Log\LoggerInterface; |
20
|
|
|
use Psr\Log\LoggerTrait; |
21
|
|
|
use Psr\Log\LogLevel; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Logger |
25
|
|
|
* |
26
|
|
|
* @package O2System\Kernel |
27
|
|
|
*/ |
28
|
|
|
class Logger extends LogLevel implements LoggerInterface |
29
|
|
|
{ |
30
|
|
|
use LoggerTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Logger Threshold |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $threshold = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Logger Path |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $path; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Logger Lines |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $lines = []; |
52
|
|
|
|
53
|
|
|
// ------------------------------------------------------------------------ |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Logger::__construct |
57
|
|
|
* |
58
|
|
|
* @param Config|null $config Logger configuration |
59
|
|
|
* |
60
|
|
|
* @return Logger |
61
|
|
|
*/ |
62
|
|
|
public function __construct(Config $config = null) |
63
|
|
|
{ |
64
|
|
|
if (isset($config->path)) { |
|
|
|
|
65
|
|
|
$this->path = $config->path; |
66
|
|
|
} elseif (defined('PATH_CACHE')) { |
67
|
|
|
$this->path = PATH_CACHE . 'logs' . DIRECTORY_SEPARATOR; |
|
|
|
|
68
|
|
|
} else { |
69
|
|
|
$this->path = dirname( |
70
|
|
|
$_SERVER[ 'SCRIPT_FILENAME' ] |
71
|
|
|
) . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (isset($config->threshold)) { |
|
|
|
|
75
|
|
|
if (is_array($config->threshold)) { |
76
|
|
|
$this->threshold = $config->threshold; |
77
|
|
|
} else { |
78
|
|
|
array_push($this->threshold, $config->threshold); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// ------------------------------------------------------------------------ |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Logger::getLines |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function getLines() |
91
|
|
|
{ |
92
|
|
|
return $this->lines; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// ------------------------------------------------------------------------ |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Logger::setThreshold |
99
|
|
|
* |
100
|
|
|
* Set logger threshold levels |
101
|
|
|
* |
102
|
|
|
* @param array $threshold |
103
|
|
|
*/ |
104
|
|
|
public function setThreshold(array $threshold) |
105
|
|
|
{ |
106
|
|
|
$this->threshold = $threshold; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// -------------------------------------------------------------------- |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Logger::addThreshold |
113
|
|
|
* |
114
|
|
|
* Add logger threshold levels |
115
|
|
|
* |
116
|
|
|
* @param string $threshold |
117
|
|
|
*/ |
118
|
|
|
public function addThreshold($threshold) |
119
|
|
|
{ |
120
|
|
|
array_push($this->threshold, $threshold); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// -------------------------------------------------------------------- |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Logger::setPath |
127
|
|
|
* |
128
|
|
|
* @param string $path |
129
|
|
|
*/ |
130
|
|
|
public function setPath($path) |
131
|
|
|
{ |
132
|
|
|
$this->path = $path; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// -------------------------------------------------------------------- |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Logger::log |
139
|
|
|
* |
140
|
|
|
* Logs with an arbitrary level. |
141
|
|
|
* |
142
|
|
|
* @param mixed $level |
143
|
|
|
* @param string $message |
144
|
|
|
* @param array $context |
145
|
|
|
* |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
public function log($level, $message, array $context = []) |
149
|
|
|
{ |
150
|
|
|
if ( ! in_array($level, $this->threshold)) { |
151
|
|
|
return false; |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// Try to get language message |
155
|
|
|
$langMessage = language()->getLine($message, $context); |
156
|
|
|
|
157
|
|
|
// Re-Define message |
158
|
|
|
$message = empty($langMessage) ? $message : $langMessage; |
159
|
|
|
|
160
|
|
|
if ( ! is_dir($this->path)) { |
161
|
|
|
mkdir($this->path, true, 0775); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$this->lines[] = new \ArrayObject([ |
165
|
|
|
'level' => strtoupper($level), |
166
|
|
|
'time' => date('r'), |
167
|
|
|
'message' => $message, |
168
|
|
|
], \ArrayObject::ARRAY_AS_PROPS); |
169
|
|
|
|
170
|
|
|
$isNewFile = false; |
171
|
|
|
$filePath = $this->path . 'log-' . date('d-m-Y') . '.log'; |
172
|
|
|
|
173
|
|
|
$log = ''; |
174
|
|
|
|
175
|
|
|
if ( ! is_file($filePath)) { |
176
|
|
|
$isNewFile = true; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if ( ! $fp = @fopen($filePath, 'ab')) { |
180
|
|
|
return false; |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$log .= strtoupper($level) . ' - ' . date('r') . ' --> ' . $message . "\n"; |
184
|
|
|
|
185
|
|
|
flock($fp, LOCK_EX); |
186
|
|
|
|
187
|
|
|
$result = null; |
188
|
|
|
for ($written = 0, $length = strlen($log); $written < $length; $written += $result) { |
189
|
|
|
if (($result = fwrite($fp, substr($log, $written))) === false) { |
190
|
|
|
break; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
flock($fp, LOCK_UN); |
195
|
|
|
fclose($fp); |
196
|
|
|
|
197
|
|
|
if ($isNewFile === true) { |
198
|
|
|
chmod($filePath, 0664); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return (bool)is_int($result); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// ------------------------------------------------------------------------ |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Logger::alert |
208
|
|
|
* |
209
|
|
|
* Action must be taken immediately. |
210
|
|
|
* |
211
|
|
|
* Example: Entire website down, database unavailable, etc. This should |
212
|
|
|
* trigger the SMS alerts and wake you up. |
213
|
|
|
* |
214
|
|
|
* @param string $message |
215
|
|
|
* @param array $context |
216
|
|
|
* |
217
|
|
|
* @return void |
218
|
|
|
*/ |
219
|
|
|
public function alert($message, array $context = []) |
220
|
|
|
{ |
221
|
|
|
$this->log(Logger::ALERT, $message, $context); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
// ------------------------------------------------------------------------ |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Logger::critical |
228
|
|
|
* |
229
|
|
|
* Critical conditions. |
230
|
|
|
* |
231
|
|
|
* Example: Application component unavailable, unexpected exception. |
232
|
|
|
* |
233
|
|
|
* @param string $message |
234
|
|
|
* @param array $context |
235
|
|
|
* |
236
|
|
|
* @return void |
237
|
|
|
*/ |
238
|
|
|
public function critical($message, array $context = []) |
239
|
|
|
{ |
240
|
|
|
$this->log(Logger::CRITICAL, $message, $context); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
// ------------------------------------------------------------------------ |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Logger::error |
247
|
|
|
* |
248
|
|
|
* Runtime errors that do not require immediate action but should typically |
249
|
|
|
* be logged and monitored. |
250
|
|
|
* |
251
|
|
|
* @param string $message |
252
|
|
|
* @param array $context |
253
|
|
|
* |
254
|
|
|
* @return void |
255
|
|
|
*/ |
256
|
|
|
public function error($message, array $context = []) |
257
|
|
|
{ |
258
|
|
|
$this->log(Logger::ERROR, $message, $context); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
// ------------------------------------------------------------------------ |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Logger::warning |
265
|
|
|
* |
266
|
|
|
* Exceptional occurrences that are not errors. |
267
|
|
|
* |
268
|
|
|
* Example: Use of deprecated APIs, poor use of an API, undesirable things |
269
|
|
|
* that are not necessarily wrong. |
270
|
|
|
* |
271
|
|
|
* @param string $message |
272
|
|
|
* @param array $context |
273
|
|
|
* |
274
|
|
|
* @return void |
275
|
|
|
*/ |
276
|
|
|
public function warning($message, array $context = []) |
277
|
|
|
{ |
278
|
|
|
$this->log(Logger::WARNING, $message, $context); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
// ------------------------------------------------------------------------ |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Logger::notice |
285
|
|
|
* |
286
|
|
|
* Normal but significant events. |
287
|
|
|
* |
288
|
|
|
* @param string $message |
289
|
|
|
* @param array $context |
290
|
|
|
* |
291
|
|
|
* @return void |
292
|
|
|
*/ |
293
|
|
|
public function notice($message, array $context = []) |
294
|
|
|
{ |
295
|
|
|
$this->log(Logger::NOTICE, $message, $context); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
// ------------------------------------------------------------------------ |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Logger::info |
302
|
|
|
* |
303
|
|
|
* Interesting events. |
304
|
|
|
* |
305
|
|
|
* Example: User logs in, SQL logs. |
306
|
|
|
* |
307
|
|
|
* @param string $message |
308
|
|
|
* @param array $context |
309
|
|
|
* |
310
|
|
|
* @return void |
311
|
|
|
*/ |
312
|
|
|
public function info($message, array $context = []) |
313
|
|
|
{ |
314
|
|
|
$this->log(Logger::INFO, $message, $context); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
// ------------------------------------------------------------------------ |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Logger::debug |
321
|
|
|
* |
322
|
|
|
* Detailed debug information. |
323
|
|
|
* |
324
|
|
|
* @param string $message |
325
|
|
|
* @param array $context |
326
|
|
|
* |
327
|
|
|
* @return void |
328
|
|
|
*/ |
329
|
|
|
public function debug($message, array $context = []) |
330
|
|
|
{ |
331
|
|
|
$this->log(Logger::DEBUG, $message, $context); |
332
|
|
|
} |
333
|
|
|
} |