path = $path; } public function path(): string { return $this->path; } public function line(string $message): void { $stamp = gmdate('Y-m-d H:i:s'); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- streaming log file inside the protected backup folder. @file_put_contents($this->path, '[' . $stamp . ' UTC] ' . $message . "\n", FILE_APPEND | LOCK_EX); } public function tail(int $lines = 200): string { if (!is_readable($this->path)) { return ''; } $size = (int)filesize($this->path); $handle = fopen($this->path, 'rb'); if (!$handle) { return ''; } $read = min($size, 64 * 1024); fseek($handle, -$read, SEEK_END); $data = (string)fread($handle, $read); fclose($handle); $rows = explode("\n", rtrim($data, "\n")); return implode("\n", array_slice($rows, -$lines)); } }