File.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Log;
  8. use OC\SystemConfig;
  9. use OCP\ILogger;
  10. use OCP\Log\IFileBased;
  11. use OCP\Log\IWriter;
  12. /**
  13. * logging utilities
  14. *
  15. * Log is saved at data/nextcloud.log (on default)
  16. */
  17. class File extends LogDetails implements IWriter, IFileBased {
  18. protected string $logFile;
  19. protected int $logFileMode;
  20. public function __construct(
  21. string $path,
  22. string $fallbackPath,
  23. private SystemConfig $config,
  24. ) {
  25. parent::__construct($config);
  26. $this->logFile = $path;
  27. if (!file_exists($this->logFile)) {
  28. if (
  29. (
  30. !is_writable(dirname($this->logFile))
  31. || !touch($this->logFile)
  32. )
  33. && $fallbackPath !== ''
  34. ) {
  35. $this->logFile = $fallbackPath;
  36. }
  37. }
  38. $this->logFileMode = $config->getValue('logfilemode', 0640);
  39. }
  40. /**
  41. * write a message in the log
  42. * @param string|array $message
  43. */
  44. public function write(string $app, $message, int $level): void {
  45. $entry = $this->logDetailsAsJSON($app, $message, $level);
  46. $handle = @fopen($this->logFile, 'a');
  47. if ($this->logFileMode > 0 && is_file($this->logFile) && (fileperms($this->logFile) & 0777) != $this->logFileMode) {
  48. @chmod($this->logFile, $this->logFileMode);
  49. }
  50. if ($handle) {
  51. fwrite($handle, $entry."\n");
  52. fclose($handle);
  53. } else {
  54. // Fall back to error_log
  55. error_log($entry);
  56. }
  57. if (php_sapi_name() === 'cli-server') {
  58. if (!\is_string($message)) {
  59. $message = json_encode($message);
  60. }
  61. error_log($message, 4);
  62. }
  63. }
  64. /**
  65. * get entries from the log in reverse chronological order
  66. */
  67. public function getEntries(int $limit = 50, int $offset = 0): array {
  68. $minLevel = $this->config->getValue("loglevel", ILogger::WARN);
  69. $entries = [];
  70. $handle = @fopen($this->logFile, 'rb');
  71. if ($handle) {
  72. fseek($handle, 0, SEEK_END);
  73. $pos = ftell($handle);
  74. $line = '';
  75. $entriesCount = 0;
  76. $lines = 0;
  77. // Loop through each character of the file looking for new lines
  78. while ($pos >= 0 && ($limit === null || $entriesCount < $limit)) {
  79. fseek($handle, $pos);
  80. $ch = fgetc($handle);
  81. if ($ch == "\n" || $pos == 0) {
  82. if ($line != '') {
  83. // Add the first character if at the start of the file,
  84. // because it doesn't hit the else in the loop
  85. if ($pos == 0) {
  86. $line = $ch.$line;
  87. }
  88. $entry = json_decode($line);
  89. // Add the line as an entry if it is passed the offset and is equal or above the log level
  90. if ($entry->level >= $minLevel) {
  91. $lines++;
  92. if ($lines > $offset) {
  93. $entries[] = $entry;
  94. $entriesCount++;
  95. }
  96. }
  97. $line = '';
  98. }
  99. } else {
  100. $line = $ch.$line;
  101. }
  102. $pos--;
  103. }
  104. fclose($handle);
  105. }
  106. return $entries;
  107. }
  108. public function getLogFilePath():string {
  109. return $this->logFile;
  110. }
  111. }