Elgg  Version master
ElggLogFormatter.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Logger;
4 
8 
12 class ElggLogFormatter extends LineFormatter {
13 
17  public function format(LogRecord $record): string {
18  $context = $record->context;
19  $exception = elgg_extract('exception', $context);
20 
21  if (!$exception instanceof \Throwable) {
22  return parent::format($record);
23  }
24 
25  $dt = new \DateTimeImmutable();
26 
27  $eol = PHP_EOL;
28  $message = "Exception at time {$dt->getTimestamp()}:{$eol}{$exception->getMessage()}{$eol}";
29  $record_message = preg_replace('~\R~u', $eol, $message);
30 
31  if ($exception instanceof DatabaseException) {
32  $context['sql'] = $exception->getQuery();
33  $context['params'] = $exception->getParameters();
34  }
35 
36  unset($context['exception']);
37 
38  return parent::format(new LogRecord($dt, $record->channel, $record->level, $record_message, $context, $record->extra, $record->formatted));
39  }
40 }
$context
Definition: add.php:8
Custom log formatter.
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
$dt
Definition: time.php:70
A generic parent class for database exceptions.
format(LogRecord $record)
{}
$exception
Definition: error.php:15