1
|
|
|
<?php |
2
|
|
|
/******************************************************************** |
3
|
|
|
* Created by: Marko Kungla @ OkramLabs on Aug 6, 2012 - 9:12:38 |
4
|
|
|
* Contact: [email protected] - https://okramlabs.com |
5
|
|
|
* @copyright: 2015 OkramLabs - https://okramlabs.com |
6
|
|
|
* @license MIT |
7
|
|
|
* |
8
|
|
|
* Package name: libhowi-filesystem |
9
|
|
|
* @category HOWI3 |
10
|
|
|
* @package libhowi |
11
|
|
|
* @subpackage filesystem |
12
|
|
|
* |
13
|
|
|
* Lang: PHP |
14
|
|
|
* Encoding: UTF-8 |
15
|
|
|
* File: TraitForLog.inc |
16
|
|
|
* @link https://github.com/okramlabs/libhowi-filesystem |
17
|
|
|
******************************************************************** |
18
|
|
|
* Contributors: |
19
|
|
|
* @author Marko Kungla <[email protected]> |
20
|
|
|
* Github: https://github.com/mkungla |
21
|
|
|
******************************************************************** |
22
|
|
|
* Comments: |
23
|
|
|
* 1. This response trait is adjusted to follow structure of PSR-3. psr/log |
24
|
|
|
* see - https://github.com/php-fig/log |
25
|
|
|
*/ |
26
|
|
|
namespace HOWI3\libhowi\Filesystem\Commons; |
27
|
|
|
|
28
|
|
|
trait TraitForResponse |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Following is optional to have user id and username attached to log |
33
|
|
|
* Following can be anything you want for example your currently logged in |
34
|
|
|
* user info or system user running the script. |
35
|
|
|
* |
36
|
|
|
* @var unknown |
37
|
|
|
*/ |
38
|
|
|
private $UID = 0; |
39
|
|
|
|
40
|
|
|
private $username = 'anonymous'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Loglevel defaults to 4 |
44
|
|
|
* so $EMERGENCY, $ALERT, $CRITICAL, $ERROR are loged |
45
|
|
|
* |
46
|
|
|
* @var int $loglevel 0 - 8 (none - all) |
47
|
|
|
*/ |
48
|
|
|
private $loglevel = 4; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Path to file if you want to log messages to file |
52
|
|
|
* |
53
|
|
|
* @var string $log_to_file |
54
|
|
|
*/ |
55
|
|
|
private $log_to_file = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* |
59
|
|
|
* @var responseObject as soon first msg is logged |
60
|
|
|
*/ |
61
|
|
|
protected $response = false; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* System is unusable, something happend which should not happen |
65
|
|
|
* |
66
|
|
|
* @var array $EMERGENCY |
67
|
|
|
*/ |
68
|
|
|
private $EMERGENCY = array( |
69
|
|
|
1 => 'emergency', |
70
|
|
|
100 => 'You need minimum php version 5.4' |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Action must be taken immediately. |
75
|
|
|
* |
76
|
|
|
* Example: Entire website down, database unavailable, etc. This should |
77
|
|
|
* trigger the SMS alerts and wake you up. |
78
|
|
|
* |
79
|
|
|
* @var array $ALERT |
80
|
|
|
*/ |
81
|
|
|
private $ALERT = array( |
82
|
|
|
2 => 'alert', |
83
|
|
|
200 => 'No such file or directory' |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Critical conditions. |
88
|
|
|
* |
89
|
|
|
* Example: Application component unavailable, unexpected exception. |
90
|
|
|
* |
91
|
|
|
* @var array $CRITICAL |
92
|
|
|
*/ |
93
|
|
|
private $CRITICAL = array( |
94
|
|
|
3 => 'critical' |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Runtime errors that do not require immediate action but should typically |
99
|
|
|
* be logged and monitored. |
100
|
|
|
* |
101
|
|
|
* @var array $ERROR |
102
|
|
|
*/ |
103
|
|
|
private $ERROR = array( |
104
|
|
|
4 => 'error', |
105
|
|
|
400 => 'Logfile location is invalid' |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Exceptional occurrences that are not errors. |
110
|
|
|
* |
111
|
|
|
* Example: Use of deprecated APIs, poor use of an API, undesirable things |
112
|
|
|
* that are not necessarily wrong. |
113
|
|
|
* |
114
|
|
|
* @var array $WARNING |
115
|
|
|
*/ |
116
|
|
|
private $WARNING = array( |
117
|
|
|
5 => 'warning', |
118
|
|
|
500 => 'Changing the working directory failed!', |
119
|
|
|
501 => 'Changed the temporary directory failed! Destination not writable: ', |
120
|
|
|
502 => 'Permission denied or parent of directory does not exist if creating recursive directories (recursive = false) ', |
121
|
|
|
503 => 'Can not create the file, permission denied or parent directory does not exist : ', |
122
|
|
|
504 => 'Failed to create file : ', |
123
|
|
|
505 => 'Reading info - Permission denied. : ', |
124
|
|
|
506 => 'Failed to create structure from array!' |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Normal but significant events. |
129
|
|
|
* |
130
|
|
|
* @var array $NOTICE |
131
|
|
|
*/ |
132
|
|
|
private $NOTICE = array( |
133
|
|
|
6 => 'notice', |
134
|
|
|
601 => 'Called file object without filename!', |
135
|
|
|
602 => 'Called info object without basename!' |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Interesting events. |
140
|
|
|
* |
141
|
|
|
* Example: User logs in, SQL logs. |
142
|
|
|
* |
143
|
|
|
* @var array $INFO |
144
|
|
|
*/ |
145
|
|
|
private $INFO = array( |
146
|
|
|
7 => 'info', |
147
|
|
|
701 => 'Created new directory : ', |
148
|
|
|
702 => 'Removed directory : ', |
149
|
|
|
703 => 'Created new file : ', |
150
|
|
|
704 => 'Created new diectory structure : ' |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Detailed debug information. |
155
|
|
|
* |
156
|
|
|
* @var array $DEBUG |
157
|
|
|
*/ |
158
|
|
|
private $DEBUG = array( |
159
|
|
|
8 => 'debug', |
160
|
|
|
800 => 'Creating directory with empty path should not be called', |
161
|
|
|
801 => 'libhowi-filesystem OK', |
162
|
|
|
802 => 'Tried to access not directly accessible function which should be private function', |
163
|
|
|
803 => 'Changed the temporary directory success', |
164
|
|
|
804 => 'Changed the temporaty directory by sys_get_temp_dir()', |
165
|
|
|
805 => 'Returned current temporaty directory : ', |
166
|
|
|
806 => 'Created new temporary name for file : ', |
167
|
|
|
807 => 'Loaded directory object', |
168
|
|
|
808 => 'Loaded file object', |
169
|
|
|
809 => 'Loaded info object', |
170
|
|
|
810 => 'Creating structure from array!' |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Add message to log |
175
|
|
|
* |
176
|
|
|
* @param array $LEVEL |
177
|
|
|
* from private static $LEVEL; |
178
|
|
|
* @param number $code |
|
|
|
|
179
|
|
|
* @param string $context |
180
|
|
|
* @param string $append_msg |
|
|
|
|
181
|
|
|
* @return void |
182
|
|
|
*/ |
183
|
731 |
|
private function addTrace($LEVEL, $code = 0, $context = "not specified", $append_msg = false, $to_file = false) |
184
|
|
|
{ |
185
|
731 |
|
$this->response = new ResponseObject(); |
186
|
|
|
|
187
|
731 |
|
$this->response->setCode($code); |
188
|
731 |
|
$this->response->setTime(); |
189
|
731 |
|
$this->response->setMsg($this->{$LEVEL}[$code] . (! empty($append_msg) ? ' - ' . $append_msg : '')); |
190
|
731 |
|
$this->response->setUID($this->getUID()); |
191
|
731 |
|
$this->response->setUsername($this->getUsername()); |
192
|
731 |
|
$this->response->setContext($context); |
193
|
731 |
|
$this->response->setLevel($LEVEL); |
194
|
|
|
|
195
|
731 |
|
$logging = $this->getLogFile(); |
196
|
731 |
|
if (! empty($logging) && ! empty($to_file)) |
197
|
510 |
|
$this->logToFile($LEVEL); |
198
|
731 |
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Write to the file |
202
|
|
|
*/ |
203
|
60 |
|
protected function logToFile($LEVEL) |
204
|
|
|
{ |
205
|
60 |
|
$message = date("d-m-Y H:i:s"); |
206
|
60 |
|
$message .= ' [lev] ' . str_pad($LEVEL, 9, " ", STR_PAD_RIGHT) . ' [/lev]'; |
207
|
60 |
|
$message .= ' [UID] ' . str_pad($this->getUID(), 11, " ", STR_PAD_RIGHT) . ' [/UID]'; |
208
|
60 |
|
$message .= ' [Uname] ' . str_pad($this->getUsername(), 15, " ", STR_PAD_RIGHT) . ' [/Uname]'; |
209
|
60 |
|
$message .= '[mt] ' . str_pad($this->getTime(), 18, " ", STR_PAD_RIGHT) . ' [/mt]'; |
210
|
60 |
|
$message .= '[code] ' . str_pad($this->getCode(), 5, " ", STR_PAD_RIGHT) . ' [/code]'; |
211
|
60 |
|
$message .= '[msg] ' . $this->getMsg() . ' [/msg]' . "\n"; |
212
|
|
|
|
213
|
60 |
|
$logging = $this->getLogFile(); |
214
|
60 |
|
if (empty($logging) || ! is_dir(dirname($logging))) |
215
|
40 |
|
return false; |
216
|
60 |
|
return file_put_contents($logging, $message, FILE_APPEND); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Set loglevel |
221
|
|
|
* |
222
|
|
|
* @param number $level |
|
|
|
|
223
|
|
|
*/ |
224
|
731 |
|
public function setLogLevel($level = 0) |
225
|
|
|
{ |
226
|
731 |
|
$this->loglevel = (in_array($level, range(0, 8))) ? $level : $this->loglevel; |
227
|
731 |
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Set log file |
231
|
|
|
* |
232
|
|
|
* @param string $filepath |
|
|
|
|
233
|
|
|
* @return boolean |
234
|
|
|
*/ |
235
|
731 |
|
public function setLogFile($filepath = false) |
236
|
|
|
{ |
237
|
731 |
|
$logfilepath = $this->makeAbsolute($filepath); |
|
|
|
|
238
|
731 |
|
if (empty($filepath) || ! $this->isWritable(dirname($logfilepath))) { |
|
|
|
|
239
|
12 |
|
$this->error(400, 'setLogFile'); |
240
|
12 |
|
return false; |
241
|
|
|
} |
242
|
719 |
|
$this->log_to_file = $logfilepath; |
243
|
719 |
|
return true; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Get logfile |
248
|
|
|
* |
249
|
|
|
* @return string |
250
|
|
|
*/ |
251
|
731 |
|
public function getLogFile() |
252
|
|
|
{ |
253
|
731 |
|
return $this->log_to_file; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Get log level |
258
|
|
|
* |
259
|
|
|
* @return int |
260
|
|
|
*/ |
261
|
731 |
|
public function getLogLevel() |
262
|
|
|
{ |
263
|
731 |
|
return $this->loglevel; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* |
268
|
|
|
* @return int userid |
269
|
|
|
*/ |
270
|
731 |
|
public function getUID() |
271
|
|
|
{ |
272
|
731 |
|
return $this->UID; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Return event microtime |
277
|
|
|
*/ |
278
|
63 |
|
public function getTime() |
|
|
|
|
279
|
|
|
{ |
280
|
63 |
|
return is_object($this->response) ? $this->response->getTime() : microtime(true); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Return username |
285
|
|
|
*/ |
286
|
731 |
|
public function getUsername() |
|
|
|
|
287
|
|
|
{ |
288
|
731 |
|
return $this->username; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Get response with or without log |
293
|
|
|
* |
294
|
|
|
* @param string $key |
|
|
|
|
295
|
|
|
* @return object |
296
|
|
|
*/ |
297
|
731 |
|
public function response() |
298
|
|
|
{ |
299
|
731 |
|
return $this->response; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Get last status |
304
|
|
|
* |
305
|
|
|
* @return int |
306
|
|
|
*/ |
307
|
33 |
|
public function getStatus() |
308
|
|
|
{ |
309
|
33 |
|
return $this->response->getStatus(); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Get last logged message |
314
|
|
|
* |
315
|
|
|
* @return string |
316
|
|
|
*/ |
317
|
66 |
|
public function getMsg() |
318
|
|
|
{ |
319
|
66 |
|
return $this->response->getMsg(); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Get last logged message code |
324
|
|
|
* |
325
|
|
|
* @return int |
326
|
|
|
*/ |
327
|
84 |
|
public function getCode() |
328
|
|
|
{ |
329
|
84 |
|
return ! is_object($this->response) ? 0 : $this->response->getCode(); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Set last status |
334
|
|
|
* |
335
|
|
|
* @return int |
336
|
|
|
*/ |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Set user id |
340
|
|
|
* |
341
|
|
|
* @param number $ID |
|
|
|
|
342
|
|
|
* @return void |
343
|
|
|
*/ |
344
|
731 |
|
public function setUID($UID) |
345
|
|
|
{ |
346
|
731 |
|
$this->UID = empty($UID) ? $this->UID : $UID; |
347
|
731 |
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Set username |
351
|
|
|
* |
352
|
|
|
* @param string $username |
353
|
|
|
* @return void |
354
|
|
|
*/ |
355
|
731 |
|
public function setUsername($username) |
356
|
|
|
{ |
357
|
731 |
|
$this->username = empty($username) ? $this->username : $username; |
358
|
731 |
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* response setStatus |
362
|
|
|
* |
363
|
|
|
* @param bool $status |
364
|
|
|
*/ |
365
|
731 |
|
public function setStatus($status) |
366
|
|
|
{ |
367
|
731 |
|
if (is_object($this->response)) |
368
|
731 |
|
$this->response->setStatus($status); |
369
|
731 |
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* response setCode |
373
|
|
|
* |
374
|
|
|
* @param bool $code |
375
|
|
|
*/ |
376
|
21 |
|
public function setCode($code) |
377
|
|
|
{ |
378
|
21 |
|
if (is_object($this->response)) |
379
|
21 |
|
$this->response->setCode($code); |
380
|
21 |
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* System is unusable, something happend which should not happen |
384
|
|
|
* |
385
|
|
|
* @param int $code |
386
|
|
|
* @param resource $context |
|
|
|
|
387
|
|
|
* @param string $append_msg |
|
|
|
|
388
|
|
|
*/ |
389
|
6 |
View Code Duplication |
public function emergency($code = 1, $append_msg = false, $context = false) |
|
|
|
|
390
|
|
|
{ |
391
|
6 |
|
$to_file = true; |
392
|
6 |
|
if ($this->getLogLevel() < 1) |
393
|
4 |
|
$to_file = false; |
394
|
|
|
|
395
|
6 |
|
$this->addTrace('EMERGENCY', $code, $context, $append_msg, $to_file); |
|
|
|
|
396
|
6 |
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Action must be taken immediately. |
400
|
|
|
* |
401
|
|
|
* Example: Entire website down, database unavailable, etc. This should |
402
|
|
|
* trigger the SMS alerts and wake you up. |
403
|
|
|
* |
404
|
|
|
* @param int $code |
405
|
|
|
* @param resource $context |
|
|
|
|
406
|
|
|
* @param string $append_msg |
|
|
|
|
407
|
|
|
* @return null |
408
|
|
|
*/ |
409
|
15 |
View Code Duplication |
public function alert($code = 2, $append_msg = false, $context = false) |
|
|
|
|
410
|
|
|
{ |
411
|
15 |
|
$to_file = true; |
412
|
15 |
|
if ($this->getLogLevel() < 2) |
413
|
10 |
|
$to_file = false; |
414
|
15 |
|
$this->addTrace('ALERT', $code, $context, $append_msg, $to_file); |
|
|
|
|
415
|
15 |
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Critical conditions. |
419
|
|
|
* |
420
|
|
|
* Example: Application component unavailable, unexpected exception. |
421
|
|
|
* |
422
|
|
|
* @param int $code |
423
|
|
|
* @param resource $context |
|
|
|
|
424
|
|
|
* @param string $append_msg |
|
|
|
|
425
|
|
|
* @return null |
426
|
|
|
*/ |
427
|
6 |
View Code Duplication |
public function critical($code = 3, $append_msg = false, $context = false) |
|
|
|
|
428
|
|
|
{ |
429
|
6 |
|
$to_file = true; |
430
|
6 |
|
if ($this->getLogLevel() < 3) |
431
|
4 |
|
$to_file = false; |
432
|
6 |
|
$this->addTrace('CRITICAL', $code, $context, $append_msg, $to_file); |
|
|
|
|
433
|
6 |
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Runtime errors that do not require immediate action but should typically |
437
|
|
|
* be logged and monitored. |
438
|
|
|
* |
439
|
|
|
* @param int $code |
440
|
|
|
* @param resource $context |
|
|
|
|
441
|
|
|
* @param string $append_msg |
|
|
|
|
442
|
|
|
* |
443
|
|
|
* @return null |
444
|
|
|
*/ |
445
|
15 |
View Code Duplication |
public function error($code = 4, $append_msg = false, $context = false) |
|
|
|
|
446
|
|
|
{ |
447
|
15 |
|
$to_file = true; |
448
|
15 |
|
if ($this->getLogLevel() < 4) |
449
|
10 |
|
$to_file = false; |
450
|
15 |
|
$this->addTrace('ERROR', $code, $context, $append_msg, $to_file); |
|
|
|
|
451
|
15 |
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Exceptional occurrences that are not errors. |
455
|
|
|
* |
456
|
|
|
* Example: Use of deprecated APIs, poor use of an API, undesirable things |
457
|
|
|
* that are not necessarily wrong. |
458
|
|
|
* |
459
|
|
|
* @param int $code |
460
|
|
|
* @param resource $context |
|
|
|
|
461
|
|
|
* @param string $append_msg |
|
|
|
|
462
|
|
|
* @return null |
463
|
|
|
*/ |
464
|
24 |
View Code Duplication |
public function warning($code = 5, $append_msg = false, $context = false) |
|
|
|
|
465
|
|
|
{ |
466
|
24 |
|
$to_file = true; |
467
|
24 |
|
if ($this->getLogLevel() < 5) |
468
|
17 |
|
$to_file = false; |
469
|
24 |
|
$this->addTrace('WARNING', $code, $context, $append_msg, $to_file); |
|
|
|
|
470
|
24 |
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* Normal but significant events. |
474
|
|
|
* |
475
|
|
|
* @param int $code |
476
|
|
|
* @param resource $context |
|
|
|
|
477
|
|
|
* @param string $append_msg |
|
|
|
|
478
|
|
|
* |
479
|
|
|
* @return null |
480
|
|
|
*/ |
481
|
9 |
View Code Duplication |
public function notice($code = 6, $append_msg = false, $context = false) |
|
|
|
|
482
|
|
|
{ |
483
|
9 |
|
$to_file = true; |
484
|
9 |
|
if ($this->getLogLevel() < 6) |
485
|
6 |
|
$to_file = false; |
486
|
9 |
|
$this->addTrace('NOTICE', $code, $context, $append_msg, $to_file); |
|
|
|
|
487
|
9 |
|
} |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* Interesting events. |
491
|
|
|
* |
492
|
|
|
* Example: User logs in, SQL logs. |
493
|
|
|
* |
494
|
|
|
* @param int $code |
495
|
|
|
* @param resource $context |
|
|
|
|
496
|
|
|
* @param string $append_msg |
|
|
|
|
497
|
|
|
* |
498
|
|
|
* @return null |
499
|
|
|
*/ |
500
|
36 |
View Code Duplication |
public function info($code = 7, $append_msg = false, $context = false) |
|
|
|
|
501
|
|
|
{ |
502
|
36 |
|
$to_file = true; |
503
|
36 |
|
if ($this->getLogLevel() < 7) |
504
|
25 |
|
$to_file = false; |
505
|
36 |
|
$this->addTrace('INFO', $code, $context, $append_msg, $to_file); |
|
|
|
|
506
|
36 |
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* Detailed debug information. |
510
|
|
|
* |
511
|
|
|
* @param int $code |
512
|
|
|
* @param resource $context |
|
|
|
|
513
|
|
|
* @param string $append_msg |
|
|
|
|
514
|
|
|
* |
515
|
|
|
* @return null |
516
|
|
|
*/ |
517
|
731 |
View Code Duplication |
public function debug($code = 8, $append_msg = false, $context = false) |
|
|
|
|
518
|
|
|
{ |
519
|
731 |
|
$to_file = true; |
520
|
731 |
|
if ($this->getLogLevel() < 8) |
521
|
731 |
|
$to_file = false; |
522
|
731 |
|
$this->addTrace('DEBUG', $code, $context, $append_msg, $to_file); |
|
|
|
|
523
|
731 |
|
} |
524
|
|
|
} |
525
|
|
|
|