Elgg  Version 3.0
Logger.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
5 use Elgg\Cli\Application as CliApplication;
19 
25 class Logger extends \Monolog\Logger {
26 
27  const CHANNEL = 'ELGG';
28 
29  const OFF = false;
30 
35  protected static $elgg_levels = [
36  0 => false,
37  100 => LogLevel::DEBUG,
38  200 => LogLevel::INFO,
39  250 => LogLevel::NOTICE,
40  300 => LogLevel::WARNING,
41  400 => LogLevel::ERROR,
42  500 => LogLevel::CRITICAL,
43  550 => LogLevel::ALERT,
44  600 => LogLevel::EMERGENCY,
45  ];
46 
51  protected static $legacy_levels = [
52  'OFF' => false,
53  'INFO' => LogLevel::INFO,
54  'NOTICE' => LogLevel::NOTICE,
55  'WARNING' => LogLevel::WARNING,
56  'ERROR' => LogLevel::ERROR,
57  ];
58 
62  protected $level;
63 
67  protected $hooks;
68 
72  private $disabled_stack;
73 
82  public static function factory(InputInterface $input = null, OutputInterface $output = null) {
83  $logger = new static(self::CHANNEL);
84 
85  if (\Elgg\Application::isCli()) {
86  if (is_null($input) || is_null($output)) {
89 
90  $app = new CliApplication();
91  $app->setup($input, $output);
92  }
93 
94  $handler = new ErrorHandler(
95  $output,
96  \Elgg\Application::getStdErr(),
97  true
98  );
99 
100  $formatter = new ErrorFormatter();
101  $formatter->allowInlineLineBreaks();
102  $formatter->ignoreEmptyContextAndExtra();
103 
104  $handler->setFormatter($formatter);
105 
106  $handler->pushProcessor(new BacktraceProcessor(self::ERROR));
107  } else {
108  $handler = new ErrorLogHandler();
109 
110  $handler->pushProcessor(new WebProcessor());
111 
112  $formatter = new ElggLogFormatter();
113  $formatter->allowInlineLineBreaks();
114  $formatter->ignoreEmptyContextAndExtra();
115 
116  $handler->setFormatter($formatter);
117 
118  $handler->pushProcessor(new MemoryUsageProcessor());
119  $handler->pushProcessor(new MemoryPeakUsageProcessor());
120  $handler->pushProcessor(new ProcessIdProcessor());
121  $handler->pushProcessor(new BacktraceProcessor(self::WARNING));
122  }
123 
124  $handler->pushProcessor(new PsrLogMessageProcessor());
125 
126  $logger->pushHandler($handler);
127 
128  $logger->setLevel();
129 
130  return $logger;
131  }
132 
140  protected function normalizeLevel($level = null) {
141  if (!$level) {
142  return false;
143  }
144 
145  if (array_key_exists($level, self::$legacy_levels)) {
146  $level = self::$legacy_levels[$level];
147  }
148 
149  if (array_key_exists($level, self::$elgg_levels)) {
150  $level = self::$elgg_levels[$level];
151  }
152 
153  if (!in_array($level, self::$elgg_levels)) {
154  $level = false;
155  }
156 
157  return $level;
158  }
159 
168  public function setLevel($level = null) {
169  if (!isset($level)) {
170  $php_error_level = error_reporting();
171 
172  $level = false;
173 
174  if (($php_error_level & E_NOTICE) == E_NOTICE) {
175  $level = LogLevel::NOTICE;
176  } else if (($php_error_level & E_WARNING) == E_WARNING) {
177  $level = LogLevel::WARNING;
178  } else if (($php_error_level & E_ERROR) == E_ERROR) {
179  $level = LogLevel::ERROR;
180  }
181  }
182 
183  $this->level = $this->normalizeLevel($level);
184  }
185 
194  public function getLevel($severity = true) {
195  if ($severity) {
196  return array_search($this->level, self::$elgg_levels);
197  }
198 
199  return $this->level;
200  }
201 
209  public function isLoggable($level) {
210  $level = $this->normalizeLevel($level);
211 
212  $severity = array_search($level, self::$elgg_levels);
213  if (!$this->getLevel() || $severity < $this->getLevel()) {
214  return false;
215  }
216 
217  return true;
218  }
219 
223  public function log($level, $message, array $context = []) {
224 
225  $level = $this->normalizeLevel($level);
226 
227  if (!empty($this->disabled_stack)) {
228  // capture to top of stack
229  end($this->disabled_stack);
230  $key = key($this->disabled_stack);
231  $this->disabled_stack[$key][] = [
232  'message' => $message,
233  'level' => $level,
234  ];
235  }
236 
237  if (!$this->isLoggable($level)) {
238  return false;
239  }
240 
241  // when capturing, still use consistent return value
242  if (!empty($this->disabled_stack)) {
243  return true;
244  }
245 
246  if ($this->hooks) {
247  $params = [
248  'level' => $level,
249  'msg' => $message,
250  'context' => $context,
251  ];
252 
253  if (!$this->hooks->triggerDeprecated('debug', 'log', $params, true)) {
254  return false;
255  }
256  }
257 
258  return parent::log($level, $message, $context);
259  }
260 
264  public function emergency($message, array $context = []) {
265  return $this->log(LogLevel::EMERGENCY, $message, $context);
266  }
267 
271  public function alert($message, array $context = []) {
272  return $this->log(LogLevel::ALERT, $message, $context);
273  }
274 
278  public function critical($message, array $context = []) {
279  return $this->log(LogLevel::CRITICAL, $message, $context);
280  }
281 
285  public function error($message, array $context = []) {
286  return $this->log(LogLevel::ERROR, $message, $context);
287  }
288 
292  public function warning($message, array $context = []) {
293  return $this->log(LogLevel::WARNING, $message, $context);
294  }
295 
299  public function notice($message, array $context = []) {
300  return $this->log(LogLevel::NOTICE, $message, $context);
301  }
302 
306  public function info($message, array $context = []) {
307  return $this->log(LogLevel::INFO, $message, $context);
308  }
309 
313  public function debug($message, array $context = []) {
314  return $this->log(LogLevel::DEBUG, $message, $context);
315  }
316 
326  public function warn($message, array $context = []) {
327  return $this->warning($message, $context);
328  }
329 
337  public function dump($data) {
338  return $this->log(LogLevel::ERROR, $data);
339  }
340 
353  public function disable() {
354  $this->disabled_stack[] = [];
355  }
356 
364  public function enable() {
365  return array_pop($this->disabled_stack);
366  }
367 
376  public function setHooks(PluginHooksService $hooks) {
377  $this->hooks = $hooks;
378  }
379 }
disable()
Temporarily disable logging and capture logs (before tests)
Definition: Logger.php:353
$context
Definition: add.php:8
Custom log formatter.
log($msg, $level=LogLevel::NOTICE)
Log a message.
Definition: Seeding.php:762
$params
Saves global plugin settings.
Definition: save.php:13
info($message, array $context=[])
{}
Definition: Logger.php:306
getLevel($severity=true)
Get the current logging level severity.
Definition: Logger.php:194
emergency($message, array $context=[])
{}
Definition: Logger.php:264
static getStdOut()
Load console output interface.
static factory(InputInterface $input=null, OutputInterface $output=null)
Build a new logger.
Definition: Logger.php:82
setHooks(PluginHooksService $hooks)
Reset the hooks service for this instance (testing)
Definition: Logger.php:376
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:57
alert($message, array $context=[])
{}
Definition: Logger.php:271
Wrapper for console application.
Definition: Application.php:11
debug($message, array $context=[])
{}
Definition: Logger.php:313
Inject backtrace stack into the record.
setLevel($level=null)
Set the logging level.
Definition: Logger.php:168
catch(LoginException $e) if($request->isXhr()) $output
Definition: login.php:56
warning($message, array $context=[])
{}
Definition: Logger.php:292
log($level, $message, array $context=[])
{}
Definition: Logger.php:223
Logger.
Definition: Logger.php:25
Configuration exception.
enable()
Restore logging and get record of log calls (after tests)
Definition: Logger.php:364
error($message, array $context=[])
{}
Definition: Logger.php:285
Handle system and PHP errors.
critical($message, array $context=[])
{}
Definition: Logger.php:278
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:55
warn($message, array $context=[])
Log message at the WARNING level.
Definition: Logger.php:326
static getStdIn()
Load console input interface.
$input
Form field view.
Definition: field.php:13
elgg ajax ERROR
Definition: ajax.js:33
dump($data)
Dump data to log.
Definition: Logger.php:337
$handler
Definition: add.php:7
isLoggable($level)
Check if a level is loggable under current logging level.
Definition: Logger.php:209
Format errors for console output.
normalizeLevel($level=null)
Normalizes legacy string or numeric representation of the level to LogLevel strings.
Definition: Logger.php:140
notice($message, array $context=[])
{}
Definition: Logger.php:299