1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pimf |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Pimf; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Logger with common logging options into a file. |
13
|
|
|
* |
14
|
|
|
* @package Pimf |
15
|
|
|
* @author Gjero Krsteski <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Logger |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var resource |
21
|
|
|
*/ |
22
|
|
|
private $infoHandle; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var resource |
26
|
|
|
*/ |
27
|
|
|
private $warnHandle; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var resource |
31
|
|
|
*/ |
32
|
|
|
private $errorHandle; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private static $remoteIp; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private static $script; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $remoteIp |
46
|
|
|
* @param string $script |
47
|
|
|
*/ |
48
|
|
|
public static function setup($remoteIp, $script) |
49
|
|
|
{ |
50
|
|
|
self::$remoteIp = $remoteIp; |
51
|
|
|
self::$script = $script; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Logger constructor. |
56
|
|
|
* @param Contracts\Streamable $infoHandle |
57
|
|
|
* @param Contracts\Streamable $warnHandle |
58
|
|
|
* @param Contracts\Streamable $errorHandle |
59
|
|
|
*/ |
60
|
|
|
public function __construct( |
61
|
|
|
Contracts\Streamable $infoHandle, |
62
|
|
|
Contracts\Streamable $warnHandle, |
63
|
|
|
Contracts\Streamable $errorHandle) |
64
|
|
|
{ |
65
|
|
|
$this->infoHandle = $infoHandle->open(); |
66
|
|
|
$this->warnHandle = $warnHandle->open(); |
67
|
|
|
$this->errorHandle = $errorHandle->open(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @throws \RuntimeException If something went wrong on creating the log dir and file. |
72
|
|
|
*/ |
73
|
|
|
public function init() |
74
|
|
|
{ |
75
|
|
|
if (is_resource($this->errorHandle) |
76
|
|
|
&& is_resource($this->infoHandle) |
77
|
|
|
&& is_resource($this->warnHandle) |
78
|
|
|
) { |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (!$this->errorHandle || !$this->infoHandle || !$this->warnHandle) { |
83
|
|
|
throw new \RuntimeException("failed to obtain a Streamable handle for logging"); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $msg |
89
|
|
|
* |
90
|
|
|
* @return Logger |
91
|
|
|
*/ |
92
|
|
|
public function debug($msg) |
93
|
|
|
{ |
94
|
|
|
if ($this->iniGetBool('display_errors') === true) { |
95
|
|
|
$this->write((string)$msg, 'DEBUG'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $msg |
103
|
|
|
* |
104
|
|
|
* @return Logger |
105
|
|
|
*/ |
106
|
|
|
public function warn($msg) |
107
|
|
|
{ |
108
|
|
|
if ($this->iniGetBool('display_errors') === true) { |
109
|
|
|
$this->write((string)$msg, 'WARNING'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $msg |
117
|
|
|
* |
118
|
|
|
* @return Logger |
119
|
|
|
*/ |
120
|
|
|
public function error($msg) |
121
|
|
|
{ |
122
|
|
|
$this->write((string)$msg, 'ERROR'); |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $msg |
129
|
|
|
* |
130
|
|
|
* @return Logger |
131
|
|
|
*/ |
132
|
|
|
public function info($msg) |
133
|
|
|
{ |
134
|
|
|
if ($this->iniGetBool('display_errors') === true) { |
135
|
|
|
$this->write((string)$msg, 'INFO'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $msg |
143
|
|
|
* @param string $severity |
144
|
|
|
*/ |
145
|
|
|
protected function write($msg, $severity = 'DEBUG') |
146
|
|
|
{ |
147
|
|
|
$msg = $this->format($msg, $severity); |
148
|
|
|
|
149
|
|
|
if ($severity == 'WARNING') { |
150
|
|
|
fwrite($this->warnHandle, $msg); |
151
|
|
|
} elseif ($severity == 'ERROR') { |
152
|
|
|
fwrite($this->errorHandle, $msg); |
153
|
|
|
} else { |
154
|
|
|
fwrite($this->infoHandle, $msg); |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function __destruct() |
160
|
|
|
{ |
161
|
|
|
if (is_resource($this->infoHandle) |
162
|
|
|
&& is_resource($this->warnHandle) |
163
|
|
|
&& is_resource($this->errorHandle) |
164
|
|
|
) { |
165
|
|
|
|
166
|
|
|
if (fclose($this->infoHandle) === false) { |
167
|
|
|
$this->error('Logger failed to close the handle to the log file'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
fclose($this->warnHandle); |
171
|
|
|
fclose($this->errorHandle); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Formats the error message in representable manner. |
177
|
|
|
* |
178
|
|
|
* @param string $message |
179
|
|
|
* @param string $severity |
180
|
|
|
* |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
private function format($message, $severity) |
184
|
|
|
{ |
185
|
|
|
$msg = date("m-d-Y") . " " . date("G:i:s") . " " . self::$remoteIp; |
186
|
|
|
|
187
|
|
|
$IPLength = strlen(self::$remoteIp); |
188
|
|
|
$numWhitespaces = 15 - $IPLength; |
189
|
|
|
|
190
|
|
|
for ($i = 0; $i < $numWhitespaces; $i++) { |
191
|
|
|
$msg .= " "; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$msg .= " " . $severity . ": "; |
195
|
|
|
|
196
|
|
|
$lastSlashIndex = strrpos(self::$script, "/"); |
197
|
|
|
$fileName = self::$script; |
198
|
|
|
|
199
|
|
|
if ($lastSlashIndex !== false) { |
200
|
|
|
$fileName = substr(self::$script, $lastSlashIndex + 1); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$msg .= $fileName . "\t"; |
204
|
|
|
$msg .= ": " . $message . "\r\n"; |
205
|
|
|
|
206
|
|
|
return $msg; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param string $varname |
211
|
|
|
* |
212
|
|
|
* @return bool |
213
|
|
|
*/ |
214
|
|
|
protected function iniGetBool($varname) |
215
|
|
|
{ |
216
|
|
|
$varvalue = ini_get($varname); |
217
|
|
|
|
218
|
|
|
switch (strtolower($varvalue)) { |
219
|
|
|
case 'on': |
220
|
|
|
case 'yes': |
221
|
|
|
case 'true': |
222
|
|
|
return 'assert.active' !== $varname; |
223
|
|
|
case 'stdout': |
224
|
|
|
case 'stderr': |
225
|
|
|
return 'display_errors' === $varname; |
226
|
|
|
default: |
227
|
|
|
return (bool)(int)$varvalue; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|