/** * Replace any '{item}' in the messsage with context['item'] value * * @see http://www.php-fig.org/psr/psr-3/ */ public function __invoke(LogEntryInterface $log) { $msg = $log->getMessage(); if (strpos($msg, '{') === false) { return; } // build a replacement array with braces around the context keys $replace = []; $context = $log->getContexts(); foreach ($context as $key => $val) { if (is_object($val)) { if ($val instanceof \Exception) { $xval = 'EXCEPTION: ' . $val->getMessage(); } elseif (method_exists($val, '__toString')) { $xval = (string) $val; } else { $xval = 'OBJECT: ' . get_class($val); } } elseif (is_scalar($val)) { $xval = strval($val); } else { $xval = 'TYPE: ' . strtoupper(gettype($val)); } $replace['{' . $key . '}'] = $xval; } // interpolate replacement values into the message $log->setMessage(strtr($msg, $replace)); }