1 <?php
2
3 4 5 6 7 8 9 10 11 12
13
14 namespace Slimore\Log;
15
16 17 18 19 20 21
22
23 class Writer
24 {
25 26 27
28 protected $level;
29
30 31 32
33 protected $message;
34
35 36 37
38 protected $filename;
39
40 41 42
43 protected $resource;
44
45 46 47
48 protected $messageOutput;
49
50 51 52
53 protected $label = 'DEBUG';
54
55 56 57
58 protected $path = '../app/logs';
59
60 61 62
63
64 protected $dateFormat = 'Y-m-d';
65
66 67 68
69 protected $extension = 'log';
70
71 72 73
74 protected $messageFormat = '[%label%][#][%date%] %message%';
75
76 77 78
79 protected $messageFormatSearchs = ['%label%', '[#]', '%date%', '%message%'];
80
81 82 83
84 protected $messageFormatReplaces;
85
86 87 88
89 public $writeBeforeHandle = null;
90
91 public $customMessageFormatParser = false;
92
93 94 95
96 protected $levels = [
97 1 => 'EMERGENCY',
98 2 => 'ALERT',
99 3 => 'CRITICAL',
100 4 => 'ERROR',
101 5 => 'WARNING',
102 6 => 'NOTICE',
103 7 => 'INFO',
104 8 => 'DEBUG'
105 ];
106
107 108 109
110
111 public function __construct()
112 {
113 }
114
115 116 117 118 119 120 121
122
123 public function set()
124 {
125 $args = func_get_args();
126 $count = func_num_args();
127
128 if ( is_array($args[0]) )
129 {
130 foreach ($args[0] as $key => $value)
131 {
132 if ($key === 'path')
133 {
134 $this->setPath($value);
135 continue;
136 }
137
138 if (property_exists($this, $key))
139 {
140 $this->$key = $value;
141 }
142 }
143 }
144 elseif ($count === 2)
145 {
146 if ($args[0] === 'path')
147 {
148 $this->setPath($args[1]);
149 return ;
150 }
151
152 if (property_exists($this, $args[0]))
153 {
154 $this[$args[0]] = $args[1];
155 }
156 }
157 }
158
159 160 161 162 163 164 165
166
167 public function setPath($path)
168 {
169 if (!file_exists($path))
170 {
171 mkdir($path, 0777, true);
172 }
173
174 $this->path = $path;
175 }
176
177 178 179 180 181
182
183 public function getPath()
184 {
185 return $this->path;
186 }
187
188 189 190 191 192
193
194 public function getLevel()
195 {
196 return $this->level;
197 }
198
199 200 201 202 203
204
205 public function getLabel()
206 {
207 return $this->label;
208 }
209
210 211 212 213 214
215
216 public function getMessage()
217 {
218 return $this->message;
219 }
220
221 222 223 224 225
226
227 public function getMessageFormat()
228 {
229 return $this->messageFormat;
230 }
231
232 233 234 235 236 237
238
239 public function setMessageFormat($format)
240 {
241 $this->messageFormat = (string) $format;
242 }
243
244 245 246 247 248 249
250
251 public function setMessageFormatSearchs(array $searchs)
252 {
253 $this->messageFormatSearchs = $searchs;
254 }
255
256 257 258 259 260 261
262
263 public function getMessageFormatSearchs()
264 {
265 return $this->messageFormatSearchs;
266 }
267
268 269 270 271 272
273
274 protected function defaultMessageFormatReplaces()
275 {
276 $this->messageFormatReplaces = [
277 $this->label,
278 str_repeat(' ', 9 - strlen($this->label)),
279 date('Y-m-d H:i:s e O'),
280 $this->message
281 ];
282 }
283
284 285 286 287 288 289
290
291 public function setMessageFormatReplaces(array $replaces)
292 {
293 $this->messageFormatReplaces = $replaces;
294 }
295
296 297 298 299 300
301
302 public function getMessageFormatReplaces()
303 {
304 return $this->messageFormatReplaces;
305 }
306
307 308 309 310 311
312
313 protected function messageFormatParser()
314 {
315 $this->label = $this->levels[$this->level];
316
317 $this->messageOutput = str_replace(
318 $this->getMessageFormatSearchs(),
319 $this->getMessageFormatReplaces(),
320 $this->getMessageFormat()
321 );
322 }
323
324 325 326 327 328 329
330
331 protected function writeBefore(callable $callback)
332 {
333 $callback($this);
334 }
335
336 337 338 339 340 341 342
343
344 public function write($content, $level)
345 {
346 $this->message = (string) $content;
347 $this->level = $level;
348
349 if ( is_callable($this->writeBeforeHandle) )
350 {
351 $this->writeBefore($this->writeBeforeHandle);
352
353 if (!$this->customMessageFormatParser)
354 {
355 $this->defaultMessageFormatReplaces();
356 }
357 }
358 else
359 {
360 $this->defaultMessageFormatReplaces();
361 }
362
363 $this->messageFormatParser();
364
365 if ( !$this->resource )
366 {
367 $this->filename = date($this->dateFormat);
368
369 if ( !empty($this->extension) )
370 {
371 $this->filename .= '.' . $this->extension;
372 }
373
374 $this->resource = fopen($this->path . DIRECTORY_SEPARATOR . $this->filename, 'a');
375 }
376
377 fwrite($this->resource, $this->messageOutput . PHP_EOL);
378 }
379 }