Elgg  Version 3.0
ElggLogFormatter.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Logger;
4 
6 
10 class ElggLogFormatter extends LineFormatter {
11 
15  public function format(array $record) {
16 
17  $context = elgg_extract('context', $record, []);
18  $exception = elgg_extract('exception', $context);
19 
20  if ($exception instanceof \Throwable) {
21  $timestamp = isset($exception->timestamp) ? (int) $exception->timestamp : time();
22 
23  $dt = new \DateTime();
24  $dt->setTimestamp($timestamp);
25  $record['datetime'] = $dt;
26 
27  $eol = PHP_EOL;
28  $message = "Exception at time {$timestamp}:{$eol}{$exception}{$eol}";
29  $record['message'] = preg_replace('~\R~u', $eol, $message);
30 
31  if ($exception instanceof \DatabaseException) {
32  $record['context']['sql'] = $exception->getQuery();
33  $record['context']['params'] = $exception->getParameters();
34  }
35  }
36 
37  return parent::format($record);
38  }
39 }
$context
Definition: add.php:8
Custom log formatter.
$timestamp
Definition: date.php:33
$dt
Definition: time.php:69
$exception
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1131