Elgg  Version master
ElggLogFormatter.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Logger;
4 
7 
11 class ElggLogFormatter extends LineFormatter {
12 
16  public function format(array $record): string {
17 
18  $context = elgg_extract('context', $record, []);
19  $exception = elgg_extract('exception', $context);
20 
21  if ($exception instanceof \Throwable) {
22  $timestamp = isset($exception->timestamp) ? (int) $exception->timestamp : time();
23 
24  $dt = new \DateTime();
25  $dt->setTimestamp($timestamp);
26  $record['datetime'] = $dt;
27 
28  $eol = PHP_EOL;
29  $message = "Exception at time {$timestamp}:{$eol}{$exception->getMessage()}{$eol}";
30  $record['message'] = preg_replace('~\R~u', $eol, $message);
31 
32  if ($exception instanceof DatabaseException) {
33  $record['context']['sql'] = $exception->getQuery();
34  $record['context']['params'] = $exception->getParameters();
35  }
36 
37  unset($record['context']['exception']);
38  }
39 
40  return parent::format($record);
41  }
42 }
$context
Definition: add.php:8
Custom log formatter.
$timestamp
Definition: date.php:34
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.
$exception
Definition: error.php:15