Feature/logger factory #2725

Closed
wants to merge 12 commits into
from
+516 −25
Split
@@ -32,10 +32,18 @@ class Base implements FormatterInterface
* Class constructor
*
* @see http://php.net/manual/en/function.date.php
- * @param null|string $dateTimeFormat Format for DateTime objects
+ * @param null|string|array|Traversable $dateTimeFormat Format for DateTime objects
*/
public function __construct($dateTimeFormat = null)
{
+ if ($dateTimeFormat instanceof Traversable) {
+ $dateTimeFormat = iterator_to_array($dateTimeFormat);
+ }
+
+ if (is_array($dateTimeFormat)) {
+ $dateTimeFormat = isset($dateTimeFormat['dateTimeFormat'])? $dateTimeFormat['dateTimeFormat'] : null;
+ }
+
if (null !== $dateTimeFormat) {
$this->dateTimeFormat = $dateTimeFormat;
}
@@ -11,6 +11,7 @@
namespace Zend\Log\Formatter;
use DateTime;
+use Traversable;
/**
* @category Zend
@@ -35,6 +36,14 @@ class Db implements FormatterInterface
*/
public function __construct($dateTimeFormat = null)
{
+ if ($dateTimeFormat instanceof Traversable) {
+ $dateTimeFormat = iterator_to_array($dateTimeFormat);
+ }
+
+ if (is_array($dateTimeFormat)) {
+ $dateTimeFormat = isset($dateTimeFormat['dateTimeFormat'])? $dateTimeFormat['dateTimeFormat'] : null;
+ }
+
if (null !== $dateTimeFormat) {
$this->setDateTimeFormat($dateTimeFormat);
}
@@ -10,6 +10,7 @@
namespace Zend\Log\Formatter;
+use Traversable;
use DateTime;
use Zend\Log\Exception;
@@ -39,6 +40,15 @@ class Simple extends Base
*/
public function __construct($format = null, $dateTimeFormat = null)
{
+ if ($format instanceof Traversable) {
+ $format = iterator_to_array($format);
+ }
+
+ if (is_array($format)) {
+ $dateTimeFormat = isset($format['dateTimeFormat'])? $format['dateTimeFormat'] : null;
+ $format = isset($format['format'])? $format['format'] : null;
+ }
+
if (isset($format) && !is_string($format)) {
throw new Exception\InvalidArgumentException('Format must be a string');
}
@@ -83,12 +83,48 @@ class Logger implements LoggerInterface
/**
* Constructor
*
- * @todo support configuration (writers, dateTimeFormat, and writer plugin manager)
+ * Set options for an logger. Accepted options are:
+ * - writers: array of writers to add to this logger
+ * - exceptionhandler: if true register this logger as exceptionhandler
+ * - errorhandler: if true register this logger as errorhandler
+ *
+ * @param array|\Traversable $options
* @return Logger
+ * @throws Exception\InvalidArgumentException
*/
- public function __construct()
+ public function __construct(array $options = null)
{
$this->writers = new SplPriorityQueue();
+
+ if ($options instanceof Traversable) {
+ $options = ArrayUtils::iteratorToArray($options);
+ }
+
+ if (is_array($options)) {
+
+ if(isset($options['writers']) && is_array($options['writers'])) {
+ foreach($options['writers'] as $writer) {
+
+ if(!isset($writer['name'])) {
+ throw new Exception\InvalidArgumentException('Options must contain a name for the writer');
+ }
+
+ $priority = (isset($writer['priority'])) ? $writer['priority'] : null;
+ $writerOptions = (isset($writer['options'])) ? $writer['options'] : null;
+
+ $this->addWriter($writer['name'], $priority, $writerOptions);
+ }
+ }
+
+ if(isset($options['exceptionhandler']) && $options['exceptionhandler'] === true) {
+ self::registerExceptionHandler($this);
+ }
+
+ if(isset($options['errorhandler']) && $options['errorhandler'] === true) {
+ self::registerErrorHandler($this);
+ }
+
+ }
}
/**
@@ -0,0 +1,31 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Log
+ */
+
+namespace Zend\Log;
+
+use Zend\ServiceManager\FactoryInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+/**
+ * Logger.
+ *
+ * @category Zend
+ * @package Zend_Log
+ */
+class LoggerServiceFactory implements FactoryInterface
+{
+ public function createService(ServiceLocatorInterface $serviceLocator)
+ {
+ // Configure the logger
+ $config = $serviceLocator->get('Config');
+ $logConfig = isset($config['log']) ? $config['log'] : array();
+ $logger = new Logger($logConfig);
+ }
+}
@@ -10,9 +10,10 @@
namespace Zend\Log\Writer;
+use Traversable;
use Zend\Log\Exception;
use Zend\Log\Filter;
-use Zend\Log\Formatter\FormatterInterface as Formatter;
+use Zend\Log\Formatter;
use Zend\Stdlib\ErrorHandler;
/**
@@ -30,6 +31,13 @@
protected $filterPlugins;
/**
+ * Formatter plugins
+ *
+ * @var FormatterPluginManager
+ */
+ protected $formatterPlugins;
+
+ /**
* Filter chain
*
* @var Filter\FilterInterface[]
@@ -58,6 +66,50 @@
protected $errorsToExceptionsConversionLevel = E_WARNING;
/**
+ * Constructor
+ *
+ * Set options for an 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']) && is_array($options['filters'])) {
+ foreach($options['filters'] as $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
@@ -77,7 +129,8 @@ public function addFilter($filter, array $options = null)
if (!$filter instanceof Filter\FilterInterface) {
throw new Exception\InvalidArgumentException(sprintf(
- 'Writer must implement Zend\Log\Filter\FilterInterface; received "%s"',
+ 'Writer must implement %s\Filter\FilterInterface; received "%s"',
+ __NAMESPACE__,
is_object($filter) ? get_class($filter) : gettype($filter)
));
}
@@ -136,6 +189,56 @@ public function filterPlugin($name, array $options = null)
}
/**
+ * 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
@@ -178,11 +281,24 @@ public function write(array $event)
/**
* Set a new formatter for this writer
*
- * @param Formatter $formatter
+ * @param string|Formatter\FormatterInterface $formatter
* @return self
+ * @throws Exception\InvalidArgumentException
*/
- public function setFormatter(Formatter $formatter)
+ 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;
}
@@ -44,7 +44,7 @@ class FilterPluginManager extends AbstractPluginManager
/**
* Validate the plugin
*
- * Checks that the writer loaded is an instance of Filter\FilterInterface.
+ * Checks that the filter loaded is an instance of Filter\FilterInterface.
*
* @param mixed $plugin
* @return void
Oops, something went wrong.