Elgg  Version 1.9
Logger.php
Go to the documentation of this file.
1 <?php
2 
14 class Elgg_Logger {
15 
16  const OFF = 0;
17  const ERROR = 400;
18  const WARNING = 300;
19  const NOTICE = 250;
20  const INFO = 200;
21 
22  protected static $levels = array(
23  0 => 'OFF',
24  200 => 'INFO',
25  250 => 'NOTICE',
26  300 => 'WARNING',
27  400 => 'ERROR',
28  );
29 
31  protected $level = self::ERROR;
32 
34  protected $display = false;
35 
37  protected $hooks;
38 
45  $this->hooks = $hooks;
46  }
47 
54  public function setLevel($level) {
55  // @todo Elgg has used string constants for logging levels
56  if (is_string($level)) {
57  $levelStringsToInts = array_flip(self::$levels);
58  $level = $levelStringsToInts[$level];
59  }
60  $this->level = $level;
61  }
62 
68  public function getLevel() {
69  return $this->level;
70  }
71 
82  public function setDisplay($display) {
83  $this->display = $display;
84  }
85 
93  public function log($message, $level = self::NOTICE) {
94  if ($this->level == self::OFF || $level < $this->level) {
95  return false;
96  }
97 
98  if (!array_key_exists($level, self::$levels)) {
99  return false;
100  }
101 
102  $levelString = self::$levels[$level];
103 
104  // notices and below never displayed to user
105  $display = $this->display && $level > self::NOTICE;
106 
107  $this->process("$levelString: $message", $display, $level);
108 
109  return true;
110  }
111 
118  public function error($message) {
119  return $this->log($message, self::ERROR);
120  }
121 
128  public function warn($message) {
129  return $this->log($message, self::WARNING);
130  }
131 
138  public function notice($message) {
139  return $this->log($message, self::NOTICE);
140  }
141 
148  public function info($message) {
149  return $this->log($message, self::INFO);
150  }
151 
159  public function dump($data, $display = true) {
160  $this->process($data, $display, self::ERROR);
161  }
162 
171  protected function process($data, $display, $level) {
172  global $CONFIG;
173 
174  // plugin can return false to stop the default logging method
175  $params = array(
176  'level' => $level,
177  'msg' => $data,
178  'display' => $display,
179  'to_screen' => $display,
180  );
181 
182  if (!$this->hooks->trigger('debug', 'log', $params, true)) {
183  return;
184  }
185 
186  // Do not want to write to screen before page creation has started.
187  // This is not fool-proof but probably fixes 95% of the cases when logging
188  // results in data sent to the browser before the page is begun.
189  if (!isset($CONFIG->pagesetupdone)) {
190  $display = false;
191  }
192 
193  // Do not want to write to JS or CSS pages
194  if (elgg_in_context('js') || elgg_in_context('css')) {
195  $display = false;
196  }
197 
198  if ($display == true) {
199  echo '<pre class="elgg-logger-data">';
200  echo htmlspecialchars(print_r($data, true), ENT_QUOTES, 'UTF-8');
201  echo '</pre>';
202  } else {
203  error_log(print_r($data, true));
204  }
205  }
206 }
const ERROR
Definition: Logger.php:17
error($message)
Log message at the ERROR level.
Definition: Logger.php:118
process($data, $display, $level)
Process logging data.
Definition: Logger.php:171
log($message, $level=self::NOTICE)
Add a message to the log.
Definition: Logger.php:93
const OFF
Definition: Logger.php:16
notice($message)
Log message at the NOTICE level.
Definition: Logger.php:138
$data
Definition: opendd.php:13
static $levels
Definition: Logger.php:22
const INFO
Definition: Logger.php:20
dump($data, $display=true)
Dump data to log or screen.
Definition: Logger.php:159
$params
Definition: login.php:72
info($message)
Log message at the INFO level.
Definition: Logger.php:148
warn($message)
Log message at the WARNING level.
Definition: Logger.php:128
getLevel()
Get the current logging level.
Definition: Logger.php:68
setDisplay($display)
Set whether the logging should be displayed to the user.
Definition: Logger.php:82
global $CONFIG
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: pageowner.php:273
elgg echo
Translates a string.
Definition: languages.js:43
elgg global
Pointer to the global context.
Definition: elgglib.js:12
elgg ajax ERROR
Definition: ajax.js:33
setLevel($level)
Set the logging level.
Definition: Logger.php:54
__construct(Elgg_PluginHooksService $hooks)
Constructor.
Definition: Logger.php:44
const NOTICE
Definition: Logger.php:19
const WARNING
Definition: Logger.php:18