contextHelper = $contextHelper; $this->logHandlers = []; } /** * @param LogHandlerInterface $handler * * @return void */ public function pushHandler(LogHandlerInterface $handler): void { $this->logHandlers[] = $handler; } /** * @param string $level * @param mixed $message * @param array $context * * @throws InvalidArgumentException * * @return void */ public function log($level, $message, array $context = []): void { if (!in_array($level, self::LOGLEVELS, true)) { throw new InvalidArgumentException(sprintf('Unrecognised loglevel "%s".', $level)); } if (!is_string($message) && !method_exists($message, '__toString')) { throw new InvalidArgumentException('Cannot convert Message to String.'); } if (count($this->logHandlers) === 0) { return; } $interpolated = $this->contextHelper->interpolateMessage($message, $context); $contextObj = $this->contextHelper->createContext($context); foreach ($this->logHandlers as $handler) { if ($handler->canHandle($level)) { $handler->addLogEntry($level, $interpolated, $contextObj); } } } }