Skip to content

Commit

Permalink
Logger refactor: add Iterator support for extra
Browse files Browse the repository at this point in the history
Update signature of Logger:log() method to accept Traversable as extra information. All iterators are converted to an array (like an instance of ArrayObject). ZF2 is oriented SOLID paradigm: containers are often an object.
  • Loading branch information
b-durand committed Dec 12, 2011
1 parent f012a38 commit ad04efe
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 42 deletions.
32 changes: 16 additions & 16 deletions library/Zend/Log/Loggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,57 +33,57 @@ interface Loggable
{
/**
* @param string $message
* @param array|null $extra
* @param array|Traversable $extra
* @return Loggabble
*/
public function emerg($message, array $extra = null);
public function emerg($message, $extra = array());

/**
* @param string $message
* @param array|null $extra
* @param array|Traversable $extra
* @return Loggabble
*/
public function alert($message, array $extra = null);
public function alert($message, $extra = array());

/**
* @param string $message
* @param array|null $extra
* @param array|Traversable $extra
* @return Loggabble
*/
public function crit($message, array $extra = null);
public function crit($message, $extra = array());

/**
* @param string $message
* @param array|null $extra
* @param array|Traversable $extra
* @return Loggabble
*/
public function err($message, array $extra = null);
public function err($message, $extra = array());

/**
* @param string $message
* @param array|null $extra
* @param array|Traversable $extra
* @return Loggabble
*/
public function warn($message, array $extra = null);
public function warn($message, $extra = array());

/**
* @param string $message
* @param array|null $extra
* @param array|Traversable $extra
* @return Loggabble
*/
public function notice($message, array $extra = null);
public function notice($message, $extra = array());

/**
* @param string $message
* @param array|null $extra
* @param array|Traversable $extra
* @return Loggabble
*/
public function info($message, array $extra = null);
public function info($message, $extra = array());

/**
* @param string $message
* @param array|null $extra
* @param array|Traversable $extra
* @return Loggabble
*/
public function debug($message, array $extra = null);
public function debug($message, $extra = array());
}
49 changes: 30 additions & 19 deletions library/Zend/Log/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@

use DateTime,
SplStack,
Traversable,
Zend\Loader\Broker,
Zend\Loader\Pluggable;
Zend\Loader\Pluggable,
Zend\Stdlib\IteratorToArray;

/**
* Logging messages with a stack of backends
Expand Down Expand Up @@ -215,18 +217,27 @@ public function addWriter($writer)
* @todo implement if stack of writers is empty (exception or null writer)
* @param int $priority
* @param string $message
* @param array|null $extra
* @param array|Traversable $extra
* @return Logger
* @throws Exception\InvalidArgumentException if message can't be cast in string
* @throws Exception\InvalidArgumentException if extra can't be iterate
*/
public function log($priority, $message, array $extra = null)
public function log($priority, $message, $extra = array())
{
if (is_object($message) && !method_exists($message, '__toString')) {
throw new Exception\InvalidArgumentException(
'$message must implement magic __toString() method'
);
}

if (!is_array($extra) && !$extra instanceof Traversable) {
throw new Exception\InvalidArgumentException(
'$extra must be an array or implement Traversable'
);
} elseif ($extra instanceof Traversable) {
$extra = IteratorToArray::convert($extra);
}

$date = new DateTime();
$timestamp = $date->format($this->getDateTimeFormat());

Expand All @@ -245,80 +256,80 @@ public function log($priority, $message, array $extra = null)

/**
* @param string $message
* @param array|null $extra
* @param array|Traversable $extra
* @return Logger
*/
public function emerg($message, array $extra = null)
public function emerg($message, $extra = array())
{
return $this->log(self::EMERG, $message, $extra);
}

/**
* @param string $message
* @param array|null $extra
* @param array|Traversable $extra
* @return Logger
*/
public function alert($message, array $extra = null)
public function alert($message, $extra = array())
{
return $this->log(self::ALERT, $message, $extra);
}

/**
* @param string $message
* @param array|null $extra
* @param array|Traversable $extra
* @return Logger
*/
public function crit($message, array $extra = null)
public function crit($message, $extra = array())
{
return $this->log(self::CRIT, $message, $extra);
}

/**
* @param string $message
* @param array|null $extra
* @param array|Traversable $extra
* @return Logger
*/
public function err($message, array $extra = null)
public function err($message, $extra = array())
{
return $this->log(self::ERR, $message, $extra);
}

/**
* @param string $message
* @param array|null $extra
* @param array|Traversable $extra
* @return Logger
*/
public function warn($message, array $extra = null)
public function warn($message, $extra = array())
{
return $this->log(self::WARN, $message, $extra);
}

/**
* @param string $message
* @param array|null $extra
* @param array|Traversable $extra
* @return Logger
*/
public function notice($message, array $extra = null)
public function notice($message, $extra = array())
{
return $this->log(self::NOTICE, $message, $extra);
}

/**
* @param string $message
* @param array|null $extra
* @param array|Traversable $extra
* @return Logger
*/
public function info($message, array $extra = null)
public function info($message, $extra = array())
{
return $this->log(self::INFO, $message, $extra);
}

/**
* @param string $message
* @param array|null $extra
* @param array|Traversable $extra
* @return Logger
*/
public function debug($message, array $extra = null)
public function debug($message, $extra = array())
{
return $this->log(self::DEBUG, $message, $extra);
}
Expand Down
58 changes: 51 additions & 7 deletions tests/Zend/Log/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ public function testPassingValidStringClassToSetBroker()
$this->assertInstanceOf('Zend\Loader\PluginBroker', $this->logger->getBroker());
}

public static function provideInvalidArguments()
public static function provideInvalidClasses()
{
return array(
array('stdClass'),
array(new \stdClass()),
array('stdClass'),
array(new \stdClass()),
);
}

/**
* @dataProvider provideInvalidArguments
* @dataProvider provideInvalidClasses
*/
public function testPassingInvalidArgumentToSetBrokerRaisesException($broker)
{
Expand All @@ -85,21 +85,65 @@ public function testPassingShortNameToBrokerReturnsWriterByThatName()
}

/**
* @dataProvider provideInvalidArguments
* @dataProvider provideInvalidClasses
*/
public function testPassingInvalidArgumentToAddWriterRaisesException($writer)
{
$this->setExpectedException('Zend\Log\Exception\InvalidArgumentException', 'must implement');
$this->logger->addWriter($writer);
}

public function testWriterShouldEmitMessage()
public function testLoggingEventDispatchesRecordInRelevantWriters()
{
$writer = new MockWriter;
$this->logger->addWriter($writer);
$this->logger->log(Logger::INFO, 'tottakai');

$this->assertArrayHasKey(0, $writer->events);
$this->assertEquals(count($writer->events), 1);
$this->assertContains('tottakai', $writer->events[0]);
}

public static function provideAttributes()
{
return array(
array(array()),
array(array('user' => 'foo', 'ip' => '127.0.0.1')),
array(new \ArrayObject(array('id' => 42))),
);
}

/**
* @dataProvider provideAttributes
*/
public function testLoggingCustomAttributesForUserContext($extra)
{
$writer = new MockWriter;
$this->logger->addWriter($writer);
$this->logger->log(Logger::ERR, 'tottakai', $extra);

$this->assertEquals(count($writer->events), 1);
$this->assertInternalType('array', $writer->events[0]['extra']);
$this->assertEquals(count($writer->events[0]['extra']), count($extra));
}

public static function provideInvalidArguments()
{
return array(
array(new \stdClass(), array('valid')),
array('valid', null),
array('valid', true),
array('valid', 10),
array('valid', 'invalid'),
array('valid', new \stdClass()),
);
}

/**
* @dataProvider provideInvalidArguments
*/
public function testPassingInvalidArgumentToLogRaisesException($message, $extra)
{
$this->setExpectedException('Zend\Log\Exception\InvalidArgumentException');
$this->logger->log(Logger::ERR, $message, $extra);
}
}

0 comments on commit ad04efe

Please sign in to comment.