From 1359f72b08f197b2f80f3d38ebe63aa5770f53a0 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 22 Apr 2012 12:36:48 +0200 Subject: [PATCH] Fix up mongo db handler and add tests --- CHANGELOG.mdown | 1 + README.mdown | 1 + src/Monolog/Handler/MongoDBHandler.php | 25 ++++++--- tests/Monolog/Handler/MongoDBHandlerTest.php | 54 ++++++++++++++++++++ tests/Monolog/TestCase.php | 4 +- 5 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 tests/Monolog/Handler/MongoDBHandlerTest.php diff --git a/CHANGELOG.mdown b/CHANGELOG.mdown index 94c07915..2f1e5466 100644 --- a/CHANGELOG.mdown +++ b/CHANGELOG.mdown @@ -5,6 +5,7 @@ * Added Monolog\Logger::isHandling() to check if a handler will handle the given log level * Added ChromePHPHandler + * Added MongoDBHandler * Added NormalizerFormatter * Added possibility to show microseconds in logs * Added `server` and `referer` to WebProcessor output diff --git a/README.mdown b/README.mdown index 843c444d..c78716c3 100644 --- a/README.mdown +++ b/README.mdown @@ -42,6 +42,7 @@ Handlers - _RotatingFileHandler_: Logs records to a file and creates one logfile per day. It will also delete files older than $maxFiles. You should use [logrotate](http://linuxcommand.org/man_pages/logrotate8.html) for high profile setups though, this is just meant as a quick and dirty solution. - _FirePHPHandler_: Handler for [FirePHP](http://www.firephp.org/), providing inline `console` messages within [FireBug](http://getfirebug.com/). - _ChromePHPHandler_: Handler for [ChromePHP](http://www.chromephp.com/), providing inline `console` messages within Chrome. +- _MongoDBHandler_: Handler to write records in MongoDB via a [Mongo](http://pecl.php.net/package/mongo) extension connection. - _NativeMailHandler_: Sends emails using PHP's mail() function. - _SwiftMailerHandler_: Sends emails using a SwiftMailer instance. - _SyslogHandler_: Logs records to the syslog. diff --git a/src/Monolog/Handler/MongoDBHandler.php b/src/Monolog/Handler/MongoDBHandler.php index cbcc7430..210bb19f 100644 --- a/src/Monolog/Handler/MongoDBHandler.php +++ b/src/Monolog/Handler/MongoDBHandler.php @@ -12,6 +12,7 @@ namespace Monolog\Handler; use Monolog\Logger; +use Monolog\Formatter\NormalizerFormatter; /** * Logs to a MongoDB database. @@ -24,19 +25,27 @@ use Monolog\Logger; * * @author Thomas Tourlourat */ -class MongoDBHandler extends AbstractProcessingHandler { - +class MongoDBHandler extends AbstractProcessingHandler +{ private $mongoCollection; - public function __construct(\Mongo $mongo, $database, $collection, $level = Logger::DEBUG, $bubble = true) { - $this->mongoCollection = $this->mongo->selectCollection($database, $collection); + public function __construct(\Mongo $mongo, $database, $collection, $level = Logger::DEBUG, $bubble = true) + { + $this->mongoCollection = $mongo->selectCollection($database, $collection); parent::__construct($level, $bubble); } - protected function write(array $record) { - unset($record["formatted"]); - $this->mongoCollection->save($record); + protected function write(array $record) + { + $this->mongoCollection->save($record["formatted"]); } -} \ No newline at end of file + /** + * {@inheritDoc} + */ + protected function getDefaultFormatter() + { + return new NormalizerFormatter(); + } +} diff --git a/tests/Monolog/Handler/MongoDBHandlerTest.php b/tests/Monolog/Handler/MongoDBHandlerTest.php new file mode 100644 index 00000000..2326c985 --- /dev/null +++ b/tests/Monolog/Handler/MongoDBHandlerTest.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Handler; + +use Monolog\TestCase; +use Monolog\Logger; + +class MongoDBHandlerTest extends TestCase +{ + public function testHandle() + { + $mongo = $this->getMock('Mongo', array('selectCollection')); + $collection = $this->getMock('stdClass', array('save')); + + $mongo->expects($this->once()) + ->method('selectCollection') + ->with('DB', 'Collection') + ->will($this->returnValue($collection)); + + $record = $this->getRecord(Logger::WARNING, 'test', array('data' => new \stdClass, 'foo' => 34)); + + $expected = array( + 'message' => 'test', + 'context' => array('data' => '[object] (stdClass: {})', 'foo' => 34), + 'level' => Logger::WARNING, + 'level_name' => 'WARNING', + 'channel' => 'test', + 'datetime' => $record['datetime']->format('Y-m-d H:i:s'), + 'extra' => array(), + ); + + $collection->expects($this->once()) + ->method('save') + ->with($expected); + + $handler = new MongoDBHandler($mongo, 'DB', 'Collection'); + $handler->handle($record); + } +} + +if (!class_exists('Mongo')) { + class Mongo { + public function selectCollection() {} + } +} diff --git a/tests/Monolog/TestCase.php b/tests/Monolog/TestCase.php index ecf655c2..8624d071 100644 --- a/tests/Monolog/TestCase.php +++ b/tests/Monolog/TestCase.php @@ -16,11 +16,11 @@ class TestCase extends \PHPUnit_Framework_TestCase /** * @return array Record */ - protected function getRecord($level = Logger::WARNING, $message = 'test') + protected function getRecord($level = Logger::WARNING, $message = 'test', $context = array()) { return array( 'message' => $message, - 'context' => array(), + 'context' => $context, 'level' => $level, 'level_name' => Logger::getLevelName($level), 'channel' => 'test',