Logger.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * @file
  4. * Trivial logger for easier debug.
  5. */
  6. namespace OSInet\Class_Grapher;
  7. class Logger {
  8. public $fDebugLevel;
  9. public function __construct($debugLevel = LOG_WARNING) {
  10. $this->debugLevel($debugLevel);
  11. }
  12. /**
  13. * Get or Set the current debug level.
  14. *
  15. * @param int $level
  16. * LOG_EMERG to LOG_DEBUG. If NULL, do not set it.
  17. *
  18. * @return
  19. * The value of the debug level before this function was called.
  20. */
  21. public function debugLevel($level = NULL) {
  22. $ret = $this->fDebugLevel;
  23. if (isset($level)) {
  24. if ($level < LOG_EMERG) {
  25. $level = LOG_EMERG;
  26. }
  27. if ($level > LOG_DEBUG) {
  28. $level = LOG_DEBUG;
  29. }
  30. $this->fDebugLevel = $level;
  31. }
  32. return $ret;
  33. }
  34. /**
  35. * Output a message on STDERR if its relevance is above minimum level.
  36. *
  37. * @param string $message
  38. * @param int $level
  39. * Defined in RFC 3164, section 4.1.1 "Severity". But compare with Drupal 7
  40. * WATCHDOG_* constants in includes/bootstrap.inc for an explanation about
  41. * syslog constants in PHP.
  42. */
  43. public function debug($message = "\n", $level = LOG_INFO) {
  44. if ($level <= $this->fDebugLevel) {
  45. fputs(STDERR, $message);
  46. fflush(STDERR);
  47. }
  48. }
  49. }