log.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. /**
  9. * logging utilities
  10. *
  11. * Log is saved by default at data/owncloud.log using OC_Log_Owncloud.
  12. * Selecting other backend is done with a config option 'log_type'.
  13. */
  14. class OC_Log {
  15. const DEBUG=0;
  16. const INFO=1;
  17. const WARN=2;
  18. const ERROR=3;
  19. const FATAL=4;
  20. static public $enabled = true;
  21. static protected $class = null;
  22. /**
  23. * write a message in the log
  24. * @param string $app
  25. * @param string $message
  26. * @param int level
  27. */
  28. public static function write($app, $message, $level) {
  29. if (self::$enabled) {
  30. if (!self::$class) {
  31. self::$class = 'OC_Log_'.ucfirst(OC_Config::getValue('log_type', 'owncloud'));
  32. call_user_func(array(self::$class, 'init'));
  33. }
  34. $log_class=self::$class;
  35. $log_class::write($app, $message, $level);
  36. }
  37. }
  38. //Fatal errors handler
  39. public static function onShutdown() {
  40. $error = error_get_last();
  41. if($error) {
  42. //ob_end_clean();
  43. self::write('PHP', $error['message'] . ' at ' . $error['file'] . '#' . $error['line'], self::FATAL);
  44. } else {
  45. return true;
  46. }
  47. }
  48. // Uncaught exception handler
  49. public static function onException($exception) {
  50. self::write('PHP', $exception->getMessage() . ' at ' . $exception->getFile() . '#' . $exception->getLine(), self::FATAL);
  51. }
  52. //Recoverable errors handler
  53. public static function onError($number, $message, $file, $line) {
  54. if (error_reporting() === 0) {
  55. return;
  56. }
  57. self::write('PHP', $message . ' at ' . $file . '#' . $line, self::WARN);
  58. }
  59. }