Elgg  Version 1.11
Logger.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
3 
15 class Logger {
16 
17  const OFF = 0;
18  const ERROR = 400;
19  const WARNING = 300;
20  const NOTICE = 250;
21  const INFO = 200;
22 
23  protected static $levels = array(
24  0 => 'OFF',
25  200 => 'INFO',
26  250 => 'NOTICE',
27  300 => 'WARNING',
28  400 => 'ERROR',
29  );
30 
32  protected $level = self::ERROR;
33 
35  protected $display = false;
36 
38  protected $hooks;
39 
41  private $CONFIG;
42 
48  public function __construct(\Elgg\PluginHooksService $hooks) {
50 
51  $this->CONFIG = $CONFIG;
52  $this->hooks = $hooks;
53  }
54 
61  public function setLevel($level) {
62  // @todo Elgg has used string constants for logging levels
63  if (is_string($level)) {
64  $levelStringsToInts = array_flip(self::$levels);
65  $level = $levelStringsToInts[$level];
66  }
67  $this->level = $level;
68  }
69 
75  public function getLevel() {
76  return $this->level;
77  }
78 
89  public function setDisplay($display) {
90  $this->display = $display;
91  }
92 
100  public function log($message, $level = self::NOTICE) {
101  if ($this->level == self::OFF || $level < $this->level) {
102  return false;
103  }
104 
105  if (!array_key_exists($level, self::$levels)) {
106  return false;
107  }
108 
109  $levelString = self::$levels[$level];
110 
111  // notices and below never displayed to user
112  $display = $this->display && $level > self::NOTICE;
113 
114  $this->process("$levelString: $message", $display, $level);
115 
116  return true;
117  }
118 
125  public function error($message) {
126  return $this->log($message, self::ERROR);
127  }
128 
135  public function warn($message) {
136  return $this->log($message, self::WARNING);
137  }
138 
145  public function notice($message) {
146  return $this->log($message, self::NOTICE);
147  }
148 
155  public function info($message) {
156  return $this->log($message, self::INFO);
157  }
158 
166  public function dump($data, $display = true) {
167  $this->process($data, $display, self::ERROR);
168  }
169 
178  protected function process($data, $display, $level) {
179 
180 
181  // plugin can return false to stop the default logging method
182  $params = array(
183  'level' => $level,
184  'msg' => $data,
185  'display' => $display,
186  'to_screen' => $display,
187  );
188 
189  if (!$this->hooks->trigger('debug', 'log', $params, true)) {
190  return;
191  }
192 
193  // Do not want to write to screen before page creation has started.
194  // This is not fool-proof but probably fixes 95% of the cases when logging
195  // results in data sent to the browser before the page is begun.
196  if (!isset($this->CONFIG->pagesetupdone)) {
197  $display = false;
198  }
199 
200  // Do not want to write to JS or CSS pages
201  if (elgg_in_context('js') || elgg_in_context('css')) {
202  $display = false;
203  }
204 
205  if ($display == true) {
206  echo '<pre class="elgg-logger-data">';
207  echo htmlspecialchars(print_r($data, true), ENT_QUOTES, 'UTF-8');
208  echo '</pre>';
209  } else {
210  error_log(print_r($data, true));
211  }
212  }
213 }
214 
$data
Definition: opendd.php:13
process($data, $display, $level)
Process logging data.
Definition: Logger.php:178
$params
Definition: login.php:72
getLevel()
Get the current logging level.
Definition: Logger.php:75
setDisplay($display)
Set whether the logging should be displayed to the user.
Definition: Logger.php:89
setLevel($level)
Set the logging level.
Definition: Logger.php:61
Save menu items.
global $CONFIG
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: pageowner.php:250
elgg echo
Translates a string.
Definition: languages.js:43
elgg global
Pointer to the global context.
Definition: elgglib.js:12
error($message)
Log message at the ERROR level.
Definition: Logger.php:125
log($message, $level=self::NOTICE)
Add a message to the log.
Definition: Logger.php:100
dump($data, $display=true)
Dump data to log or screen.
Definition: Logger.php:166
__construct(\Elgg\PluginHooksService $hooks)
Constructor.
Definition: Logger.php:48
info($message)
Log message at the INFO level.
Definition: Logger.php:155
elgg ajax ERROR
Definition: ajax.js:33
warn($message)
Log message at the WARNING level.
Definition: Logger.php:135
notice($message)
Log message at the NOTICE level.
Definition: Logger.php:145