Logger.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace Elgg;
  3. /**
  4. * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
  5. *
  6. * Use the elgg_* versions instead.
  7. *
  8. * @access private
  9. *
  10. * @package Elgg.Core
  11. * @subpackage Logging
  12. * @since 1.9.0
  13. */
  14. class Logger {
  15. const OFF = 0;
  16. const ERROR = 400;
  17. const WARNING = 300;
  18. const NOTICE = 250;
  19. const INFO = 200;
  20. protected static $levels = array(
  21. 0 => 'OFF',
  22. 200 => 'INFO',
  23. 250 => 'NOTICE',
  24. 300 => 'WARNING',
  25. 400 => 'ERROR',
  26. );
  27. /** @var int $level The logging level */
  28. protected $level = self::ERROR;
  29. /** @var bool $display Display to user? */
  30. protected $display = false;
  31. /** @var \Elgg\PluginHooksService $hooks */
  32. protected $hooks;
  33. /** @var \stdClass Global Elgg configuration */
  34. private $CONFIG;
  35. /**
  36. * Constructor
  37. *
  38. * @param \Elgg\PluginHooksService $hooks Hooks service
  39. */
  40. public function __construct(\Elgg\PluginHooksService $hooks) {
  41. global $CONFIG;
  42. $this->CONFIG = $CONFIG;
  43. $this->hooks = $hooks;
  44. }
  45. /**
  46. * Set the logging level
  47. *
  48. * @param int $level The logging level
  49. * @return void
  50. */
  51. public function setLevel($level) {
  52. // @todo Elgg has used string constants for logging levels
  53. if (is_string($level)) {
  54. $levelStringsToInts = array_flip(self::$levels);
  55. $level = $levelStringsToInts[$level];
  56. }
  57. $this->level = $level;
  58. }
  59. /**
  60. * Get the current logging level
  61. *
  62. * @return int
  63. */
  64. public function getLevel() {
  65. return $this->level;
  66. }
  67. /**
  68. * Set whether the logging should be displayed to the user
  69. *
  70. * Whether data is actually displayed to the user depends on this setting
  71. * and other factors such as whether we are generating a JavaScript or CSS
  72. * file.
  73. *
  74. * @param bool $display Whether to display logging
  75. * @return void
  76. */
  77. public function setDisplay($display) {
  78. $this->display = $display;
  79. }
  80. /**
  81. * Add a message to the log
  82. *
  83. * @param string $message The message to log
  84. * @param int $level The logging level
  85. * @return bool Whether the messages was logged
  86. */
  87. public function log($message, $level = self::NOTICE) {
  88. if ($this->level == self::OFF || $level < $this->level) {
  89. return false;
  90. }
  91. if (!array_key_exists($level, self::$levels)) {
  92. return false;
  93. }
  94. $levelString = self::$levels[$level];
  95. // notices and below never displayed to user
  96. $display = $this->display && $level > self::NOTICE;
  97. $this->process("$levelString: $message", $display, $level);
  98. return true;
  99. }
  100. /**
  101. * Log message at the ERROR level
  102. *
  103. * @param string $message The message to log
  104. * @return bool
  105. */
  106. public function error($message) {
  107. return $this->log($message, self::ERROR);
  108. }
  109. /**
  110. * Log message at the WARNING level
  111. *
  112. * @param string $message The message to log
  113. * @return bool
  114. */
  115. public function warn($message) {
  116. return $this->log($message, self::WARNING);
  117. }
  118. /**
  119. * Log message at the NOTICE level
  120. *
  121. * @param string $message The message to log
  122. * @return bool
  123. */
  124. public function notice($message) {
  125. return $this->log($message, self::NOTICE);
  126. }
  127. /**
  128. * Log message at the INFO level
  129. *
  130. * @param string $message The message to log
  131. * @return bool
  132. */
  133. public function info($message) {
  134. return $this->log($message, self::INFO);
  135. }
  136. /**
  137. * Dump data to log or screen
  138. *
  139. * @param mixed $data The data to log
  140. * @param bool $display Whether to include this in the HTML page
  141. * @return void
  142. */
  143. public function dump($data, $display = true) {
  144. $this->process($data, $display, self::ERROR);
  145. }
  146. /**
  147. * Process logging data
  148. *
  149. * @param mixed $data The data to process
  150. * @param bool $display Whether to display the data to the user. Otherwise log it.
  151. * @param int $level The logging level for this data
  152. * @return void
  153. */
  154. protected function process($data, $display, $level) {
  155. // plugin can return false to stop the default logging method
  156. $params = array(
  157. 'level' => $level,
  158. 'msg' => $data,
  159. 'display' => $display,
  160. 'to_screen' => $display,
  161. );
  162. if (!$this->hooks->trigger('debug', 'log', $params, true)) {
  163. return;
  164. }
  165. // Do not want to write to screen before page creation has started.
  166. // This is not fool-proof but probably fixes 95% of the cases when logging
  167. // results in data sent to the browser before the page is begun.
  168. if (!isset($this->CONFIG->pagesetupdone)) {
  169. $display = false;
  170. }
  171. // Do not want to write to JS or CSS pages
  172. if (elgg_in_context('js') || elgg_in_context('css')) {
  173. $display = false;
  174. }
  175. // don't display in simplecache requests
  176. $path = substr(current_page_url(), strlen(elgg_get_site_url()));
  177. if (preg_match('~^(cache|action)/~', $path)) {
  178. $display = false;
  179. }
  180. if ($display == true) {
  181. echo '<pre class="elgg-logger-data">';
  182. echo htmlspecialchars(print_r($data, true), ENT_QUOTES, 'UTF-8');
  183. echo '</pre>';
  184. } else {
  185. error_log(print_r($data, true));
  186. }
  187. }
  188. }