Skip to content

Commit

Permalink
Merge pull request doctrine#61 from doctrine/pretty-logging-option
Browse files Browse the repository at this point in the history
made profiler configurable
  • Loading branch information
kriswallsmith committed Dec 7, 2011
2 parents 66f0ce1 + 2515346 commit e622702
Show file tree
Hide file tree
Showing 14 changed files with 445 additions and 256 deletions.
Expand Up @@ -9,101 +9,30 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\DoctrineMongoDBBundle\Logger;
namespace Symfony\Bundle\DoctrineMongoDBBundle\DataCollector;

use Doctrine\MongoDB\GridFSFile;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Logger for the Doctrine MongoDB ODM.
*
* The {@link logQuery()} method is configured as the logger callable in the
* service container.
* A data collector that formats pretty queries.
*
* @author Kris Wallsmith <kris@symfony.com>
*/
class DoctrineMongoDBLogger
class PrettyDataCollector extends StandardDataCollector
{
protected $logger;

protected $prefix;
protected $queries;

protected $processed;
protected $formattedQueries;
protected $nbRealQueries;

/**
* Constructor.
*
* @param LoggerInterface $logger The Symfony logger
* @param string $prefix A prefix for messages sent to the Symfony logger
*/
public function __construct(LoggerInterface $logger = null, $prefix = 'MongoDB query: ')
{
$this->logger = $logger;
$this->prefix = $prefix;
$this->queries = array();
$this->processed = false;
}
private $batchInsertThreshold;

/**
* Logs a query.
*
* This method is configured as the logger callable in the service
* container.
*
* @param array $query A query log array from Doctrine
*/
public function logQuery(array $query)
public function setBatchInsertThreshold($batchInsertThreshold)
{
$this->queries[] = $query;
$this->processed = false;

if (null !== $this->logger) {
$this->logger->info($this->prefix.static::bsonEncode($query));
}
$this->batchInsertThreshold = $batchInsertThreshold;
}

/**
* Returns the number of queries that have been logged.
*
* @return integer The number of queries logged
*/
public function getNbQueries()
public function collect(Request $request, Response $response, \Exception $exception = null)
{
if (!$this->processed) {
$this->processQueries();
}

return $this->nbRealQueries;
}

/**
* Returns a human-readable array of queries logged.
*
* @return array An array of queries
*/
public function getQueries()
{
if (!$this->processed) {
$this->processQueries();
}

return $this->formattedQueries;
}

/**
* Groups and formats query arrays.
*
* @param array $queries An array of query arrays
*
* @return array An array of human-readable queries
*/
protected function processQueries()
{
$this->formattedQueries = array();
$this->nbRealQueries = 0;
$this->data['queries'] = array();
$this->data['nb_queries'] = 0;

$grouped = array();
$ordered = array();
Expand Down Expand Up @@ -133,7 +62,7 @@ protected function processQueries()
foreach ($logs as $log) {
if (isset($log['db']) && $db != $log['db']) {
// for readability
$this->formattedQueries[$i++] = 'use '.$log['db'].';';
$this->data['queries'][$i++] = 'use '.$log['db'].';';
$db = $log['db'];
}

Expand All @@ -144,8 +73,8 @@ protected function processQueries()
$query = 'db'.$query;
}

$this->formattedQueries[$i++] = $query.';';
++$this->nbRealQueries;
$this->data['queries'][$i++] = $query.';';
++$this->data['nb_queries'];
}

$query = 'db.'.$log['collection'];
Expand All @@ -155,17 +84,23 @@ protected function processQueries()
if (isset($log['authenticate'])) {
$query .= '.authenticate()';
} elseif (isset($log['batchInsert'])) {
$query .= '.batchInsert(**'.$log['num'].' item(s)**)';
if (1 === $log['num']) {
$query .= '.insert('.$this->bsonEncode($log['data']).')';
} elseif (null !== $this->batchInsertThreshold && $this->batchInsertThreshold <= $log['num']) {
$query .= '.batchInsert(**'.$log['num'].' items**)';
} else {
$query .= '.batchInsert('.$this->bsonEncode($log['data']).')';
}
} elseif (isset($log['command'])) {
$query .= '.command()';
} elseif (isset($log['count'])) {
$query .= '.count(';
if ($log['query'] || $log['limit'] || $log['skip']) {
$query .= static::bsonEncode($log['query']);
$query .= $this->bsonEncode($log['query']);
if ($log['limit'] || $log['skip']) {
$query .= ', '.static::bsonEncode($log['limit']);
$query .= ', '.$this->bsonEncode($log['limit']);
if ($log['skip']) {
$query .= ', '.static::bsonEncode($log['skip']);
$query .= ', '.$this->bsonEncode($log['skip']);
}
}
}
Expand All @@ -175,54 +110,54 @@ protected function processQueries()
} elseif (isset($log['createDBRef'])) {
$query .= '.createDBRef()';
} elseif (isset($log['deleteIndex'])) {
$query .= '.dropIndex('.static::bsonEncode($log['keys']).')';
$query .= '.dropIndex('.$this->bsonEncode($log['keys']).')';
} elseif (isset($log['deleteIndexes'])) {
$query .= '.dropIndexes()';
} elseif (isset($log['drop'])) {
$query .= '.drop()';
} elseif (isset($log['dropDatabase'])) {
$query .= '.dropDatabase()';
} elseif (isset($log['ensureIndex'])) {
$query .= '.ensureIndex('.static::bsonEncode($log['keys']).', '.static::bsonEncode($log['options']).')';
$query .= '.ensureIndex('.$this->bsonEncode($log['keys']).', '.$this->bsonEncode($log['options']).')';
} elseif (isset($log['execute'])) {
$query .= '.execute()';
} elseif (isset($log['find'])) {
$query .= '.find(';
if ($log['query'] || $log['fields']) {
$query .= static::bsonEncode($log['query']);
$query .= $this->bsonEncode($log['query']);
if ($log['fields']) {
$query .= ', '.static::bsonEncode($log['fields']);
$query .= ', '.$this->bsonEncode($log['fields']);
}
}
$query .= ')';
} elseif (isset($log['findOne'])) {
$query .= '.findOne(';
if ($log['query'] || $log['fields']) {
$query .= static::bsonEncode($log['query']);
$query .= $this->bsonEncode($log['query']);
if ($log['fields']) {
$query .= ', '.static::bsonEncode($log['fields']);
$query .= ', '.$this->bsonEncode($log['fields']);
}
}
$query .= ')';
} elseif (isset($log['getDBRef'])) {
$query .= '.getDBRef()';
} elseif (isset($log['group'])) {
$query .= '.group('.static::bsonEncode(array(
$query .= '.group('.$this->bsonEncode(array(
'keys' => $log['keys'],
'initial' => $log['initial'],
'reduce' => $log['reduce'],
)).')';
} elseif (isset($log['insert'])) {
$query .= '.insert('.static::bsonEncode($log['document']).')';
$query .= '.insert('.$this->bsonEncode($log['document']).')';
} elseif (isset($log['remove'])) {
$query .= '.remove('.static::bsonEncode($log['query']).')';
$query .= '.remove('.$this->bsonEncode($log['query']).')';
} elseif (isset($log['save'])) {
$query .= '.save('.static::bsonEncode($log['document']).')';
$query .= '.save('.$this->bsonEncode($log['document']).')';
} elseif (isset($log['sort'])) {
$query .= '.sort('.static::bsonEncode($log['sortFields']).')';
$query .= '.sort('.$this->bsonEncode($log['sortFields']).')';
} elseif (isset($log['update'])) {
// todo: include $log['options']
$query .= '.update('.static::bsonEncode($log['query']).', '.static::bsonEncode($log['newObj']).')';
$query .= '.update('.$this->bsonEncode($log['query']).', '.$this->bsonEncode($log['newObj']).')';
} elseif (isset($log['validate'])) {
$query .= '.validate()';
}
Expand All @@ -234,12 +169,15 @@ protected function processQueries()
$query = 'db'.$query;
}

$this->formattedQueries[$i++] = $query.';';
++$this->nbRealQueries;
$this->data['queries'][$i++] = $query.';';
++$this->data['nb_queries'];
}
}

static protected function bsonEncode($query, $array = true)
/**
* @todo Move this to a collaborator
*/
private function bsonEncode($query, $array = true)
{
$parts = array();

Expand All @@ -257,13 +195,13 @@ static protected function bsonEncode($query, $array = true)
} elseif (is_scalar($value)) {
$formatted = '"'.$value.'"';
} elseif (is_array($value)) {
$formatted = static::bsonEncode($value);
$formatted = $this->bsonEncode($value);
} elseif ($value instanceof \MongoId) {
$formatted = 'ObjectId("'.$value.'")';
} elseif ($value instanceof \MongoDate) {
$formatted = 'new Date("'.date('r', $value->sec).'")';
$formatted = 'new ISODate("'.date('c', $value->sec).'")';
} elseif ($value instanceof \DateTime) {
$formatted = 'new Date("'.date('r', $value->getTimestamp()).'")';
$formatted = 'new ISODate("'.date('c', $value->getTimestamp()).'")';
} elseif ($value instanceof \MongoRegex) {
$formatted = 'new RegExp("'.$value->regex.'", "'.$value->flags.'")';
} elseif ($value instanceof \MongoMinKey) {
Expand All @@ -275,7 +213,7 @@ static protected function bsonEncode($query, $array = true)
} elseif ($value instanceof \MongoGridFSFile || $value instanceof GridFSFile) {
$formatted = 'new MongoGridFSFile("'.$value->getFilename().'")';
} elseif ($value instanceof \stdClass) {
$formatted = static::bsonEncode((array) $value);
$formatted = $this->bsonEncode((array) $value);
} else {
$formatted = (string) $value;
}
Expand Down
Expand Up @@ -11,32 +11,34 @@

namespace Symfony\Bundle\DoctrineMongoDBBundle\DataCollector;

use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Bundle\DoctrineMongoDBBundle\Logger\DoctrineMongoDBLogger;
use Symfony\Bundle\DoctrineMongoDBBundle\Logger\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;

/**
* Data collector for the Doctrine MongoDB ODM.
*
* @author Kris Wallsmith <kris@symfony.com>
*/
class DoctrineMongoDBDataCollector extends DataCollector
class StandardDataCollector extends DataCollector implements LoggerInterface
{
protected $logger;
protected $queries;

public function __construct()
{
$this->queries = array();
}

public function __construct(DoctrineMongoDBLogger $logger)
public function logQuery(array $query)
{
$this->logger = $logger;
$this->queries[] = $query;
}

/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
$this->data['nb_queries'] = $this->logger->getNbQueries();
$this->data['queries'] = $this->logger->getQueries();
$this->data['nb_queries'] = count($this->queries);
$this->data['queries'] = array_map('json_encode', $this->queries);
}

public function getQueryCount()
Expand All @@ -49,9 +51,6 @@ public function getQueries()
return $this->data['queries'];
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'mongodb';
Expand Down
23 changes: 10 additions & 13 deletions DependencyInjection/Configuration.php
Expand Up @@ -13,18 +13,6 @@
*/
class Configuration implements ConfigurationInterface
{
private $debug;

/**
* Constructor.
*
* @param Boolean $debug The kernel.debug value
*/
public function __construct($debug)
{
$this->debug = (Boolean) $debug;
}

/**
* Generates the configuration tree builder.
*
Expand Down Expand Up @@ -70,7 +58,16 @@ private function addDocumentManagersSection(ArrayNodeDefinition $rootNode)
->children()
->scalarNode('connection')->end()
->scalarNode('database')->end()
->booleanNode('logging')->defaultValue($this->debug)->end()
->booleanNode('logging')->defaultValue('%kernel.debug%')->end()
->arrayNode('profiler')
->addDefaultsIfNotSet()
->treatTrueLike(array('enabled' => true))
->treatFalseLike(array('enabled' => false))
->children()
->booleanNode('enabled')->defaultValue('%kernel.debug%')->end()
->booleanNode('pretty')->defaultValue('%kernel.debug%')->end()
->end()
->end()
->scalarNode('auto_mapping')->defaultFalse()->end()
->arrayNode('metadata_cache_driver')
->beforeNormalization()
Expand Down

0 comments on commit e622702

Please sign in to comment.