Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
filelogger.php
1<?php
2
10namespace Bitrix\Main\Diag;
11
12class FileLogger extends Logger
13{
14 protected $fileName;
15 protected $maxSize = 1048576;
16
21 public function __construct(string $fileName, int $maxSize = null)
22 {
23 $this->fileName = $fileName;
24
25 if ($maxSize !== null)
26 {
27 $this->maxSize = $maxSize;
28 }
29 }
30
31 protected function logMessage(string $level, string $message)
32 {
33 $current = ignore_user_abort(true);
34
35 if ($fp = fopen($this->fileName, 'ab'))
36 {
37 if (flock($fp, LOCK_EX))
38 {
39 // need it for filesize()
40 clearstatcache();
41 $logSize = filesize($this->fileName);
42
43 if ($this->maxSize > 0 && $logSize > $this->maxSize)
44 {
45 $this->rotateLog();
46 ftruncate($fp, 0);
47 }
48
49 fwrite($fp, $message);
50 fflush($fp);
51 flock($fp, LOCK_UN);
52 }
53 fclose($fp);
54 }
55
56 ignore_user_abort($current);
57 }
58
59 protected function rotateLog()
60 {
61 $historyName = $this->fileName . '.old';
62
63 copy($this->fileName, $historyName);
64 }
65}
logMessage(string $level, string $message)
__construct(string $fileName, int $maxSize=null)