Logger.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. if (!isset($GLOBALS["HMCLOGGER_TIMEZONE_SET"])) {
  3. @date_default_timezone_set("UTC");
  4. $GLOBALS["HMCLOGGER_TIMEZONE_SET"] = TRUE;
  5. }
  6. /**
  7. * Simple primitive logger.
  8. */
  9. class HMCLogger {
  10. const ALL = 0;
  11. const TRACE = 1;
  12. const DEBUG = 2;
  13. const INFO = 3;
  14. const WARN = 4;
  15. const ERROR = 5;
  16. const FATAL = 6;
  17. const OFF = 10;
  18. // component to log along with each message
  19. private $component;
  20. // log level
  21. private $level;
  22. // log file to log to
  23. private $logFile;
  24. // TODO
  25. const DEFAULT_LOG_FILE = "/tmp/hmc.log";
  26. /**
  27. * Constructor
  28. * @param component Component name to log along with log messages
  29. */
  30. public function __construct($component) {
  31. $this->component = $component;
  32. $this->level = self::ALL;
  33. $this->logFile = self::DEFAULT_LOG_FILE;
  34. if (isset($GLOBALS["HMC_LOG_LEVEL"])) {
  35. $this->level = $GLOBALS["HMC_LOG_LEVEL"];
  36. }
  37. if (isset($GLOBALS["HMC_LOG_FILE"])) {
  38. $this->logFile = $GLOBALS["HMC_LOG_FILE"];
  39. }
  40. }
  41. /**
  42. * Set log level. Overrides global log level for this instance.
  43. */
  44. public function setLevel($level) {
  45. $this->level = $level;
  46. }
  47. /**
  48. * Internal helper function to log messages.
  49. */
  50. private function do_log($level, $msg) {
  51. $trace = debug_backtrace();
  52. $file = "";
  53. $line = "";
  54. $func = "";
  55. if (!empty($trace)) {
  56. $file_idx = 0;
  57. $func_idx = 0;
  58. if (count($trace) >= 2) {
  59. $file_idx = 1;
  60. $func_idx = 2;
  61. }
  62. $file = isset($trace[$file_idx]["file"]) ? basename($trace[$file_idx]["file"]) : "";
  63. $line = isset($trace[$file_idx]["line"]) ? $trace[$file_idx]["line"] : "";
  64. $func = isset($trace[$func_idx]["function"]) ? $trace[$func_idx]["function"] : "";
  65. }
  66. $curTime = @date("Y:m:d H:i:s");
  67. error_log("[$curTime][$level][$this->component][$file:$line][$func]: ".$msg."\n", 3,
  68. $this->logFile);
  69. }
  70. /**
  71. * Log a message with ERROR level
  72. */
  73. public function log_error($msg) {
  74. if ($this->level <= self::ERROR) {
  75. $this->do_log("ERROR", $msg);
  76. }
  77. }
  78. /**
  79. * Log a message with DEBUG level
  80. */
  81. public function log_debug($msg) {
  82. if ($this->level <= self::DEBUG) {
  83. $this->do_log("DEBUG", $msg);
  84. }
  85. }
  86. /**
  87. * Log a message with INFO level
  88. */
  89. public function log_info($msg) {
  90. if ($this->level <= self::INFO) {
  91. $this->do_log("INFO", $msg);
  92. }
  93. }
  94. /**
  95. * Log a message with TRACE level
  96. */
  97. public function log_trace($msg) {
  98. if ($this->level <= self::TRACE) {
  99. $this->do_log("TRACE", $msg);
  100. }
  101. }
  102. /**
  103. * Log a message with WARN level
  104. */
  105. public function log_warn($msg) {
  106. if ($this->level <= self::WARN) {
  107. $this->do_log("WARN", $msg);
  108. }
  109. }
  110. }
  111. ?>