File.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andreas Fischer <bantu@owncloud.com>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Georg Ehrke <georg@owncloud.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Phiber2000 <phiber2000@gmx.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. * @author Thomas Pulzer <t.pulzer@kniel.de>
  16. * @author Vincent Petry <pvince81@owncloud.com>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OC\Log;
  34. /**
  35. * logging utilities
  36. *
  37. * Log is saved at data/nextcloud.log (on default)
  38. */
  39. class File {
  40. static protected $logFile;
  41. /**
  42. * Init class data
  43. */
  44. public static function init() {
  45. $systemConfig = \OC::$server->getSystemConfig();
  46. $defaultLogFile = $systemConfig->getValue("datadirectory", \OC::$SERVERROOT.'/data').'/nextcloud.log';
  47. self::$logFile = $systemConfig->getValue("logfile", $defaultLogFile);
  48. /**
  49. * Fall back to default log file if specified logfile does not exist
  50. * and can not be created.
  51. */
  52. if (!file_exists(self::$logFile)) {
  53. if(!is_writable(dirname(self::$logFile))) {
  54. self::$logFile = $defaultLogFile;
  55. } else {
  56. if(!touch(self::$logFile)) {
  57. self::$logFile = $defaultLogFile;
  58. }
  59. }
  60. }
  61. }
  62. /**
  63. * write a message in the log
  64. * @param string $app
  65. * @param string $message
  66. * @param int $level
  67. */
  68. public static function write($app, $message, $level) {
  69. $config = \OC::$server->getSystemConfig();
  70. // default to ISO8601
  71. $format = $config->getValue('logdateformat', 'c');
  72. $logTimeZone = $config->getValue( "logtimezone", 'UTC' );
  73. try {
  74. $timezone = new \DateTimeZone($logTimeZone);
  75. } catch (\Exception $e) {
  76. $timezone = new \DateTimeZone('UTC');
  77. }
  78. $time = \DateTime::createFromFormat("U.u", number_format(microtime(true), 4, ".", ""));
  79. if ($time === false) {
  80. $time = new \DateTime(null, $timezone);
  81. } else {
  82. // apply timezone if $time is created from UNIX timestamp
  83. $time->setTimezone($timezone);
  84. }
  85. $request = \OC::$server->getRequest();
  86. $reqId = $request->getId();
  87. $remoteAddr = $request->getRemoteAddress();
  88. // remove username/passwords from URLs before writing the to the log file
  89. $time = $time->format($format);
  90. $url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--';
  91. $method = is_string($request->getMethod()) ? $request->getMethod() : '--';
  92. if(\OC::$server->getConfig()->getSystemValue('installed', false)) {
  93. $user = (\OC_User::getUser()) ? \OC_User::getUser() : '--';
  94. } else {
  95. $user = '--';
  96. }
  97. $entry = compact(
  98. 'reqId',
  99. 'remoteAddr',
  100. 'app',
  101. 'message',
  102. 'level',
  103. 'time',
  104. 'method',
  105. 'url',
  106. 'user'
  107. );
  108. $entry = json_encode($entry);
  109. $handle = @fopen(self::$logFile, 'a');
  110. @chmod(self::$logFile, 0640);
  111. if ($handle) {
  112. fwrite($handle, $entry."\n");
  113. fclose($handle);
  114. } else {
  115. // Fall back to error_log
  116. error_log($entry);
  117. }
  118. if (php_sapi_name() === 'cli-server') {
  119. error_log($message, 4);
  120. }
  121. }
  122. /**
  123. * get entries from the log in reverse chronological order
  124. * @param int $limit
  125. * @param int $offset
  126. * @return array
  127. */
  128. public static function getEntries($limit=50, $offset=0) {
  129. self::init();
  130. $minLevel = \OC::$server->getSystemConfig()->getValue("loglevel", \OCP\Util::WARN);
  131. $entries = array();
  132. $handle = @fopen(self::$logFile, 'rb');
  133. if ($handle) {
  134. fseek($handle, 0, SEEK_END);
  135. $pos = ftell($handle);
  136. $line = '';
  137. $entriesCount = 0;
  138. $lines = 0;
  139. // Loop through each character of the file looking for new lines
  140. while ($pos >= 0 && ($limit === null ||$entriesCount < $limit)) {
  141. fseek($handle, $pos);
  142. $ch = fgetc($handle);
  143. if ($ch == "\n" || $pos == 0) {
  144. if ($line != '') {
  145. // Add the first character if at the start of the file,
  146. // because it doesn't hit the else in the loop
  147. if ($pos == 0) {
  148. $line = $ch.$line;
  149. }
  150. $entry = json_decode($line);
  151. // Add the line as an entry if it is passed the offset and is equal or above the log level
  152. if ($entry->level >= $minLevel) {
  153. $lines++;
  154. if ($lines > $offset) {
  155. $entries[] = $entry;
  156. $entriesCount++;
  157. }
  158. }
  159. $line = '';
  160. }
  161. } else {
  162. $line = $ch.$line;
  163. }
  164. $pos--;
  165. }
  166. fclose($handle);
  167. }
  168. return $entries;
  169. }
  170. /**
  171. * @return string
  172. */
  173. public static function getLogFilePath() {
  174. return self::$logFile;
  175. }
  176. }