Permalink
Browse files

Merge pull request #51 from jhuet/48-logging-configuration

Logging configuration
  • Loading branch information...
2 parents 0649743 + d029025 commit bb785407a5e9c330d659c5a951bdb5de2fcc774c @Ocramius Ocramius committed Dec 9, 2012
@@ -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'
)
),
@@ -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;
+ }
+ }
+}
+
@@ -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);
+}
@@ -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',
),
@@ -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;
+ }
}
@@ -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());
@@ -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()));
}
/**

0 comments on commit bb78540

Please sign in to comment.