1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Třída pro logování. |
4
|
|
|
* |
5
|
|
|
* @author Vitex <[email protected]> |
6
|
|
|
* @copyright 2009-2016 [email protected] (G) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Ease\Logger; |
10
|
|
|
|
11
|
|
|
class ToFile extends ToMemory |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Předvolená metoda logování. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
public $logType = 'file'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Adresář do kterého se zapisují logy. |
22
|
|
|
* |
23
|
|
|
* @var string dirpath |
24
|
|
|
*/ |
25
|
|
|
public $logPrefix = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Soubor s do kterého se zapisuje log. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
public $logFileName = 'Ease.log'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* úroveň logování. |
36
|
|
|
* |
37
|
|
|
* @var string - silent,debug |
38
|
|
|
*/ |
39
|
|
|
public $logLevel = 'debug'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Soubor do kterého se zapisuje report. |
43
|
|
|
* |
44
|
|
|
* @var string filepath |
45
|
|
|
*/ |
46
|
|
|
public $reportFile = 'EaseReport.log'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Soubor do kterého se lougují pouze zprávy typu Error. |
50
|
|
|
* |
51
|
|
|
* @var string filepath |
52
|
|
|
*/ |
53
|
|
|
public $errorLogFile = 'EaseErrors.log'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Odkaz na vlastnící objekt. |
57
|
|
|
* |
58
|
|
|
* @var \Ease\Sand |
59
|
|
|
*/ |
60
|
|
|
public $parentObject = null; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Filedescriptor Logu. |
64
|
|
|
* |
65
|
|
|
* @var resource |
66
|
|
|
*/ |
67
|
|
|
private $_logFileHandle = null; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Filedescriptor chybového Logu. |
71
|
|
|
* |
72
|
|
|
* @var resource |
73
|
|
|
*/ |
74
|
|
|
private $_errorLogFileHandle = null; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* ID naposledy ulozene zpravy. |
78
|
|
|
* |
79
|
|
|
* @var int unsigned |
80
|
|
|
*/ |
81
|
|
|
private $messageID = 0; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Obecné konfigurace frameworku. |
85
|
|
|
* |
86
|
|
|
* @var \Ease\Shared |
87
|
|
|
*/ |
88
|
|
|
public $easeShared = null; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Saves obejct instace (singleton...). |
92
|
|
|
*/ |
93
|
|
|
private static $_instance = null; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Logovací třída. |
97
|
|
|
* |
98
|
|
|
* @param string $baseLogDir |
99
|
|
|
*/ |
100
|
|
|
public function __construct($baseLogDir = null) |
101
|
|
|
{ |
102
|
|
|
$this->easeShared = \Ease\Shared::singleton(); |
103
|
|
|
$this->setupLogFiles($baseLogDir); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Pri vytvareni objektu pomoci funkce singleton (ma stejne parametry, jako |
108
|
|
|
* konstruktor) se bude v ramci behu programu pouzivat pouze jedna jeho |
109
|
|
|
* instance (ta prvni). |
110
|
|
|
* |
111
|
|
|
* @link http://docs.php.net/en/language.oop5.patterns.html Dokumentace a |
112
|
|
|
* priklad |
113
|
|
|
*/ |
114
|
|
|
public static function singleton() |
115
|
|
|
{ |
116
|
|
|
if (!isset(self::$_instance)) { |
117
|
|
|
$Class = __CLASS__; |
118
|
|
|
self::$_instance = new $Class(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return self::$_instance; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Nastaví cesty logovacích souborů. |
126
|
|
|
* @param string $baseLogDir |
127
|
|
|
*/ |
128
|
|
|
public function setupLogFiles($baseLogDir = null) |
129
|
|
|
{ |
130
|
|
|
if (is_null($baseLogDir)) { |
131
|
|
|
$baseLogDir = $this->logPrefix; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (is_null($baseLogDir) && defined('LOG_DIRECTORY')) { |
|
|
|
|
135
|
|
|
$baseLogDir = constant('LOG_DIRECTORY'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (!empty($baseLogDir)) { |
139
|
|
|
$this->logPrefix = \Ease\Brick::sysFilename($baseLogDir); |
140
|
|
|
if ($this->TestDirectory($this->logPrefix)) { |
141
|
|
|
$this->logFileName = $this->logPrefix.$this->logFileName; |
142
|
|
|
$this->reportFile = $this->logPrefix.$this->reportFile; |
143
|
|
|
$this->errorLogFile = $this->logPrefix.$this->errorLogFile; |
144
|
|
|
} else { |
145
|
|
|
$this->logPrefix = null; |
146
|
|
|
$this->logFileName = null; |
147
|
|
|
$this->reportFile = null; |
148
|
|
|
$this->errorLogFile = null; |
149
|
|
|
} |
150
|
|
|
} else { |
151
|
|
|
$this->logType = 'none'; |
152
|
|
|
$this->logPrefix = null; |
153
|
|
|
$this->logFileName = null; |
154
|
|
|
$this->reportFile = null; |
155
|
|
|
$this->errorLogFile = null; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Zapise zapravu do logu. |
161
|
|
|
* |
162
|
|
|
* @param string $caller název volajícího objektu |
163
|
|
|
* @param string $message zpráva |
164
|
|
|
* @param string $type typ zprávy (success|info|error|warning|*) |
165
|
|
|
* |
166
|
|
|
* @return null|boolean byl report zapsán ? |
167
|
|
|
*/ |
168
|
|
|
public function addToLog($caller, $message, $type = 'message') |
169
|
|
|
{ |
170
|
|
|
++$this->messageID; |
171
|
|
|
if (($this->logLevel == 'silent') && ($type != 'error')) { |
172
|
|
|
return; |
173
|
|
|
} |
174
|
|
|
if (($this->logLevel != 'debug') && ($type == 'debug')) { |
175
|
|
|
return; |
176
|
|
|
} |
177
|
|
|
$this->statusMessages[$type][$this->messageID] = $message; |
178
|
|
|
|
179
|
|
|
$message = htmlspecialchars_decode(strip_tags(stripslashes($message))); |
180
|
|
|
|
181
|
|
|
$logLine = date(DATE_ATOM).' ('.$caller.') '.str_replace(['notice', 'message', |
182
|
|
|
'debug', 'report', 'error', 'warning', 'success', 'info', 'mail',], |
183
|
|
|
['**', '##', '@@', '::'], $type).' '.$message."\n"; |
184
|
|
|
if (!isset($this->logStyles[$type])) { |
185
|
|
|
$type = 'notice'; |
186
|
|
|
} |
187
|
|
|
if ($this->logType == 'console' || $this->logType == 'both') { |
188
|
|
|
if ($this->runType == 'cgi') { |
|
|
|
|
189
|
|
|
echo $logLine; |
190
|
|
|
} else { |
191
|
|
|
echo '<div style="'.$this->logStyles[$type].'">'.$logLine."</div>\n"; |
192
|
|
|
flush(); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
if (!empty($this->logPrefix)) { |
196
|
|
|
if ($this->logType == 'file' || $this->logType == 'both') { |
197
|
|
|
if (!empty($this->logFileName)) { |
198
|
|
|
if (!$this->_logFileHandle) { |
199
|
|
|
$this->_logFileHandle = fopen($this->logFileName, 'a+'); |
|
|
|
|
200
|
|
|
} |
201
|
|
|
if ($this->_logFileHandle !== null) { |
202
|
|
|
fwrite($this->_logFileHandle, $logLine); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
if ($type == 'error') { |
207
|
|
|
if (!empty($this->errorLogFile)) { |
208
|
|
|
if (!$this->_errorLogFileHandle) { |
209
|
|
|
$this->_errorLogFileHandle = fopen($this->errorLogFile, |
|
|
|
|
210
|
|
|
'a+'); |
211
|
|
|
} |
212
|
|
|
if ($this->_errorLogFileHandle) { |
213
|
|
|
fputs($this->_errorLogFileHandle, $logLine); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return true; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Přejmenuje soubor s logem. |
224
|
|
|
* |
225
|
|
|
* @param string $newLogFileName new log filename |
226
|
|
|
* |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
|
|
public function renameLogFile($newLogFileName) |
230
|
|
|
{ |
231
|
|
|
$newLogFileName = dirname($this->logFileName).'/'.basename($newLogFileName); |
232
|
|
|
if (rename($this->logFileName, $newLogFileName)) { |
233
|
|
|
return realpath($newLogFileName); |
234
|
|
|
} else { |
235
|
|
|
return realpath($this->logFileName); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Zkontroluje stav adresáře a upozorní na případné nesnáze. |
241
|
|
|
* |
242
|
|
|
* @param string $directoryPath cesta k adresáři |
243
|
|
|
* @param bool $isDir detekovat existenci adresáře |
244
|
|
|
* @param bool $isReadable testovat čitelnost |
245
|
|
|
* @param bool $isWritable testovat zapisovatelnost |
246
|
|
|
* @param bool $logToFile povolí logování do souboru |
247
|
|
|
* |
248
|
|
|
* @return bool konečný výsledek testu |
249
|
|
|
*/ |
250
|
|
|
public static function testDirectory($directoryPath, $isDir = true, |
251
|
|
|
$isReadable = true, $isWritable = true, |
252
|
|
|
$logToFile = false) |
253
|
|
|
{ |
254
|
|
|
$sanity = true; |
255
|
|
|
if ($isDir) { |
256
|
|
|
if (!is_dir($directoryPath)) { |
257
|
|
|
echo $directoryPath._(' not an folder. Current directory:').' '.getcwd(); |
258
|
|
|
if ($logToFile) { |
259
|
|
|
\Ease\Shared::logger()->addToLog('TestDirectory', |
260
|
|
|
$directoryPath._(' not an folder. Current directory:').' '.getcwd()); |
261
|
|
|
} |
262
|
|
|
$sanity = false; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
if ($isReadable) { |
266
|
|
|
if (!is_readable($directoryPath)) { |
267
|
|
|
echo $directoryPath._(' not an readable folder. Current directory:').' '.getcwd(); |
268
|
|
|
if ($logToFile) { |
269
|
|
|
\Ease\Shared::logger()->addToLog('TestDirectory', |
270
|
|
|
$directoryPath._(' not an readable folder. Current directory:').' '.getcwd()); |
271
|
|
|
} |
272
|
|
|
$sanity = false; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
if ($isWritable) { |
276
|
|
|
if (!is_writable($directoryPath)) { |
277
|
|
|
echo $directoryPath._(' not an writeable folder. Current directory:').' '.getcwd(); |
278
|
|
|
if ($logToFile) { |
279
|
|
|
\Ease\Shared::logger()->addToLog('TestDirectory', |
280
|
|
|
$directoryPath._(' not an writeable folder. Current directory:').' '.getcwd()); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
$sanity = false; |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return $sanity; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Oznamuje chybovou událost. |
292
|
|
|
* |
293
|
|
|
* @param string $caller název volající funkce, nebo objektu |
294
|
|
|
* @param string $message zpráva |
295
|
|
|
* @param mixed $objectData data k zaznamenání |
296
|
|
|
*/ |
297
|
|
|
public function error($caller, $message, $objectData = null) |
298
|
|
|
{ |
299
|
|
|
if ($this->errorLogFile) { |
300
|
|
|
$logFileHandle = @fopen($this->errorLogFile, 'a+'); |
301
|
|
|
if ($logFileHandle) { |
|
|
|
|
302
|
|
|
if ($this->easeShared->runType == 'web') { |
303
|
|
|
fputs($logFileHandle, |
304
|
|
|
\Ease\Brick::printPreBasic($_SERVER)."\n #End of Server enviroment <<<<<<<<<<<<<<<<<<<<<<<<<<< # \n\n"); |
305
|
|
|
} else { |
306
|
|
|
fputs($logFileHandle, |
307
|
|
|
\Ease\Brick::printPreBasic($_ENV)."\n #End of CLI enviroment <<<<<<<<<<<<<<<<<<<<<<<<<<< # \n\n"); |
308
|
|
|
} |
309
|
|
|
if (isset($_POST) && count($_POST)) { |
310
|
|
|
fputs($logFileHandle, |
311
|
|
|
\Ease\Brick::printPreBasic($_POST)."\n #End of _POST <<<<<<<<<<<<<<<<<<<<<<<<<<< # \n\n"); |
312
|
|
|
} |
313
|
|
|
if (isset($_GET) && count($_GET)) { |
314
|
|
|
fputs($logFileHandle, |
315
|
|
|
\Ease\Brick::printPreBasic($_GET)."\n #End of _GET enviroment <<<<<<<<<<<<<<<<<<<<<<<<<<< # \n\n"); |
316
|
|
|
} |
317
|
|
|
if ($objectData) { |
318
|
|
|
fputs($logFileHandle, |
319
|
|
|
\Ease\Brick::printPreBasic($objectData)."\n #End of ObjectData >>>>>>>>>>>>>>>>>>>>>>>>>>>># \n\n"); |
320
|
|
|
} |
321
|
|
|
fclose($logFileHandle); |
322
|
|
|
} else { |
323
|
|
|
$this->addToLog('Error: Couldn\'t open the '.realpath($this->errorLogFile).' error log file', |
324
|
|
|
'error'); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
$this->addToLog($caller, $message, 'error'); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Uzavře chybové soubory. |
332
|
|
|
*/ |
333
|
|
|
public function __destruct() |
334
|
|
|
{ |
335
|
|
|
if ($this->_logFileHandle) { |
336
|
|
|
fclose($this->_logFileHandle); |
337
|
|
|
} |
338
|
|
|
if ($this->_errorLogFileHandle) { |
339
|
|
|
fclose($this->_errorLogFileHandle); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Vrací styl logování. |
345
|
|
|
* |
346
|
|
|
* @param string $logType typ logu warning|info|success|error|notice|* |
347
|
|
|
* |
348
|
|
|
* @return string |
349
|
|
|
*/ |
350
|
|
|
public function getLogStyle($logType = 'notice') |
351
|
|
|
{ |
352
|
|
|
if (key_exists($logType, $this->logStyles)) { |
353
|
|
|
return $this->logStyles[$logType]; |
354
|
|
|
} else { |
355
|
|
|
return ''; |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|