1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58:
<?php
namespace SAREhub\Commons\Logger;
use Monolog\Formatter\JsonFormatter;
class DefaultJsonLogFormatter extends JsonFormatter
{
const DATE_TIME_FORMAT = \DateTime::ATOM;
public function __construct()
{
parent::__construct();
$this->includeStacktraces(true);
}
public function format(array $record)
{
$record["datetime"] = $this->normalizeDateTime($record["datetime"]);
foreach ($record["context"] as &$value) {
if (is_object($value)) {
$value = $this->normalizeObject($value);
}
}
return parent::format($record);
}
public function normalizeException($e)
{
return parent::normalizeException($e);
}
public function normalizeDateTime(\DateTime $dateTime): string
{
return $dateTime->format(\DateTime::ATOM);
}
public function normalizeObject($value)
{
if ($value instanceof \JsonSerializable) {
return $value->jsonSerialize();
}
if ($value instanceof \Throwable) {
return $value;
}
if (method_exists($value, '__toString')) {
return (string)$value;
}
return "object of class: " . get_class($value) . " can't be serialized to json";
}
}