You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.4 KiB

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