1
0

File.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author duritong <peter.meier+github@immerda.ch>
  9. * @author Georg Ehrke <oc.list@georgehrke.com>
  10. * @author J0WI <J0WI@users.noreply.github.com>
  11. * @author Joas Schilling <coding@schilljs.com>
  12. * @author Julius Härtl <jus@bitgrid.net>
  13. * @author Lukas Reschke <lukas@statuscode.ch>
  14. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  15. * @author Morris Jobke <hey@morrisjobke.de>
  16. * @author Robin Appelman <robin@icewind.nl>
  17. * @author Roeland Jago Douma <roeland@famdouma.nl>
  18. * @author Roland Tapken <roland@bitarbeiter.net>
  19. * @author Thomas Müller <thomas.mueller@tmit.eu>
  20. * @author Thomas Pulzer <t.pulzer@kniel.de>
  21. *
  22. * @license AGPL-3.0
  23. *
  24. * This code is free software: you can redistribute it and/or modify
  25. * it under the terms of the GNU Affero General Public License, version 3,
  26. * as published by the Free Software Foundation.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU Affero General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU Affero General Public License, version 3,
  34. * along with this program. If not, see <http://www.gnu.org/licenses/>
  35. *
  36. */
  37. namespace OC\Log;
  38. use OC\SystemConfig;
  39. use OCP\ILogger;
  40. use OCP\Log\IFileBased;
  41. use OCP\Log\IWriter;
  42. /**
  43. * logging utilities
  44. *
  45. * Log is saved at data/nextcloud.log (on default)
  46. */
  47. class File extends LogDetails implements IWriter, IFileBased {
  48. protected string $logFile;
  49. protected int $logFileMode;
  50. public function __construct(
  51. string $path,
  52. string $fallbackPath,
  53. private SystemConfig $config,
  54. ) {
  55. parent::__construct($config);
  56. $this->logFile = $path;
  57. if (!file_exists($this->logFile)) {
  58. if (
  59. (
  60. !is_writable(dirname($this->logFile))
  61. || !touch($this->logFile)
  62. )
  63. && $fallbackPath !== ''
  64. ) {
  65. $this->logFile = $fallbackPath;
  66. }
  67. }
  68. $this->logFileMode = $config->getValue('logfilemode', 0640);
  69. }
  70. /**
  71. * write a message in the log
  72. * @param string|array $message
  73. */
  74. public function write(string $app, $message, int $level): void {
  75. $entry = $this->logDetailsAsJSON($app, $message, $level);
  76. $handle = @fopen($this->logFile, 'a');
  77. if ($this->logFileMode > 0 && is_file($this->logFile) && (fileperms($this->logFile) & 0777) != $this->logFileMode) {
  78. @chmod($this->logFile, $this->logFileMode);
  79. }
  80. if ($handle) {
  81. fwrite($handle, $entry."\n");
  82. fclose($handle);
  83. } else {
  84. // Fall back to error_log
  85. error_log($entry);
  86. }
  87. if (php_sapi_name() === 'cli-server') {
  88. if (!\is_string($message)) {
  89. $message = json_encode($message);
  90. }
  91. error_log($message, 4);
  92. }
  93. }
  94. /**
  95. * get entries from the log in reverse chronological order
  96. */
  97. public function getEntries(int $limit = 50, int $offset = 0): array {
  98. $minLevel = $this->config->getValue("loglevel", ILogger::WARN);
  99. $entries = [];
  100. $handle = @fopen($this->logFile, 'rb');
  101. if ($handle) {
  102. fseek($handle, 0, SEEK_END);
  103. $pos = ftell($handle);
  104. $line = '';
  105. $entriesCount = 0;
  106. $lines = 0;
  107. // Loop through each character of the file looking for new lines
  108. while ($pos >= 0 && ($limit === null || $entriesCount < $limit)) {
  109. fseek($handle, $pos);
  110. $ch = fgetc($handle);
  111. if ($ch == "\n" || $pos == 0) {
  112. if ($line != '') {
  113. // Add the first character if at the start of the file,
  114. // because it doesn't hit the else in the loop
  115. if ($pos == 0) {
  116. $line = $ch.$line;
  117. }
  118. $entry = json_decode($line);
  119. // Add the line as an entry if it is passed the offset and is equal or above the log level
  120. if ($entry->level >= $minLevel) {
  121. $lines++;
  122. if ($lines > $offset) {
  123. $entries[] = $entry;
  124. $entriesCount++;
  125. }
  126. }
  127. $line = '';
  128. }
  129. } else {
  130. $line = $ch.$line;
  131. }
  132. $pos--;
  133. }
  134. fclose($handle);
  135. }
  136. return $entries;
  137. }
  138. public function getLogFilePath():string {
  139. return $this->logFile;
  140. }
  141. }