Created
February 17, 2012 16:35
-
-
Save armetiz/1854268 to your computer and use it in GitHub Desktop.
Monolog Handler for MongoDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* | |
* Thomas Tourlourat <thomas@tourlourat.com> | |
*/ | |
namespace Acme\TestBundle\Monolog; | |
use Monolog\Logger; | |
use Monolog\Handler\AbstractProcessingHandler; | |
use Monolog\Formatter\JsonFormatter; | |
/** | |
* Base class for all mail handlers | |
* | |
* @author Gyula Sallai | |
*/ | |
class MongoDBHandler extends AbstractProcessingHandler | |
{ | |
private $mongo; | |
private $mongoDB; | |
private $mongoCollection; | |
public function __construct(\Mongo $mongo, $database, $collection, $level = Logger::DEBUG, $bubble = true) | |
{ | |
$this->mongo = $mongo; | |
$this->mongoDB = new \MongoDB($this->mongo, $database); | |
$this->mongoCollection = $this->mongoDB->selectCollection ($collection); | |
parent::__construct($level, $bubble); | |
} | |
public function close() | |
{ | |
$this->mongo->close(); | |
} | |
protected function write(array $record) | |
{ | |
unset($record["formatted"]); | |
$this->mongoCollection->save ($record); | |
} | |
} |
You totally right !
Because your the author of Monolog, can you integrate it ? Because I can do this only next week.. I'm little busy today.
If you don't mind I think it would be better if you sent a pull request. No hurry.. next week is fine. I'm quite busy myself.
When will be this handler integrated in the official monolog repo? Mongo is non-blocking, schemaless and you can do very elaborated queries over it, totally ++
See Seldaek/monolog#59 - I just need to find some time to take care of those few Monolog issues :/
tnx!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 28 could be
$mongo->selectDB($database)
right? It reads a bit easier I'd say.As for the close(), I would not implement it because the Mongo instance is passed in, so it shouldn't be the handler's responsibility to close it.