Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
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 
34  protected $level = self::ERROR;
35 
39  protected $display = false;
40 
44  protected $hooks;
45 
49  private $config;
50 
54  private $context;
55 
59  private $disabled_stack;
60 
68  public function __construct(PluginHooksService $hooks, Config $config, Context $context) {
69  $this->config = $config;
70  $this->hooks = $hooks;
71  $this->context = $context;
72  }
73 
80  public function setLevel($level) {
81  // @todo Elgg has used string constants for logging levels
82  if (is_string($level)) {
83  $levelStringsToInts = array_flip(self::$levels);
84  $level = $levelStringsToInts[$level];
85  }
86  $this->level = $level;
87  }
88 
94  public function getLevel() {
95  return $this->level;
96  }
97 
108  public function setDisplay($display) {
109  $this->display = $display;
110  }
111 
119  public function log($message, $level = self::NOTICE) {
120  if ($this->disabled_stack) {
121  // capture to top of stack
122  end($this->disabled_stack);
123  $key = key($this->disabled_stack);
124  $this->disabled_stack[$key][] = [
125  'message' => $message,
126  'level' => $level,
127  ];
128  }
129 
130  if ($this->level == self::OFF || $level < $this->level) {
131  return false;
132  }
133 
134  if (!array_key_exists($level, self::$levels)) {
135  return false;
136  }
137 
138  // when capturing, still use consistent return value
139  if ($this->disabled_stack) {
140  return true;
141  }
142 
143  $levelString = self::$levels[$level];
144 
145  // notices and below never displayed to user
146  $display = $this->display && $level > self::NOTICE;
147 
148  $this->process("$levelString: $message", $display, $level);
149 
150  return true;
151  }
152 
159  public function error($message) {
160  return $this->log($message, self::ERROR);
161  }
162 
169  public function warn($message) {
170  return $this->log($message, self::WARNING);
171  }
172 
179  public function notice($message) {
180  return $this->log($message, self::NOTICE);
181  }
182 
189  public function info($message) {
190  return $this->log($message, self::INFO);
191  }
192 
200  public function dump($data, $display = true) {
201  $this->process($data, $display, self::ERROR);
202  }
203 
212  protected function process($data, $display, $level) {
213 
214 
215  // plugin can return false to stop the default logging method
216  $params = array(
217  'level' => $level,
218  'msg' => $data,
219  'display' => $display,
220  'to_screen' => $display,
221  );
222 
223  if (!$this->hooks->trigger('debug', 'log', $params, true)) {
224  return;
225  }
226 
227  // Do not want to write to screen before page creation has started.
228  // This is not fool-proof but probably fixes 95% of the cases when logging
229  // results in data sent to the browser before the page is begun.
230  if (!isset($GLOBALS['_ELGG']->pagesetupdone)) {
231  $display = false;
232  }
233 
234  // Do not want to write to JS or CSS pages
235  if ($this->context->contains('js') || $this->context->contains('css')) {
236  $display = false;
237  }
238 
239  // don't display in simplecache requests
240  $path = substr(current_page_url(), strlen(elgg_get_site_url()));
241  if (preg_match('~^(cache|action|serve-file)/~', $path)) {
242  $display = false;
243  }
244 
245  if ($display == true) {
246  echo '<pre class="elgg-logger-data">';
247  echo htmlspecialchars(print_r($data, true), ENT_QUOTES, 'UTF-8');
248  echo '</pre>';
249  }
250 
251  error_log(print_r($data, true));
252  }
253 
267  public function disable() {
268  $this->disabled_stack[] = [];
269  }
270 
279  public function enable() {
280  return array_pop($this->disabled_stack);
281  }
282 }
283 
disable()
Temporarily disable logging and capture logs (before tests)
Definition: Logger.php:267
$context
Definition: add.php:11
$data
Definition: opendd.php:13
$path
Definition: details.php:88
Access to configuration values.
Definition: Config.php:11
current_page_url()
Returns the current page's complete URL.
Definition: input.php:65
process($data, $display, $level)
Process logging data.
Definition: Logger.php:212
$params
Definition: login.php:72
getLevel()
Get the current logging level.
Definition: Logger.php:94
setDisplay($display)
Set whether the logging should be displayed to the user.
Definition: Logger.php:108
setLevel($level)
Set the logging level.
Definition: Logger.php:80
__construct(PluginHooksService $hooks, Config $config, Context $context)
Constructor.
Definition: Logger.php:68
enable()
Restore logging and get record of log calls (after tests)
Definition: Logger.php:279
$key
Definition: summary.php:34
elgg_get_site_url($site_guid=0)
Get the URL for the current (or specified) site.
error($message)
Log message at the ERROR level.
Definition: Logger.php:159
log($message, $level=self::NOTICE)
Add a message to the log.
Definition: Logger.php:119
dump($data, $display=true)
Dump data to log or screen.
Definition: Logger.php:200
info($message)
Log message at the INFO level.
Definition: Logger.php:189
warn($message)
Log message at the WARNING level.
Definition: Logger.php:169
notice($message)
Log message at the NOTICE level.
Definition: Logger.php:179
Manages a global stack of strings for sharing information about the current execution context...
Definition: Context.php:24