Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging configuration #51

Merged
merged 6 commits into from Dec 9, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion config/module.doctrine-mongo-odm.local.php.dist
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
@@ -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
@@ -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
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
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
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
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