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