owncloud.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * @author Andreas Fischer <bantu@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Björn Schießle <schiessle@owncloud.com>
  7. * @author Felix Moeller <mail@felixmoeller.de>
  8. * @author Frank Karlitschek <frank@owncloud.org>
  9. * @author Georg Ehrke <georg@owncloud.com>
  10. * @author Lukas Reschke <lukas@owncloud.com>
  11. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Owen Winkler <a_github@midnightcircus.com>
  14. * @author Robin Appelman <icewind@owncloud.com>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. * @author Tom Needham <tom@owncloud.com>
  17. *
  18. * @copyright Copyright (c) 2015, ownCloud, Inc.
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. /**
  35. * logging utilities
  36. *
  37. * Log is saved at data/owncloud.log (on default)
  38. */
  39. class OC_Log_Owncloud {
  40. static protected $logFile;
  41. /**
  42. * Init class data
  43. */
  44. public static function init() {
  45. $defaultLogFile = OC_Config::getValue("datadirectory", OC::$SERVERROOT.'/data').'/owncloud.log';
  46. self::$logFile = OC_Config::getValue("logfile", $defaultLogFile);
  47. /*
  48. * Fall back to default log file if specified logfile does not exist
  49. * and can not be created. Error suppression is required in order to
  50. * not end up in the error handler which will try to log the error.
  51. * A better solution (compared to error suppression) would be checking
  52. * !is_writable(dirname(self::$logFile)) before touch(), but
  53. * is_writable() on directories used to be pretty unreliable on Windows
  54. * for at least some time.
  55. */
  56. if (!file_exists(self::$logFile) && !@touch(self::$logFile)) {
  57. self::$logFile = $defaultLogFile;
  58. }
  59. }
  60. /**
  61. * write a message in the log
  62. * @param string $app
  63. * @param string $message
  64. * @param int $level
  65. */
  66. public static function write($app, $message, $level) {
  67. $config = \OC::$server->getSystemConfig();
  68. // default to ISO8601
  69. $format = $config->getValue('logdateformat', 'c');
  70. $logtimezone = $config->getValue( "logtimezone", 'UTC' );
  71. try {
  72. $timezone = new DateTimeZone($logtimezone);
  73. } catch (Exception $e) {
  74. $timezone = new DateTimeZone('UTC');
  75. }
  76. $time = new DateTime(null, $timezone);
  77. $request = \OC::$server->getRequest();
  78. $reqId = $request->getId();
  79. $remoteAddr = $request->getRemoteAddress();
  80. // remove username/passwords from URLs before writing the to the log file
  81. $time = $time->format($format);
  82. $minLevel=min($config->getValue( "loglevel", OC_Log::WARN ), OC_Log::ERROR);
  83. if($minLevel == OC_Log::DEBUG) {
  84. $url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '--';
  85. $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '--';
  86. $entry = compact('reqId', 'remoteAddr', 'app', 'message', 'level', 'time', 'method', 'url');
  87. }
  88. else {
  89. $entry = compact('reqId', 'remoteAddr', 'app', 'message', 'level', 'time');
  90. }
  91. $entry = json_encode($entry);
  92. $handle = @fopen(self::$logFile, 'a');
  93. @chmod(self::$logFile, 0640);
  94. if ($handle) {
  95. fwrite($handle, $entry."\n");
  96. fclose($handle);
  97. } else {
  98. // Fall back to error_log
  99. error_log($entry);
  100. }
  101. }
  102. /**
  103. * get entries from the log in reverse chronological order
  104. * @param int $limit
  105. * @param int $offset
  106. * @return array
  107. */
  108. public static function getEntries($limit=50, $offset=0) {
  109. self::init();
  110. $minLevel=OC_Config::getValue( "loglevel", OC_Log::WARN );
  111. $entries = array();
  112. $handle = @fopen(self::$logFile, 'rb');
  113. if ($handle) {
  114. fseek($handle, 0, SEEK_END);
  115. $pos = ftell($handle);
  116. $line = '';
  117. $entriesCount = 0;
  118. $lines = 0;
  119. // Loop through each character of the file looking for new lines
  120. while ($pos >= 0 && ($limit === null ||$entriesCount < $limit)) {
  121. fseek($handle, $pos);
  122. $ch = fgetc($handle);
  123. if ($ch == "\n" || $pos == 0) {
  124. if ($line != '') {
  125. // Add the first character if at the start of the file,
  126. // because it doesn't hit the else in the loop
  127. if ($pos == 0) {
  128. $line = $ch.$line;
  129. }
  130. $entry = json_decode($line);
  131. // Add the line as an entry if it is passed the offset and is equal or above the log level
  132. if ($entry->level >= $minLevel) {
  133. $lines++;
  134. if ($lines > $offset) {
  135. $entries[] = $entry;
  136. $entriesCount++;
  137. }
  138. }
  139. $line = '';
  140. }
  141. } else {
  142. $line = $ch.$line;
  143. }
  144. $pos--;
  145. }
  146. fclose($handle);
  147. }
  148. return $entries;
  149. }
  150. /**
  151. * @return string
  152. */
  153. public static function getLogFilePath() {
  154. return self::$logFile;
  155. }
  156. }