Skip to content

Instantly share code, notes, and snippets.

@armetiz
Created February 17, 2012 16:35
Show Gist options
  • Save armetiz/1854268 to your computer and use it in GitHub Desktop.
Save armetiz/1854268 to your computer and use it in GitHub Desktop.
Monolog Handler for MongoDB
<?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);
}
}
@Seldaek
Copy link

Seldaek commented Feb 17, 2012

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.

@armetiz
Copy link
Author

armetiz commented Feb 17, 2012

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.

@Seldaek
Copy link

Seldaek commented Feb 17, 2012

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.

@pablco
Copy link

pablco commented Mar 22, 2012

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 ++

@Seldaek
Copy link

Seldaek commented Mar 22, 2012

See Seldaek/monolog#59 - I just need to find some time to take care of those few Monolog issues :/

@pablco
Copy link

pablco commented Mar 22, 2012

tnx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment