123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php
- namespace Elgg;
- class Logger {
- const OFF = 0;
- const ERROR = 400;
- const WARNING = 300;
- const NOTICE = 250;
- const INFO = 200;
- protected static $levels = array(
- 0 => 'OFF',
- 200 => 'INFO',
- 250 => 'NOTICE',
- 300 => 'WARNING',
- 400 => 'ERROR',
- );
-
- protected $level = self::ERROR;
-
- protected $display = false;
-
- protected $hooks;
-
- private $CONFIG;
-
- public function __construct(\Elgg\PluginHooksService $hooks) {
- global $CONFIG;
-
- $this->CONFIG = $CONFIG;
- $this->hooks = $hooks;
- }
-
- public function setLevel($level) {
-
- if (is_string($level)) {
- $levelStringsToInts = array_flip(self::$levels);
- $level = $levelStringsToInts[$level];
- }
- $this->level = $level;
- }
-
- public function getLevel() {
- return $this->level;
- }
-
- public function setDisplay($display) {
- $this->display = $display;
- }
-
- public function log($message, $level = self::NOTICE) {
- if ($this->level == self::OFF || $level < $this->level) {
- return false;
- }
- if (!array_key_exists($level, self::$levels)) {
- return false;
- }
- $levelString = self::$levels[$level];
-
- $display = $this->display && $level > self::NOTICE;
- $this->process("$levelString: $message", $display, $level);
- return true;
- }
-
- public function error($message) {
- return $this->log($message, self::ERROR);
- }
-
- public function warn($message) {
- return $this->log($message, self::WARNING);
- }
-
- public function notice($message) {
- return $this->log($message, self::NOTICE);
- }
-
- public function info($message) {
- return $this->log($message, self::INFO);
- }
-
- public function dump($data, $display = true) {
- $this->process($data, $display, self::ERROR);
- }
-
- protected function process($data, $display, $level) {
-
-
- $params = array(
- 'level' => $level,
- 'msg' => $data,
- 'display' => $display,
- 'to_screen' => $display,
- );
- if (!$this->hooks->trigger('debug', 'log', $params, true)) {
- return;
- }
-
-
-
- if (!isset($this->CONFIG->pagesetupdone)) {
- $display = false;
- }
-
- if (elgg_in_context('js') || elgg_in_context('css')) {
- $display = false;
- }
-
- $path = substr(current_page_url(), strlen(elgg_get_site_url()));
- if (preg_match('~^(cache|action)/~', $path)) {
- $display = false;
- }
- if ($display == true) {
- echo '<pre class="elgg-logger-data">';
- echo htmlspecialchars(print_r($data, true), ENT_QUOTES, 'UTF-8');
- echo '</pre>';
- } else {
- error_log(print_r($data, true));
- }
- }
- }
|