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