writer = $writer; if ($writer instanceof Traversable) { $writer = ArrayUtils::iteratorToArray($writer); } if (is_array($writer)) { $filterOrPriority = $writer['priority'] ?? null; $bufferSize = $writer['bufferSize'] ?? null; $writer = $writer['writer'] ?? null; } if (null === $filterOrPriority) { $filterOrPriority = new PriorityFilter(Logger::WARN); } elseif (! $filterOrPriority instanceof FilterInterface) { $filterOrPriority = new PriorityFilter($filterOrPriority); } if (is_array($writer) && isset($writer['name'])) { $this->setWriter($writer['name'], $writer['options']); } else { $this->setWriter($writer); } $this->addFilter($filterOrPriority); $this->bufferSize = $bufferSize; } /** * Set a new writer * * @param string|WriterInterface $writer * @param array|null $options * @return self * @throws Exception\InvalidArgumentException */ public function setWriter($writer, ?array $options = null) { if (is_string($writer)) { $writer = $this->writerPlugin($writer, $options); } if (! $writer instanceof WriterInterface) { throw new Exception\InvalidArgumentException(sprintf( 'Writer must implement %s\WriterInterface; received "%s"', __NAMESPACE__, is_object($writer) ? get_class($writer) : gettype($writer) )); } $this->writer = $writer; return $this; } /** * Get writer plugin manager * * @return WriterPluginManager */ public function getWriterPluginManager() { if (null === $this->writerPlugins) { $this->setWriterPluginManager(new WriterPluginManager(new ServiceManager())); } return $this->writerPlugins; } /** * Set writer plugin manager * * @param string|WriterPluginManager $plugins * @return FingersCrossed * @throws Exception\InvalidArgumentException */ public function setWriterPluginManager($plugins) { if (is_string($plugins)) { $plugins = new $plugins(); } if (! $plugins instanceof WriterPluginManager) { throw new Exception\InvalidArgumentException(sprintf( 'Writer plugin manager must extend %s\WriterPluginManager; received %s', __NAMESPACE__, is_object($plugins) ? get_class($plugins) : gettype($plugins) )); } $this->writerPlugins = $plugins; return $this; } /** * Get writer instance * * @param string $name * @param array|null $options * @return WriterInterface */ public function writerPlugin($name, ?array $options = null) { return $this->getWriterPluginManager()->get($name, $options); } /** * Log a message to this writer. * * @param array $event log data event * @return void */ public function write(array $event) { $this->doWrite($event); } /** * Check if buffered data should be flushed * * @param array $event event data * @return bool true if buffered data should be flushed */ protected function isActivated(array $event) { foreach ($this->filters as $filter) { if (! $filter->filter($event)) { return false; } } return true; } /** * Write message to buffer or delegate event data to the wrapped writer * * @param array $event event data * @return void */ protected function doWrite(array $event) { if (! $this->buffering) { $this->writer->write($event); return; } $this->buffer[] = $event; if ($this->bufferSize > 0 && count($this->buffer) > $this->bufferSize) { array_shift($this->buffer); } if (! $this->isActivated($event)) { return; } $this->buffering = false; foreach ($this->buffer as $bufferedEvent) { $this->writer->write($bufferedEvent); } } /** * Resets the state of the handler. * Stops forwarding records to the wrapped writer */ public function reset() { $this->buffering = true; } /** * Stub in accordance to parent method signature. * Fomatters must be set on the wrapped writer. * * @param string|FormatterInterface $formatter * @param array|null $options (unused) * @return WriterInterface */ public function setFormatter($formatter, ?array $options = null) { return $this->writer; } /** * Record shutdown * * @return void */ public function shutdown() { $this->writer->shutdown(); $this->buffer = null; } }