Source of file AbstractWriter.php
Size: 10,192 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Log/Writer/AbstractWriter.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Log\Writer; use Traversable; use Zend\Log\Exception; use Zend\Log\Filter; use Zend\Log\Formatter; use Zend\Stdlib\ErrorHandler; abstract class AbstractWriter implements WriterInterface { /** * Filter plugins * * @var FilterPluginManager */ protected $filterPlugins; /** * Formatter plugins * * @var FormatterPluginManager */ protected $formatterPlugins; /** * Filter chain * * @var Filter\FilterInterface[] */ protected $filters = array(); /** * Formats the log message before writing * * @var Formatter\FormatterInterface */ protected $formatter; /** * Use Zend\Stdlib\ErrorHandler to report errors during calls to write * * @var bool */ protected $convertWriteErrorsToExceptions = true; /** * Error level passed to Zend\Stdlib\ErrorHandler::start for errors reported during calls to write * * @var bool */ protected $errorsToExceptionsConversionLevel = E_WARNING; /** * Constructor * * Set options for a writer. Accepted options are: * - filters: array of filters to add to this filter * - formatter: formatter for this writer * * @param array|Traversable $options * @return Logger * @throws Exception\InvalidArgumentException */ public function __construct($options = null) { if ($options instanceof Traversable) { $options = iterator_to_array($options); } if (is_array($options)) { if (isset($options['filters'])) { $filters = $options['filters']; if (is_string($filters) || $filters instanceof Filter\FilterInterface) { $this->addFilter($filters); } elseif (is_array($filters)) { foreach ($filters as $filter) { if (is_string($filter) || $filter instanceof Filter\FilterInterface) { $this->addFilter($filter); } elseif (is_array($filter)) { if (!isset($filter['name'])) { throw new Exception\InvalidArgumentException('Options must contain a name for the filter'); } $filterOptions = (isset($filter['options'])) ? $filter['options'] : null; $this->addFilter($filter['name'], $filterOptions); } } } } if (isset($options['formatter'])) { $formatter = $options['formatter']; if (is_string($formatter) || $formatter instanceof Formatter\FormatterInterface) { $this->setFormatter($formatter); } elseif (is_array($formatter)) { if (!isset($formatter['name'])) { throw new Exception\InvalidArgumentException('Options must contain a name for the formatter'); } $formatterOptions = (isset($formatter['options'])) ? $formatter['options'] : null; $this->setFormatter($formatter['name'], $formatterOptions); } } } } /** * Add a filter specific to this writer. * * @param int|string|Filter\FilterInterface $filter * @param array|null $options * @return AbstractWriter * @throws Exception\InvalidArgumentException */ public function addFilter($filter, array $options = null) { if (is_int($filter)) { $filter = new Filter\Priority($filter); } if (is_string($filter)) { $filter = $this->filterPlugin($filter, $options); } if (!$filter instanceof Filter\FilterInterface) { throw new Exception\InvalidArgumentException(sprintf( 'Filter must implement %s\Filter\FilterInterface; received "%s"', __NAMESPACE__, is_object($filter) ? get_class($filter) : gettype($filter) )); } $this->filters[] = $filter; return $this; } /** * Get filter plugin manager * * @return FilterPluginManager */ public function getFilterPluginManager() { if (null === $this->filterPlugins) { $this->setFilterPluginManager(new FilterPluginManager()); } return $this->filterPlugins; } /** * Set filter plugin manager * * @param string|FilterPluginManager $plugins * @return self * @throws Exception\InvalidArgumentException */ public function setFilterPluginManager($plugins) { if (is_string($plugins)) { $plugins = new $plugins; } if (!$plugins instanceof FilterPluginManager) { throw new Exception\InvalidArgumentException(sprintf( 'Writer plugin manager must extend %s\FilterPluginManager; received %s', __NAMESPACE__, is_object($plugins) ? get_class($plugins) : gettype($plugins) )); } $this->filterPlugins = $plugins; return $this; } /** * Get filter instance * * @param string $name * @param array|null $options * @return Filter\FilterInterface */ public function filterPlugin($name, array $options = null) { return $this->getFilterPluginManager()->get($name, $options); } /** * Get formatter plugin manager * * @return FormatterPluginManager */ public function getFormatterPluginManager() { if (null === $this->formatterPlugins) { $this->setFormatterPluginManager(new FormatterPluginManager()); } return $this->formatterPlugins; } /** * Set formatter plugin manager * * @param string|FormatterPluginManager $plugins * @return self * @throws Exception\InvalidArgumentException */ public function setFormatterPluginManager($plugins) { if (is_string($plugins)) { $plugins = new $plugins; } if (!$plugins instanceof FormatterPluginManager) { throw new Exception\InvalidArgumentException(sprintf( 'Writer plugin manager must extend %s\FormatterPluginManager; received %s', __NAMESPACE__, is_object($plugins) ? get_class($plugins) : gettype($plugins) )); } $this->formatterPlugins = $plugins; return $this; } /** * Get formatter instance * * @param string $name * @param array|null $options * @return Formatter\FormatterInterface */ public function formatterPlugin($name, array $options = null) { return $this->getFormatterPluginManager()->get($name, $options); } /** * Log a message to this writer. * * @param array $event log data event * @return void */ public function write(array $event) { foreach ($this->filters as $filter) { if (!$filter->filter($event)) { return; } } $errorHandlerStarted = false; if ($this->convertWriteErrorsToExceptions && !ErrorHandler::started()) { ErrorHandler::start($this->errorsToExceptionsConversionLevel); $errorHandlerStarted = true; } try { $this->doWrite($event); } catch (\Exception $e) { if ($errorHandlerStarted) { ErrorHandler::stop(); $errorHandlerStarted = false; } throw $e; } if ($errorHandlerStarted) { $error = ErrorHandler::stop(); $errorHandlerStarted = false; if ($error) { throw new Exception\RuntimeException("Unable to write", 0, $error); } } } /** * Set a new formatter for this writer * * @param string|Formatter\FormatterInterface $formatter * @param array|null $options * @return self * @throws Exception\InvalidArgumentException */ public function setFormatter($formatter, array $options = null) { if (is_string($formatter)) { $formatter = $this->formatterPlugin($formatter, $options); } if (!$formatter instanceof Formatter\FormatterInterface) { throw new Exception\InvalidArgumentException(sprintf( 'Formatter must implement %s\Formatter\FormatterInterface; received "%s"', __NAMESPACE__, is_object($formatter) ? get_class($formatter) : gettype($formatter) )); } $this->formatter = $formatter; return $this; } /** * Get formatter * * @return Formatter\FormatterInterface */ protected function getFormatter() { return $this->formatter; } /** * Check if the writer has a formatter * * @return bool */ protected function hasFormatter() { return $this->formatter instanceof Formatter\FormatterInterface; } /** * Set convert write errors to exception flag * * @param bool $convertErrors */ public function setConvertWriteErrorsToExceptions($convertErrors) { $this->convertWriteErrorsToExceptions = $convertErrors; } /** * Perform shutdown activities such as closing open resources * * @return void */ public function shutdown() {} /** * Write a message to the log * * @param array $event log data event * @return void */ abstract protected function doWrite(array $event); } |