Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added ability to pass configuration options to Logger constructor

  • Loading branch information...
commit 00ad826bc5d565485685bd04bec5de8e38afa9ea 1 parent ed981ec
Stefan Kleff authored
Showing with 72 additions and 2 deletions.
  1. +38 −2 library/Zend/Log/Logger.php
  2. +34 −0 tests/ZendTest/Log/LoggerTest.php
40 library/Zend/Log/Logger.php
View
@@ -82,13 +82,49 @@ class Logger implements LoggerInterface
/**
* Constructor
+ *
+ * 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
*
- * @todo support configuration (writers, dateTimeFormat, and writer plugin manager)
+ * @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);
+ }
+
+ }
}
/**
34 tests/ZendTest/Log/LoggerTest.php
View
@@ -259,4 +259,38 @@ public function testRegisterErrorHandler()
Logger::unregisterErrorHandler();
$this->assertEquals($writer->events[0]['message'], 'Undefined variable: test');
}
+
+ public function testOptionsWithMock()
+ {
+ $options = array('writers' => array(
+ 'first_writer' => array(
+ 'name' => 'mock',
+ )
+ ));
+ $logger = new Logger($options);
+
+ $writers = $logger->getWriters()->toArray();
+ $this->assertCount(1, $writers);
+ $this->assertInstanceOf('Zend\Log\Writer\Mock', $writers[0]);
+ }
+
+ public function testOptionsWithWriterOptions()
+ {
+ $options = array('writers' => array(
+ 'first_writer' => array(
+ 'name' => 'stream',
+ 'options' => array(
+ 'stream' => 'php://output',
+ 'log_separator' => 'foo'
+ ),
+ )
+ ));
+ $logger = new Logger($options);
+
+ $writers = $logger->getWriters()->toArray();
+ $this->assertCount(1, $writers);
+ $this->assertInstanceOf('Zend\Log\Writer\Stream', $writers[0]);
+ $this->assertEquals('foo', $writers[0]->getLogSeparator());
+
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.