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.

log.php 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2011 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{
  28. const DEBUG=0;
  29. const INFO=1;
  30. const WARN=2;
  31. const ERROR=3;
  32. const FATAL=4;
  33. /**
  34. * write a message in the log
  35. * @param string $app
  36. * @param string $message
  37. * @param int level
  38. */
  39. public static function write($app,$message,$level){
  40. $minLevel=OC_Config::getValue( "loglevel", 2 );
  41. if($level>=$minLevel){
  42. $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' );
  43. $logFile=OC_Config::getValue( "logfile", $datadir.'/owncloud.log' );
  44. $entry=array('app'=>$app,'message'=>$message,'level'=>$level,'time'=>time());
  45. $fh=fopen($logFile,'a');
  46. fwrite($fh,json_encode($entry)."\n");
  47. fclose($fh);
  48. }
  49. }
  50. public static function getEntries(){
  51. $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' );
  52. $logFile=OC_Config::getValue( "logfile", $datadir.'/owncloud.log' );
  53. $entries=array();
  54. if(!file_exists($logFile)){
  55. return array();
  56. }
  57. $fh=fopen($logFile,'r');
  58. while(!feof($fh)){
  59. $line=fgets($fh);
  60. if($line){
  61. $entries[]=json_decode($line);
  62. }
  63. }
  64. fclose($fh);
  65. return $entries;
  66. }
  67. }