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.

owncloud.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2012 Robin Appelman icewind1991@gmail.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * logging utilities
  24. *
  25. * Log is saved at data/owncloud.log (on default)
  26. */
  27. class OC_Log_Owncloud {
  28. static protected $logFile;
  29. /**
  30. * Init class data
  31. */
  32. public static function init() {
  33. $defaultLogFile = OC_Config::getValue("datadirectory", OC::$SERVERROOT.'/data').'/owncloud.log';
  34. self::$logFile = OC_Config::getValue("logfile", $defaultLogFile);
  35. /*
  36. * Fall back to default log file if specified logfile does not exist
  37. * and can not be created. Error suppression is required in order to
  38. * not end up in the error handler which will try to log the error.
  39. * A better solution (compared to error suppression) would be checking
  40. * !is_writable(dirname(self::$logFile)) before touch(), but
  41. * is_writable() on directories used to be pretty unreliable on Windows
  42. * for at least some time.
  43. */
  44. if (!file_exists(self::$logFile) && !@touch(self::$logFile)) {
  45. self::$logFile = $defaultLogFile;
  46. }
  47. }
  48. /**
  49. * write a message in the log
  50. * @param string $app
  51. * @param string $message
  52. * @param int $level
  53. */
  54. public static function write($app, $message, $level) {
  55. $minLevel=min(OC_Config::getValue( "loglevel", OC_Log::WARN ), OC_Log::ERROR);
  56. if($level>=$minLevel) {
  57. // default to ISO8601
  58. $format = OC_Config::getValue('logdateformat', 'c');
  59. $logtimezone=OC_Config::getValue( "logtimezone", 'UTC' );
  60. try {
  61. $timezone = new DateTimeZone($logtimezone);
  62. } catch (Exception $e) {
  63. $timezone = new DateTimeZone('UTC');
  64. }
  65. $time = new DateTime(null, $timezone);
  66. // remove username/passswords from URLs before writing the to the log file
  67. $entry=array('app'=>$app, 'message'=>$message, 'level'=>$level, 'time'=> $time->format($format));
  68. $entry = json_encode($entry);
  69. $handle = @fopen(self::$logFile, 'a');
  70. @chmod(self::$logFile, 0640);
  71. if ($handle) {
  72. fwrite($handle, $entry."\n");
  73. fclose($handle);
  74. } else {
  75. // Fall back to error_log
  76. error_log($entry);
  77. }
  78. }
  79. }
  80. /**
  81. * get entries from the log in reverse chronological order
  82. * @param int $limit
  83. * @param int $offset
  84. * @return array
  85. */
  86. public static function getEntries($limit=50, $offset=0) {
  87. self::init();
  88. $minLevel=OC_Config::getValue( "loglevel", OC_Log::WARN );
  89. $entries = array();
  90. $handle = @fopen(self::$logFile, 'rb');
  91. if ($handle) {
  92. fseek($handle, 0, SEEK_END);
  93. $pos = ftell($handle);
  94. $line = '';
  95. $entriesCount = 0;
  96. $lines = 0;
  97. // Loop through each character of the file looking for new lines
  98. while ($pos >= 0 && $entriesCount < $limit) {
  99. fseek($handle, $pos);
  100. $ch = fgetc($handle);
  101. if ($ch == "\n" || $pos == 0) {
  102. if ($line != '') {
  103. // Add the first character if at the start of the file,
  104. // because it doesn't hit the else in the loop
  105. if ($pos == 0) {
  106. $line = $ch.$line;
  107. }
  108. $entry = json_decode($line);
  109. // Add the line as an entry if it is passed the offset and is equal or above the log level
  110. if ($entry->level >= $minLevel) {
  111. $lines++;
  112. if ($lines > $offset) {
  113. $entries[] = $entry;
  114. $entriesCount++;
  115. }
  116. }
  117. $line = '';
  118. }
  119. } else {
  120. $line = $ch.$line;
  121. }
  122. $pos--;
  123. }
  124. fclose($handle);
  125. }
  126. return $entries;
  127. }
  128. }