Logging.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. class Logging {
  3. private static $_logger;
  4. private static $_path;
  5. public static function getLogger()
  6. {
  7. if (!isset(self::$_logger)) {
  8. $writer = new Zend_Log_Writer_Stream(self::$_path);
  9. if (Zend_Version::compareVersion("1.11") > 0) {
  10. //Running Zend version 1.10 or lower. Need to instantiate our
  11. //own Zend Log class with backported code from 1.11.
  12. require_once __DIR__."/AirtimeLog.php";
  13. self::$_logger = new Airtime_Zend_Log($writer);
  14. } else {
  15. self::$_logger = new Zend_Log($writer);
  16. }
  17. self::$_logger->registerErrorHandler();
  18. }
  19. return self::$_logger;
  20. }
  21. public static function setLogPath($path)
  22. {
  23. self::$_path = $path;
  24. }
  25. public static function toString($p_msg)
  26. {
  27. if (is_array($p_msg) || is_object($p_msg)) {
  28. return print_r($p_msg, true);
  29. } else if (is_bool($p_msg)) {
  30. return $p_msg ? "true" : "false";
  31. } else {
  32. return $p_msg;
  33. }
  34. }
  35. /** @param debugMode Prints the function name, file, and line number. This is slow as it uses debug_backtrace()
  36. * so don't use it unless you need it.
  37. */
  38. private static function getLinePrefix($debugMode=false)
  39. {
  40. $linePrefix = "";
  41. if (array_key_exists('SERVER_NAME', $_SERVER)) {
  42. $linePrefix .= $_SERVER['SERVER_NAME'] . " ";
  43. }
  44. if ($debugMode) {
  45. //debug_backtrace is SLOW so we don't want this invoke unless there was a real error! (hence $debugMode)
  46. $bt = debug_backtrace();
  47. $caller = $bt[1];
  48. $file = basename($caller['file']);
  49. $line = $caller['line'];
  50. $function = "Unknown function";
  51. if (array_key_exists(2, $bt)) {
  52. $function = $bt[2]['function'];
  53. }
  54. $linePrefix .= "[$file:$line - $function()] - ";
  55. }
  56. return $linePrefix;
  57. }
  58. public static function info($p_msg)
  59. {
  60. $logger = self::getLogger();
  61. $logger->info(self::getLinePrefix() . self::toString($p_msg));
  62. }
  63. public static function warn($p_msg)
  64. {
  65. $logger = self::getLogger();
  66. $logger->warn(self::getLinePrefix() . self::toString($p_msg));
  67. }
  68. public static function error($p_msg)
  69. {
  70. $logger = self::getLogger();
  71. $logger->err(self::getLinePrefix(true) . self::toString($p_msg));
  72. }
  73. public static function debug($p_msg)
  74. {
  75. if (!(defined('APPLICATION_ENV') && APPLICATION_ENV == "development")) {
  76. return;
  77. }
  78. $logger = self::getLogger();
  79. $logger->debug(self::getLinePrefix(true) . self::toString($p_msg));
  80. }
  81. // kind of like debug but for printing arrays more compactly (skipping
  82. // empty elements
  83. public static function debug_sparse(array $p_msg)
  84. {
  85. Logging::debug("Sparse output:");
  86. Logging::debug( array_filter($p_msg) );
  87. }
  88. public static function enablePropelLogging()
  89. {
  90. $logger = Logging::getLogger();
  91. Propel::setLogger($logger);
  92. $con = Propel::getConnection();
  93. $con->useDebug(true);
  94. $config = Propel::getConfiguration(PropelConfiguration::TYPE_OBJECT);
  95. $config->setParameter('debugpdo.logging.details.method.enabled', true);
  96. $config->setParameter('debugpdo.logging.details.time.enabled', true);
  97. $config->setParameter('debugpdo.logging.details.mem.enabled', true);
  98. }
  99. public static function disablePropelLogging()
  100. {
  101. $con = Propel::getConnection();
  102. $con->useDebug(false);
  103. Propel::setLogger(null);
  104. }
  105. public static function loggingShutdownCallback()
  106. {
  107. //Catch the types of errors that PHP doesn't normally let us catch and
  108. //would otherwise log to the apache log. We route these to our Airtime log to improve the modularity
  109. //and reliability of our error logging. (All errors are in one spot!)
  110. $err = error_get_last();
  111. if (!is_array($err) || !array_key_exists('type', $err)) {
  112. return;
  113. }
  114. switch($err['type'])
  115. {
  116. case E_ERROR:
  117. case E_PARSE:
  118. case E_CORE_ERROR:
  119. case E_CORE_WARNING:
  120. case E_COMPILE_ERROR:
  121. case E_COMPILE_WARNING:
  122. //error_log("Oh noes, a fatal: " . var_export($err, true), 1, 'fatals@example.com');
  123. $errorStr = '';
  124. if (array_key_exists('message', $err)) {
  125. $errorStr .= $err['message'];
  126. }
  127. if (array_key_exists('file', $err))
  128. {
  129. $errorStr .= ' at ' .$err['file'];
  130. }
  131. if (array_key_exists('line', $err))
  132. {
  133. $errorStr .= ':' . $err['line'];
  134. }
  135. $errorStr .= "\n" . var_export($err, true);
  136. Logging::error($errorStr);
  137. break;
  138. }
  139. }
  140. public static function setupParseErrorLogging()
  141. {
  142. //Static callback:
  143. register_shutdown_function('Logging::loggingShutdownCallback');
  144. }
  145. }