Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Logging configuration #51

Merged
merged 6 commits into from

4 participants

Jérémy Huet Christian Gahlert Marco Pivetta SteveTalbot
Jérémy Huet

Fixes #48 by adding an option to set the loggerCallable to log queries.

Christian Gahlert

:+1: Have been waiting for this! :)

...ctrineMongoODMModule/Service/ConfigurationFactory.php
@@ -46,6 +46,13 @@ public function createService(ServiceLocatorInterface $serviceLocator)
$config = new Configuration;
+ // the logger with an injection of the service locator as a 2nd argument
+ if (is_callable($options->getLoggerCallable())) {
+ $config->setLoggerCallable(function(array $log) use ($options, $serviceLocator) {
Marco Pivetta Owner

Could you instead refactor it so that the logger callable COMES from the service locator (instead of having SL in the logger)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...ctrineMongoODMModule/Service/ConfigurationFactory.php
@@ -46,6 +46,13 @@ public function createService(ServiceLocatorInterface $serviceLocator)
$config = new Configuration;
+ // the logger with an injection of the service locator as a 2nd argument
+ if (is_callable($options->getLoggerCallable())) {
+ $config->setLoggerCallable(function(array $log) use ($options, $serviceLocator) {
+ call_user_func_array($options->getLoggerCallable(), array($log, $serviceLocator));
Marco Pivetta Owner

4 spaces too much ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
config/module.doctrine-mongo-odm.local.php.dist
@@ -29,7 +29,10 @@ return array(
//
// 'default_db' => null,
//
-// 'filters' => array() // array('filterName' => 'BSON\Filter\Class')
+// 'filters' => array() // array('filterName' => 'BSON\Filter\Class'),
+//
+// 'logger_callable' => function (array $log, \Zend\ServiceManager\ServiceLocatorInterface $sl) {
Marco Pivetta Owner

This can just be the service name for the logger, which would allow us to reuse the logger in ZDT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This was referenced
Jérémy Huet jhuet referenced this pull request from a commit in jhuet/DoctrineMongoODMModule
Jérémy Huet jhuet Zend Developer Tools integration based on #51 logs implementation. 983c198
Marco Pivetta
Owner

@jhuet what's the status here? =)

Jérémy Huet

Hey @Ocramius, sorry i didn't follow up with a comment but to me it's working the way we wanted it :) The failed Travis build status is weird as i have no clue why the doctrine/data-fixtures v1.0.0-ALPHA3 zipball couldn't be downloaded only for the PHP 5.3 env.

Marco Pivetta Ocramius merged commit bb78540 into from
SteveTalbot

This change assumes the configuration is named the same as the connection, but actually it's possible to specify different configuration and connection names in the document manager configuration.

The following example configuration will throw a ServiceNotFoundException when you try to instantiate the document manager called MyDM, with the error message "Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for doctrine.configuration.MyConnection"

'documentmanager' => array(
    'MyDM' => array(
        'configuration' => 'MyConfig',
        'connection' => 'MyConnection'
    )
),
'configuration' => array(
    'MyConfig' => array( ... )
),
'connection' => array(
    'MyConnection' => array( ... )
)

Is it by design that the connection and configuration have to be named the same (in which case why have two different options in the document manager?) or has this change introduced a bug?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
4 config/module.doctrine-mongo-odm.local.php.dist
View
@@ -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'
)
),
47 src/DoctrineMongoODMModule/Logging/DebugStack.php
View
@@ -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 src/DoctrineMongoODMModule/Logging/Logger.php
View
@@ -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  src/DoctrineMongoODMModule/Module.php
View
@@ -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',
),
26 src/DoctrineMongoODMModule/Options/Configuration.php
View
@@ -104,6 +104,12 @@ class Configuration extends AbstractOptions
/**
*
+ * @var \DoctrineMongoODMModule\Logging\Logger
+ */
+ protected $logger;
+
+ /**
+ *
* @param string $driver
* @return \DoctrineMongoODMModule\Options\Configuration
*/
@@ -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 src/DoctrineMongoODMModule/Service/ConfigurationFactory.php
View
@@ -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());
2  src/DoctrineMongoODMModule/Service/ConnectionFactory.php
View
@@ -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()));
}
/**
Something went wrong with that request. Please try again.