@@ -48,110 +48,110 @@ |
||
48 | 48 | */ |
49 | 49 | |
50 | 50 | class File extends LogDetails implements IWriter, IFileBased { |
51 | - /** @var string */ |
|
52 | - protected $logFile; |
|
53 | - /** @var int */ |
|
54 | - protected $logFileMode; |
|
55 | - /** @var SystemConfig */ |
|
56 | - private $config; |
|
51 | + /** @var string */ |
|
52 | + protected $logFile; |
|
53 | + /** @var int */ |
|
54 | + protected $logFileMode; |
|
55 | + /** @var SystemConfig */ |
|
56 | + private $config; |
|
57 | 57 | |
58 | - public function __construct(string $path, string $fallbackPath = '', SystemConfig $config) { |
|
59 | - parent::__construct($config); |
|
60 | - $this->logFile = $path; |
|
61 | - if (!file_exists($this->logFile)) { |
|
62 | - if ( |
|
63 | - ( |
|
64 | - !is_writable(dirname($this->logFile)) |
|
65 | - || !touch($this->logFile) |
|
66 | - ) |
|
67 | - && $fallbackPath !== '' |
|
68 | - ) { |
|
69 | - $this->logFile = $fallbackPath; |
|
70 | - } |
|
71 | - } |
|
72 | - $this->config = $config; |
|
73 | - $this->logFileMode = $config->getValue('logfilemode', 0640); |
|
74 | - } |
|
58 | + public function __construct(string $path, string $fallbackPath = '', SystemConfig $config) { |
|
59 | + parent::__construct($config); |
|
60 | + $this->logFile = $path; |
|
61 | + if (!file_exists($this->logFile)) { |
|
62 | + if ( |
|
63 | + ( |
|
64 | + !is_writable(dirname($this->logFile)) |
|
65 | + || !touch($this->logFile) |
|
66 | + ) |
|
67 | + && $fallbackPath !== '' |
|
68 | + ) { |
|
69 | + $this->logFile = $fallbackPath; |
|
70 | + } |
|
71 | + } |
|
72 | + $this->config = $config; |
|
73 | + $this->logFileMode = $config->getValue('logfilemode', 0640); |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * write a message in the log |
|
78 | - * @param string $app |
|
79 | - * @param string|array $message |
|
80 | - * @param int $level |
|
81 | - */ |
|
82 | - public function write(string $app, $message, int $level) { |
|
83 | - $entry = $this->logDetailsAsJSON($app, $message, $level); |
|
84 | - $handle = @fopen($this->logFile, 'a'); |
|
85 | - if ($this->logFileMode > 0 && is_file($this->logFile) && (fileperms($this->logFile) & 0777) != $this->logFileMode) { |
|
86 | - @chmod($this->logFile, $this->logFileMode); |
|
87 | - } |
|
88 | - if ($handle) { |
|
89 | - fwrite($handle, $entry."\n"); |
|
90 | - fclose($handle); |
|
91 | - } else { |
|
92 | - // Fall back to error_log |
|
93 | - error_log($entry); |
|
94 | - } |
|
95 | - if (php_sapi_name() === 'cli-server') { |
|
96 | - if (!\is_string($message)) { |
|
97 | - $message = json_encode($message); |
|
98 | - } |
|
99 | - error_log($message, 4); |
|
100 | - } |
|
101 | - } |
|
76 | + /** |
|
77 | + * write a message in the log |
|
78 | + * @param string $app |
|
79 | + * @param string|array $message |
|
80 | + * @param int $level |
|
81 | + */ |
|
82 | + public function write(string $app, $message, int $level) { |
|
83 | + $entry = $this->logDetailsAsJSON($app, $message, $level); |
|
84 | + $handle = @fopen($this->logFile, 'a'); |
|
85 | + if ($this->logFileMode > 0 && is_file($this->logFile) && (fileperms($this->logFile) & 0777) != $this->logFileMode) { |
|
86 | + @chmod($this->logFile, $this->logFileMode); |
|
87 | + } |
|
88 | + if ($handle) { |
|
89 | + fwrite($handle, $entry."\n"); |
|
90 | + fclose($handle); |
|
91 | + } else { |
|
92 | + // Fall back to error_log |
|
93 | + error_log($entry); |
|
94 | + } |
|
95 | + if (php_sapi_name() === 'cli-server') { |
|
96 | + if (!\is_string($message)) { |
|
97 | + $message = json_encode($message); |
|
98 | + } |
|
99 | + error_log($message, 4); |
|
100 | + } |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * get entries from the log in reverse chronological order |
|
105 | - * @param int $limit |
|
106 | - * @param int $offset |
|
107 | - * @return array |
|
108 | - */ |
|
109 | - public function getEntries(int $limit=50, int $offset=0):array { |
|
110 | - $minLevel = $this->config->getValue("loglevel", ILogger::WARN); |
|
111 | - $entries = []; |
|
112 | - $handle = @fopen($this->logFile, 'rb'); |
|
113 | - if ($handle) { |
|
114 | - fseek($handle, 0, SEEK_END); |
|
115 | - $pos = ftell($handle); |
|
116 | - $line = ''; |
|
117 | - $entriesCount = 0; |
|
118 | - $lines = 0; |
|
119 | - // Loop through each character of the file looking for new lines |
|
120 | - while ($pos >= 0 && ($limit === null ||$entriesCount < $limit)) { |
|
121 | - fseek($handle, $pos); |
|
122 | - $ch = fgetc($handle); |
|
123 | - if ($ch == "\n" || $pos == 0) { |
|
124 | - if ($line != '') { |
|
125 | - // Add the first character if at the start of the file, |
|
126 | - // because it doesn't hit the else in the loop |
|
127 | - if ($pos == 0) { |
|
128 | - $line = $ch.$line; |
|
129 | - } |
|
130 | - $entry = json_decode($line); |
|
131 | - // Add the line as an entry if it is passed the offset and is equal or above the log level |
|
132 | - if ($entry->level >= $minLevel) { |
|
133 | - $lines++; |
|
134 | - if ($lines > $offset) { |
|
135 | - $entries[] = $entry; |
|
136 | - $entriesCount++; |
|
137 | - } |
|
138 | - } |
|
139 | - $line = ''; |
|
140 | - } |
|
141 | - } else { |
|
142 | - $line = $ch.$line; |
|
143 | - } |
|
144 | - $pos--; |
|
145 | - } |
|
146 | - fclose($handle); |
|
147 | - } |
|
148 | - return $entries; |
|
149 | - } |
|
103 | + /** |
|
104 | + * get entries from the log in reverse chronological order |
|
105 | + * @param int $limit |
|
106 | + * @param int $offset |
|
107 | + * @return array |
|
108 | + */ |
|
109 | + public function getEntries(int $limit=50, int $offset=0):array { |
|
110 | + $minLevel = $this->config->getValue("loglevel", ILogger::WARN); |
|
111 | + $entries = []; |
|
112 | + $handle = @fopen($this->logFile, 'rb'); |
|
113 | + if ($handle) { |
|
114 | + fseek($handle, 0, SEEK_END); |
|
115 | + $pos = ftell($handle); |
|
116 | + $line = ''; |
|
117 | + $entriesCount = 0; |
|
118 | + $lines = 0; |
|
119 | + // Loop through each character of the file looking for new lines |
|
120 | + while ($pos >= 0 && ($limit === null ||$entriesCount < $limit)) { |
|
121 | + fseek($handle, $pos); |
|
122 | + $ch = fgetc($handle); |
|
123 | + if ($ch == "\n" || $pos == 0) { |
|
124 | + if ($line != '') { |
|
125 | + // Add the first character if at the start of the file, |
|
126 | + // because it doesn't hit the else in the loop |
|
127 | + if ($pos == 0) { |
|
128 | + $line = $ch.$line; |
|
129 | + } |
|
130 | + $entry = json_decode($line); |
|
131 | + // Add the line as an entry if it is passed the offset and is equal or above the log level |
|
132 | + if ($entry->level >= $minLevel) { |
|
133 | + $lines++; |
|
134 | + if ($lines > $offset) { |
|
135 | + $entries[] = $entry; |
|
136 | + $entriesCount++; |
|
137 | + } |
|
138 | + } |
|
139 | + $line = ''; |
|
140 | + } |
|
141 | + } else { |
|
142 | + $line = $ch.$line; |
|
143 | + } |
|
144 | + $pos--; |
|
145 | + } |
|
146 | + fclose($handle); |
|
147 | + } |
|
148 | + return $entries; |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * @return string |
|
153 | - */ |
|
154 | - public function getLogFilePath():string { |
|
155 | - return $this->logFile; |
|
156 | - } |
|
151 | + /** |
|
152 | + * @return string |
|
153 | + */ |
|
154 | + public function getLogFilePath():string { |
|
155 | + return $this->logFile; |
|
156 | + } |
|
157 | 157 | } |