owncloud.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' );
  34. self::$logFile=OC_Config::getValue( "logfile", $datadir.'/owncloud.log' );
  35. }
  36. /**
  37. * write a message in the log
  38. * @param string $app
  39. * @param string $message
  40. * @param int level
  41. */
  42. public static function write($app, $message, $level) {
  43. $minLevel=min(OC_Config::getValue( "loglevel", OC_Log::WARN ),OC_Log::ERROR);
  44. if($level>=$minLevel){
  45. $entry=array('app'=>$app, 'message'=>$message, 'level'=>$level,'time'=>time());
  46. $fh=fopen(self::$logFile, 'a');
  47. fwrite($fh, json_encode($entry)."\n");
  48. fclose($fh);
  49. }
  50. }
  51. /**
  52. * get entries from the log in reverse chronological order
  53. * @param int limit
  54. * @param int offset
  55. * @return array
  56. */
  57. public static function getEntries($limit=50, $offset=0){
  58. self::init();
  59. $minLevel=OC_Config::getValue( "loglevel", OC_Log::WARN );
  60. $entries=array();
  61. if(!file_exists(self::$logFile)) {
  62. return array();
  63. }
  64. $contents=file(self::$logFile);
  65. if(!$contents) {//error while reading log
  66. return array();
  67. }
  68. $end=max(count($contents)-$offset-1, 0);
  69. $start=max($end-$limit,0);
  70. $i=$end;
  71. while($i>$start){
  72. $entry=json_decode($contents[$i]);
  73. if($entry->level>=$minLevel){
  74. $entries[]=$entry;
  75. }
  76. $i--;
  77. }
  78. return $entries;
  79. }
  80. }