File.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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', \DateTime::ATOM);
  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($config->getValue('installed', false)) {
  93. $user = (\OC_User::getUser()) ? \OC_User::getUser() : '--';
  94. } else {
  95. $user = '--';
  96. }
  97. $version = $config->getValue('version', '');
  98. $entry = compact(
  99. 'reqId',
  100. 'remoteAddr',
  101. 'app',
  102. 'message',
  103. 'level',
  104. 'time',
  105. 'method',
  106. 'url',
  107. 'user',
  108. 'version'
  109. );
  110. $entry = json_encode($entry);
  111. $handle = @fopen(self::$logFile, 'a');
  112. @chmod(self::$logFile, 0640);
  113. if ($handle) {
  114. fwrite($handle, $entry."\n");
  115. fclose($handle);
  116. } else {
  117. // Fall back to error_log
  118. error_log($entry);
  119. }
  120. if (php_sapi_name() === 'cli-server') {
  121. error_log($message, 4);
  122. }
  123. }
  124. /**
  125. * get entries from the log in reverse chronological order
  126. * @param int $limit
  127. * @param int $offset
  128. * @return array
  129. */
  130. public static function getEntries($limit=50, $offset=0) {
  131. self::init();
  132. $minLevel = \OC::$server->getSystemConfig()->getValue("loglevel", \OCP\Util::WARN);
  133. $entries = array();
  134. $handle = @fopen(self::$logFile, 'rb');
  135. if ($handle) {
  136. fseek($handle, 0, SEEK_END);
  137. $pos = ftell($handle);
  138. $line = '';
  139. $entriesCount = 0;
  140. $lines = 0;
  141. // Loop through each character of the file looking for new lines
  142. while ($pos >= 0 && ($limit === null ||$entriesCount < $limit)) {
  143. fseek($handle, $pos);
  144. $ch = fgetc($handle);
  145. if ($ch == "\n" || $pos == 0) {
  146. if ($line != '') {
  147. // Add the first character if at the start of the file,
  148. // because it doesn't hit the else in the loop
  149. if ($pos == 0) {
  150. $line = $ch.$line;
  151. }
  152. $entry = json_decode($line);
  153. // Add the line as an entry if it is passed the offset and is equal or above the log level
  154. if ($entry->level >= $minLevel) {
  155. $lines++;
  156. if ($lines > $offset) {
  157. $entries[] = $entry;
  158. $entriesCount++;
  159. }
  160. }
  161. $line = '';
  162. }
  163. } else {
  164. $line = $ch.$line;
  165. }
  166. $pos--;
  167. }
  168. fclose($handle);
  169. }
  170. return $entries;
  171. }
  172. /**
  173. * @return string
  174. */
  175. public static function getLogFilePath() {
  176. return self::$logFile;
  177. }
  178. }