/core/classes/TBGLogging.class.php

https://github.com/pb30/thebuggenie · PHP · 204 lines · 121 code · 21 blank · 62 comment · 18 complexity · 89cdd8f00264c1f14dca9c67ef60014d MD5 · raw file

  1. <?php
  2. /**
  3. * Logging class
  4. *
  5. * @author Daniel Andre Eikeland <zegenie@zegeniestudios.net>
  6. * @version 3.1
  7. * @license http://www.opensource.org/licenses/mozilla1.1.php Mozilla Public License 1.1 (MPL 1.1)
  8. * @package thebuggenie
  9. * @subpackage core
  10. */
  11. /**
  12. * Logging class
  13. *
  14. * @package thebuggenie
  15. * @subpackage core
  16. */
  17. class TBGLogging
  18. {
  19. const LEVEL_INFO = 1;
  20. const LEVEL_NOTICE = 5;
  21. const LEVEL_WARNING = 10;
  22. const LEVEL_WARNING_RISK = 15;
  23. const LEVEL_FATAL = 20;
  24. protected static $_logging_enabled = true;
  25. protected static $_logfile;
  26. protected static $_logonajaxcalls = true;
  27. protected static $_entries = array();
  28. protected static $_categorized_entries = array();
  29. protected static $_loglevel = 1;
  30. protected static $_cli_log_to_screen_in_debug_mode = false;
  31. /**
  32. * Log a message to the logger
  33. *
  34. * @param string $message The message to log
  35. * @param string $category[optional] The message category (default "main")
  36. * @param integer $level[optional] The loglevel
  37. */
  38. public static function log($message, $category = 'main', $level = 1)
  39. {
  40. if (!self::$_logging_enabled) return false;
  41. if (self::$_loglevel > $level) return false;
  42. if (self::$_cli_log_to_screen_in_debug_mode && TBGContext::isCLI() && TBGContext::isDebugMode() && class_exists('TBGCliCommand'))
  43. {
  44. TBGCliCommand::cli_echo(mb_strtoupper(self::getLevelName($level)), 'white', 'bold');
  45. TBGCliCommand::cli_echo(" [{$category}] ", 'green', 'bold');
  46. TBGCliCommand::cli_echo("$message\n");
  47. }
  48. if (self::$_logonajaxcalls || TBGContext::getRequest()->isAjaxCall())
  49. {
  50. if (self::$_logfile !== null)
  51. {
  52. file_put_contents(self::$_logfile, mb_strtoupper(self::getLevelName($level)) . " [{$category}] {$message}\n", FILE_APPEND);
  53. }
  54. $time_msg = (TBGContext::isDebugMode()) ? (($load_time = TBGContext::getLoadtime()) >= 1) ? round($load_time, 2) . ' seconds' : round(($load_time * 1000), 3) . ' ms' : '';
  55. self::$_entries[] = array('category' => $category, 'time' => $time_msg, 'message' => $message, 'level' => $level);
  56. self::$_categorized_entries[$category][] = array('time' => $time_msg, 'message' => $message, 'level' => $level);
  57. }
  58. }
  59. /**
  60. * Get the level name for a given level
  61. *
  62. * @param integer $level
  63. *
  64. * @return string
  65. */
  66. public static function getLevelName($level)
  67. {
  68. switch ($level)
  69. {
  70. case self::LEVEL_INFO:
  71. return 'info';
  72. case self::LEVEL_NOTICE:
  73. return 'notice';
  74. case self::LEVEL_WARNING:
  75. return 'warning';
  76. case self::LEVEL_WARNING_RISK:
  77. return 'risk';
  78. case self::LEVEL_FATAL:
  79. return 'fatal';
  80. default:
  81. return 'unknown';
  82. }
  83. }
  84. public static function setLogFilePath($filename)
  85. {
  86. if (!file_exists($filename))
  87. {
  88. throw new Exception('Invalid log filename');
  89. }
  90. self::$_logfile = $filename;
  91. }
  92. /**
  93. * Return the color assigned to a specific category
  94. *
  95. * @param string $category
  96. *
  97. * @return string
  98. */
  99. public static function getCategoryColor($category)
  100. {
  101. switch ($category)
  102. {
  103. case 'main':
  104. return "55C";
  105. case 'B2DB':
  106. return "33B";
  107. case 'routing':
  108. return "5C5";
  109. case 'i18n':
  110. return "A83";
  111. case 'cache':
  112. return "8A3";
  113. case 'search':
  114. return "2FA";
  115. case 'publish':
  116. return "A79";
  117. default:
  118. return "999";
  119. }
  120. }
  121. /**
  122. * Get current logged entries
  123. *
  124. * @return array
  125. */
  126. public static function getEntries()
  127. {
  128. return self::$_entries;
  129. }
  130. /**
  131. * Get complete log entries for a specific category
  132. *
  133. * @param string $category
  134. * @param integer $min_level[optional]
  135. *
  136. * @return array
  137. */
  138. public static function getEntriesForCategory($category, $min_level = 1)
  139. {
  140. $retval = array();
  141. foreach (self::$_entries as $entry)
  142. {
  143. if ($entry['category'] == $category && $entry['level'] >= $min_level)
  144. {
  145. $retval[] = $entry;
  146. }
  147. }
  148. return $retval;
  149. }
  150. /**
  151. * Get log messages for a specific category
  152. *
  153. * @param string $category
  154. * @param integer $min_level[optional]
  155. *
  156. * @return array
  157. */
  158. public static function getMessagesForCategory($category, $min_level = 1)
  159. {
  160. $retval = array();
  161. foreach (self::$_entries as $entry)
  162. {
  163. if ($entry['category'] == $category && $entry['level'] >= $min_level)
  164. {
  165. $retval[] = $entry['message'];
  166. }
  167. }
  168. return $retval;
  169. }
  170. /**
  171. * Return whether logging is enabled
  172. *
  173. * @return boolean
  174. */
  175. public static function isEnabled()
  176. {
  177. return self::$_logging_enabled;
  178. }
  179. public static function setCLIDebug($value = true)
  180. {
  181. self::$_cli_log_to_screen_in_debug_mode = $value;
  182. }
  183. }