1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Třída pro logování. |
4
|
|
|
* |
5
|
|
|
* @author Vitex <[email protected]> |
6
|
|
|
* @copyright 2009-2019 [email protected] (G) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Ease\Logger; |
10
|
|
|
|
11
|
|
|
class ToFile extends ToMemory implements Loggingable |
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
|
|
|
* Odkaz na vlastnící objekt. |
36
|
|
|
* |
37
|
|
|
* @var \Ease\Sand |
38
|
|
|
*/ |
39
|
|
|
public $parentObject = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Filedescriptor Logu. |
43
|
|
|
* |
44
|
|
|
* @var resource|boolean |
45
|
|
|
*/ |
46
|
|
|
private $_logFileHandle = null; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* ID naposledy ulozene zpravy. |
50
|
|
|
* |
51
|
|
|
* @var int unsigned |
52
|
|
|
*/ |
53
|
|
|
private $messageID = 0; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Obecné konfigurace frameworku. |
57
|
|
|
* |
58
|
|
|
* @var \Ease\Shared |
59
|
|
|
*/ |
60
|
|
|
public $easeShared = null; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Saves obejct instace (singleton...). |
64
|
|
|
*/ |
65
|
|
|
private static $instance = null; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Logovací třída. |
69
|
|
|
* |
70
|
|
|
* @param string $baseLogDir |
71
|
|
|
*/ |
72
|
|
|
public function __construct($baseLogDir = null) |
73
|
|
|
{ |
74
|
|
|
$this->setupLogFiles($baseLogDir); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get instanece of File Logger |
79
|
|
|
* |
80
|
|
|
* @param string $logdir |
81
|
|
|
* |
82
|
|
|
* @return ToFile |
83
|
|
|
*/ |
84
|
|
|
public static function singleton($logdir = null) |
85
|
|
|
{ |
86
|
|
|
if (!isset(self::$instance)) { |
87
|
|
|
self::$instance = new self($logdir); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return self::$instance; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Nastaví cesty logovacích souborů. |
95
|
|
|
* |
96
|
|
|
* @param string $baseLogDir |
97
|
|
|
*/ |
98
|
|
|
public function setupLogFiles($baseLogDir = null) |
99
|
|
|
{ |
100
|
|
|
|
101
|
|
|
$baseLogDir = is_null($baseLogDir) ? ( is_null($this->logPrefix) && defined('LOG_DIRECTORY') ? constant('LOG_DIRECTORY') : null ) : $baseLogDir; |
|
|
|
|
102
|
|
|
|
103
|
|
|
if (!empty($baseLogDir)) { |
104
|
|
|
$this->logPrefix = \Ease\Functions::sysFilename($baseLogDir); |
105
|
|
|
if ($this->testDirectory($this->logPrefix)) { |
106
|
|
|
$this->logFileName = $this->logPrefix.$this->logFileName; |
107
|
|
|
} else { |
108
|
|
|
$this->logPrefix = null; |
109
|
|
|
$this->logFileName = null; |
110
|
|
|
} |
111
|
|
|
} else { |
112
|
|
|
$this->logType = 'none'; |
113
|
|
|
$this->logPrefix = null; |
114
|
|
|
$this->logFileName = null; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Zapise zapravu do logu. |
120
|
|
|
* |
121
|
|
|
* @param string $caller název volajícího objektu |
122
|
|
|
* @param string $message zpráva |
123
|
|
|
* @param string $type typ zprávy (success|info|error|warning|*) |
124
|
|
|
* |
125
|
|
|
* @return null|boolean byl report zapsán ? |
126
|
|
|
*/ |
127
|
|
|
public function addToLog($caller, $message, $type = 'message') |
128
|
|
|
{ |
129
|
|
|
++$this->messageID; |
130
|
|
|
$this->statusMessages[$type][$this->messageID] = $message; |
131
|
|
|
|
132
|
|
|
$message = htmlspecialchars_decode(strip_tags(stripslashes($message))); |
133
|
|
|
|
134
|
|
|
$logLine = date(DATE_ATOM).' ('.$caller.') '.str_replace( |
135
|
|
|
['notice', 'message', |
136
|
|
|
'debug', 'error', 'warning', 'success', 'info', 'mail',], |
137
|
|
|
['**', '##', '@@', '::'], $type |
138
|
|
|
).' '.$message."\n"; |
139
|
|
|
if (!isset($this->logStyles[$type])) { |
140
|
|
|
$type = 'notice'; |
141
|
|
|
} |
142
|
|
|
if ($this->logType == 'console' || $this->logType == 'both') { |
143
|
|
|
if (php_sapi_name() == 'cli') { |
144
|
|
|
echo $logLine; |
145
|
|
|
} else { |
146
|
|
|
echo '<div style="'.$this->logStyles[$type].'">'.$logLine."</div>\n"; |
147
|
|
|
flush(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
if (!empty($this->logPrefix)) { |
151
|
|
|
if ($this->logType == 'file' || $this->logType == 'both') { |
152
|
|
|
if (!empty($this->logFileName)) { |
153
|
|
|
if (!$this->_logFileHandle) { |
154
|
|
|
$this->_logFileHandle = fopen($this->logFileName, 'a+'); |
155
|
|
|
} |
156
|
|
|
if ($this->_logFileHandle !== null) { |
157
|
|
|
fwrite($this->_logFileHandle, $logLine); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return true; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Zkontroluje stav adresáře a upozorní na případné nesnáze. |
168
|
|
|
* |
169
|
|
|
* @param string $directoryPath cesta k adresáři |
170
|
|
|
* @param bool $isDir detekovat existenci adresáře |
171
|
|
|
* @param bool $isReadable testovat čitelnost |
172
|
|
|
* @param bool $isWritable testovat zapisovatelnost |
173
|
|
|
* |
174
|
|
|
* @return bool konečný výsledek testu |
175
|
|
|
*/ |
176
|
|
|
public static function testDirectory($directoryPath, $isDir = true, |
177
|
|
|
$isReadable = true, $isWritable = true |
178
|
|
|
) { |
179
|
|
|
if ($isDir && !is_dir($directoryPath)) { |
180
|
|
|
throw new \Exception($directoryPath._(' is not an folder. Current directory:').' '.getcwd()); |
181
|
|
|
} |
182
|
|
|
if ($isReadable && !is_readable($directoryPath)) { |
183
|
|
|
throw new \Exception($directoryPath._(' not an readable folder. Current directory:').' '.getcwd()); |
184
|
|
|
} |
185
|
|
|
if ($isWritable && !is_writable($directoryPath)) { |
186
|
|
|
throw new \Exception($directoryPath._(' not an writeable folder. Current directory:').' '.getcwd()); |
187
|
|
|
} |
188
|
|
|
return true; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Uzavře chybové soubory. |
193
|
|
|
*/ |
194
|
|
|
public function __destruct() |
195
|
|
|
{ |
196
|
|
|
if ($this->_logFileHandle) { |
197
|
|
|
fclose($this->_logFileHandle); |
|
|
|
|
198
|
|
|
} |
199
|
|
|
if ($this->_errorLogFileHandle) { |
|
|
|
|
200
|
|
|
fclose($this->_errorLogFileHandle); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Vrací styl logování. |
206
|
|
|
* |
207
|
|
|
* @param string $logType typ logu warning|info|success|error|notice|* |
208
|
|
|
* |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
|
|
public function getLogStyle($logType = 'notice') |
212
|
|
|
{ |
213
|
|
|
if (key_exists($logType, $this->logStyles)) { |
214
|
|
|
return $this->logStyles[$logType]; |
215
|
|
|
} else { |
216
|
|
|
return ''; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|