mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-14 17:14:16 +02:00
Fix up mongo db handler and add tests
This commit is contained in:
@@ -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
|
||||
|
@@ -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.
|
||||
|
@@ -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 <thomas@tourlourat.com>
|
||||
*/
|
||||
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"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getDefaultFormatter()
|
||||
{
|
||||
return new NormalizerFormatter();
|
||||
}
|
||||
}
|
54
tests/Monolog/Handler/MongoDBHandlerTest.php
Normal file
54
tests/Monolog/Handler/MongoDBHandlerTest.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Monolog package.
|
||||
*
|
||||
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* 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() {}
|
||||
}
|
||||
}
|
@@ -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',
|
||||
|
Reference in New Issue
Block a user