|
1
|
|
|
<?php |
|
2
|
|
|
namespace krysvac\Logger; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* CLogger |
|
6
|
|
|
* |
|
7
|
|
|
* @package krysvac\CLogger |
|
8
|
|
|
* @author Jonathan Sundqvist |
|
9
|
|
|
* @version 1.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
class CLogger |
|
12
|
|
|
{ |
|
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* output directory |
|
15
|
|
|
*/ |
|
16
|
|
|
private static $outputDir = __DIR__ . DIRECTORY_SEPARATOR; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Initializes class |
|
20
|
|
|
*/ |
|
21
|
1 |
|
public static function init() |
|
22
|
|
|
{ |
|
23
|
1 |
|
if (!file_exists(self::getOutput())) |
|
24
|
|
|
{ |
|
25
|
|
|
mkdir(self::getOutput()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
1 |
|
set_error_handler([self::class, "writeErrors"]); |
|
29
|
1 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Sets output directory |
|
33
|
|
|
* |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function setOutputDir($dir) |
|
36
|
|
|
{ |
|
37
|
|
|
self::$outputDir = $dir; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns output directory |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public static function getOutput() |
|
44
|
|
|
{ |
|
45
|
1 |
|
return self::$outputDir . "log" . DIRECTORY_SEPARATOR; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Writes error to file. |
|
50
|
|
|
* |
|
51
|
|
|
* @param $errno int - error level |
|
52
|
|
|
* @param $errstr string - error message |
|
53
|
|
|
* @param $errfile string - filename of error causing file |
|
54
|
|
|
* @param $errline int - line number of error |
|
55
|
|
|
* |
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public static function writeErrors($errno, $errstr, $errfile, $errline) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$filename = date("Y-m-d") . ".log"; |
|
61
|
1 |
|
$content = self::getContents($errno, $errstr, $errfile, $errline); |
|
62
|
1 |
|
file_put_contents(self::getOutput() . $filename, implode("\n", $content), FILE_APPEND); |
|
63
|
|
|
|
|
64
|
1 |
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Converts errordata to array |
|
69
|
|
|
* |
|
70
|
|
|
* @param $errno int - error level |
|
71
|
|
|
* @param $errstr string - error message |
|
72
|
|
|
* @param $errfile string - filename of error causing file |
|
73
|
|
|
* @param $errline int - line number of error |
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
1 |
|
private static function getContents($errno, $errstr, $errfile, $errline) |
|
78
|
|
|
{ |
|
79
|
|
|
$errors = [ |
|
80
|
1 |
|
E_WARNING => "E_WARNING", |
|
81
|
1 |
|
E_NOTICE => "E_NOTICE", |
|
82
|
1 |
|
E_USER_ERROR => "E_USER_ERROR", |
|
83
|
1 |
|
E_USER_WARNING => "E_USER_WARNING", |
|
84
|
1 |
|
E_USER_NOTICE => "E_USER_NOTICE", |
|
85
|
1 |
|
E_STRICT => "E_STRICT", |
|
86
|
1 |
|
E_RECOVERABLE_ERROR => "E_RECOVERABLE_ERROR", |
|
87
|
1 |
|
E_DEPRECATED => "E_DEPRECATED", |
|
88
|
1 |
|
E_USER_DEPRECATED => "E_USER_DEPRECATED" |
|
89
|
|
|
]; |
|
90
|
|
|
|
|
91
|
1 |
|
$errmsg = $errors[$errno]; |
|
92
|
1 |
|
$time = time(); |
|
93
|
1 |
|
$date = date("Y-m-d H:i:s", $time); |
|
94
|
|
|
return [ |
|
95
|
1 |
|
"Error: $errno ($errmsg)", |
|
96
|
1 |
|
"Unix Time: $time ($date)", |
|
97
|
1 |
|
"File: $errfile", |
|
98
|
1 |
|
"Line: $errline", |
|
99
|
1 |
|
"Message: $errstr" |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|