Skip to content

Commit

Permalink
Merge pull request #51 from jhuet/48-logging-configuration
Browse files Browse the repository at this point in the history
Logging configuration
  • Loading branch information
Ocramius committed Dec 9, 2012
2 parents 0649743 + d029025 commit bb78540
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 2 deletions.
4 changes: 3 additions & 1 deletion config/module.doctrine-mongo-odm.local.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ return array(
//
// 'default_db' => null,
//
// 'filters' => array() // array('filterName' => 'BSON\Filter\Class')
// 'filters' => array(), // array('filterName' => 'BSON\Filter\Class'),
//
// 'logger' => null // 'DoctrineMongoODMModule\Logging\DebugStack'
)
),

Expand Down
47 changes: 47 additions & 0 deletions src/DoctrineMongoODMModule/Logging/DebugStack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace DoctrineMongoODMModule\Logging;

/**
* Includes executed queries in a Debug Stack
*
* @license MIT
* @link www.doctrine-project.org
*/
class DebugStack implements Logger
{
/** @var array $queries Executed queries. */
public $queries = array();

/** @var boolean $enabled If Debug Stack is enabled (log queries) or not. */
public $enabled = true;

protected $currentQuery = 0;

/**
* {@inheritdoc}
*/
public function log(array $logs)
{
if ($this->enabled) {
$this->queries[++$this->currentQuery] = $logs;
}
}
}

36 changes: 36 additions & 0 deletions src/DoctrineMongoODMModule/Logging/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace DoctrineMongoODMModule\Logging;

/**
* Interface for loggers.
*
* @license MIT
* @link www.doctrine-project.org
*/
interface Logger
{
/**
* Logs a SQL statement somewhere.
*
* @param array $logs The logs explaining the query
* @return void
*/
public function log(array $logs);
}
3 changes: 3 additions & 0 deletions src/DoctrineMongoODMModule/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public function getConfig()
public function getServiceConfig()
{
return array(
'invokables' => array(
'DoctrineMongoODMModule\Logging\DebugStack' => 'DoctrineMongoODMModule\Logging\DebugStack',
),
'aliases' => array(
'Doctrine\ODM\Mongo\DocumentManager' => 'doctrine.documentmanager.odm_default',
),
Expand Down
26 changes: 26 additions & 0 deletions src/DoctrineMongoODMModule/Options/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ class Configuration extends AbstractOptions
*/
protected $filters = array();

/**
*
* @var \DoctrineMongoODMModule\Logging\Logger
*/
protected $logger;

/**
*
* @param string $driver
Expand Down Expand Up @@ -286,4 +292,24 @@ public function setFilters(array $filters) {
$this->filters = $filters;
return $this;
}

/**
*
* @param \DoctrineMongoODMModule\Logging\Logger $logger
* @return \DoctrineMongoODMModule\Options\Configuration
*/
public function setLogger($logger)
{
$this->logger = $logger;
return $this;
}

/**
*
* @return \DoctrineMongoODMModule\Logging\Logger
*/
public function getLogger()
{
return $this->logger;
}
}
6 changes: 6 additions & 0 deletions src/DoctrineMongoODMModule/Service/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public function createService(ServiceLocatorInterface $serviceLocator)

$config = new Configuration;

// logger
if ($options->getLogger()) {
$logger = $serviceLocator->get($options->getLogger());
$config->setLoggerCallable(array($logger, 'log'));
}

// proxies
$config->setAutoGenerateProxyClasses($options->getGenerateProxies());
$config->setProxyDir($options->getProxyDir());
Expand Down
2 changes: 1 addition & 1 deletion src/DoctrineMongoODMModule/Service/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function createService(ServiceLocatorInterface $serviceLocator)
if ($options->getDbName()) {
$connectionString .= '/' . $options->getDbName();
}
return new Connection($connectionString, $options->getOptions());
return new Connection($connectionString, $options->getOptions(), $serviceLocator->get('doctrine.configuration.'.$this->getName()));
}

/**
Expand Down

0 comments on commit bb78540

Please sign in to comment.