owncloud.php 3.9 KB

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. $entry=array('app'=>$app, 'message'=>$message, 'level'=>$level, 'time'=> $time->format($format));
  67. $entry = json_encode($entry);
  68. $handle = @fopen(self::$logFile, 'a');
  69. @chmod(self::$logFile, 0640);
  70. if ($handle) {
  71. fwrite($handle, $entry."\n");
  72. fclose($handle);
  73. } else {
  74. // Fall back to error_log
  75. error_log($entry);
  76. }
  77. }
  78. }
  79. /**
  80. * get entries from the log in reverse chronological order
  81. * @param int $limit
  82. * @param int $offset
  83. * @return array
  84. */
  85. public static function getEntries($limit=50, $offset=0) {
  86. self::init();
  87. $minLevel=OC_Config::getValue( "loglevel", OC_Log::WARN );
  88. $entries = array();
  89. $handle = @fopen(self::$logFile, 'rb');
  90. if ($handle) {
  91. fseek($handle, 0, SEEK_END);
  92. $pos = ftell($handle);
  93. $line = '';
  94. $entriesCount = 0;
  95. $lines = 0;
  96. // Loop through each character of the file looking for new lines
  97. while ($pos >= 0 && $entriesCount < $limit) {
  98. fseek($handle, $pos);
  99. $ch = fgetc($handle);
  100. if ($ch == "\n" || $pos == 0) {
  101. if ($line != '') {
  102. // Add the first character if at the start of the file,
  103. // because it doesn't hit the else in the loop
  104. if ($pos == 0) {
  105. $line = $ch.$line;
  106. }
  107. $entry = json_decode($line);
  108. // Add the line as an entry if it is passed the offset and is equal or above the log level
  109. if ($entry->level >= $minLevel) {
  110. $lines++;
  111. if ($lines > $offset) {
  112. $entries[] = $entry;
  113. $entriesCount++;
  114. }
  115. }
  116. $line = '';
  117. }
  118. } else {
  119. $line = $ch.$line;
  120. }
  121. $pos--;
  122. }
  123. fclose($handle);
  124. }
  125. return $entries;
  126. }
  127. }