Permalink
Browse files

[monolog] Made per-connection logger possible again.

As planned, this implies passing the connection name to the connection
via the ConnectionManager. It changes two interfaces, but it's not that
big a deal.
The modification also allows us to get rid of the Configuration object
in the ConnectionWrapper, so the next step is to remove this object from
the Runtime code altogether (its features are now handled mostly by the
ServiceContainer).
  • Loading branch information...
1 parent 49fc1bb commit 5f2e787d059f6821208bb73a0ee46f0aa7c178df @fzaninotto fzaninotto committed Dec 21, 2011
@@ -136,7 +136,7 @@ Alternatively, you can ask Propel to always enable the debug mode for a particul
<classname>Propel\Runtime\Connection\DebugPDO</classname>
{% endhighlight %}
->**Tip**<br />You can use your own connection class there, but make sure that it implements `Propel\Runtime\Connection\ConnectionInterface`. Propel requires certain fixes to PDO API that are provided by this interface.
+>**Tip**<br />You can use your own connection class there, but make sure that it implements `Propel\Runtime\Connection\ConnectionInterface`.
### Counting Queries ###
@@ -179,7 +179,7 @@ echo $con->getLastExecutedQuery(); // 'SELECT * FROM my_obj where name = "foo"';
## Full Query Logging ##
-If you use both the debug mode and a logger, then Propel logs automatically all executed queries in the provided log handler (or the `propel.log` file if no custom handler is defined):
+If you use both the debug mode and a logger, then Propel logs automatically all executed queries in the provided log handler:
{% highlight text %}
Oct 04 00:00:18 propel-bookstore [debug] INSERT INTO publisher (`ID`,`NAME`) VALUES (NULL,'William Morrow')
@@ -192,6 +192,54 @@ Oct 04 00:00:18 propel-bookstore [debug] SELECT bookstore_employee_account.EMPLO
By default, Propel logs all SQL queries, together with the date of the query and the name of the connection.
+### Using a Custom Logger per Connection ###
+
+To log SQL queries for a connection, Propel first looks for a logger named after the connection itself, and falls back to the default logger if no custom logger is defined for the connection.
+
+Using the follofing config, Propel will log SQL queries from the `bookstore` datasource into a `propel_bookstore.log` file, and the SQL queries for all other datasources into a `propel.log` file.
+
+{% highlight xml %}
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<config>
+ <log>
+ <logger name="defaultLogger">
+ <type>stream</type>
+ <path>/var/log/propel.log</path>
+ <level>300</level>
+ </logger>
+ <logger name="bookstore">
+ <type>stream</type>
+ <path>/var/log/propel_bookstore.log</path>
+ </logger>
+ </log>
+ <propel>
+ ...
+ </propel>
+</config>
+{% endhighlight %}
+
+This allows you to define a different logger per connection, for instance to have different log files for each database, or to log only the queries from a MySQL database to a file while the ones from an Oracle database go into Syslog.
+
+### Logging More Events ###
+
+By default, the full query logger logs only executed SQL queries. But the `ConnectionWrapper` class can write into the log when most of its methods are called. To enable more methods, just set the list of logging methods to use by calling `ConnectionWrapper::setLogMethods()`, as follows:
+
+{% highlight php %}
+<?php
+$con = Propel::getConnection(MyObjPeer::DATABASE_NAME);
+$con->setLogMethods(array(
+ 'exec',
+ 'query',
+ 'execute', // these first three are the default
+ 'beginTransaction',
+ 'commit',
+ 'rollBack',
+ 'bindValue'
+));
+{% endhighlight %}
+
+Note that this list takes into account the methods from both `ConnecitonWrapper` and `StatementWrapper`.
+
### Adding Profiler Information ###
In addition to the executed queries, you can ask Propel to log the execution time for each query, the memory consumption, and more. To enable profiling, change the connection class name to `Propel\Runtime\Connection\ProfilerConnectionWrapper` in the `runtime-conf.xml`, as follows:
@@ -281,31 +329,4 @@ $serviceContainer->setProfilerConfiguration(array(
'outerGlue' => ': ',
'innerGlue' => ' | '
));
-{% endhighlight %}
-
-### Changing the Log Level ###
-
-By default the connection log messages are logged at the `Propel::LOG_DEBUG` level. This can be changed by calling the `setLogLevel()` method on the connection object:
-
-{% highlight php %}
-<?php
-$con = Propel::getConnection(MyObjPeer::DATABASE_NAME);
-$con->setLogLevel(Propel::LOG_INFO);
-{% endhighlight %}
-
-Now all queries and bind param values will be logged at the INFO level.
-
-### Configuring a Different Full Query Logger ###
-
-By default the `ConnectionWrapper` connection logs queries and binds param values using the `Propel::log()` static method. As explained above, this method uses the log storage configured by the `<log>` tag in the `runtime-conf.xml` file.
-
-If you would like the queries to be logged using a different logger (e.g. to a different file, or with different ident, etc.), you can set a logger explicitly on the connection at runtime, using `Propel::setLogger()`:
-
-{% highlight php %}
-<?php
-$con = Propel::getConnection(MyObjPeer::DATABASE_NAME);
-$logger = Log::factory('syslog', LOG_LOCAL0, 'propel', array(), PEAR_LOG_INFO);
-$con->setLogger($logger);
-{% endhighlight %}
-
-This will not affect the general Propel logging, but only the full query logging. That way you can log the Propel error and warnings in one file, and the SQL queries in another file.
+{% endhighlight %}
@@ -21,6 +21,27 @@
class PdoConnection extends PDO implements ConnectionInterface
{
/**
+ * @var string The datasource name associated to this connection
+ */
+ protected $name;
+
+ /**
+ * @param string $name The datasource name associated to this connection
+ */
+ public function setName($name)
+ {
+ $this->name = $name;
+ }
+
+ /**
+ * @return string The datasource name associated to this connection
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ /**
* Creates a PDO instance representing a connection to a database.
*/
public function __construct($dsn, $user = null, $password = null, array $options = null)
@@ -20,6 +20,16 @@
interface ConnectionInterface
{
/**
+ * @param string $name The datasource name associated to this connection
+ */
+ function setName($name);
+
+ /**
+ * @return string The datasource name associated to this connection
+ */
+ function getName();
+
+ /**
* Turns off autocommit mode.
*
* While autocommit mode is turned off, changes made to the database via
@@ -15,6 +15,16 @@
interface ConnectionManagerInterface
{
/**
+ * @param string $name The datasource name associated to this connection
+ */
+ function setName($name);
+
+ /**
+ * @return string The datasource name associated to this connection
+ */
+ function getName();
+
+ /**
* @param \Propel\Runtime\Adapter\AdapterInterface $adapter
*
* @return \Propel\Runtime\Connection\ConnectionInterface
@@ -19,6 +19,11 @@ class ConnectionManagerMasterSlave implements ConnectionManagerInterface
{
/**
+ * @var string The datasource name associated to this connection
+ */
+ protected $name;
+
+ /**
* @var array
*/
protected $writeConfiguration;
@@ -44,6 +49,22 @@ class ConnectionManagerMasterSlave implements ConnectionManagerInterface
protected $isForceMasterConnection = false;
/**
+ * @param string $name The datasource name associated to this connection
+ */
+ public function setName($name)
+ {
+ $this->name = $name;
+ }
+
+ /**
+ * @return string The datasource name associated to this connection
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ /**
* For replication, whether to always force the use of a master connection.
*
* @return boolean
@@ -121,6 +142,7 @@ public function getWriteConnection(AdapterInterface $adapter = null)
{
if (null === $this->writeConnection) {
$this->writeConnection = ConnectionFactory::create($this->writeConfiguration, $adapter);
+ $this->writeConnection->setName($this->getName());
}
return $this->writeConnection;
@@ -149,6 +171,7 @@ public function getReadConnection(AdapterInterface $adapter = null)
$key = $keys[mt_rand(0, count($keys) - 1)];
$configuration = $this->readConfiguration[$key];
$this->readConnection = ConnectionFactory::create($configuration, $adapter);
+ $this->readConnection->setName($this->getName());
}
}
@@ -19,6 +19,11 @@
class ConnectionManagerSingle implements ConnectionManagerInterface
{
/**
+ * @var string The datasource name associated to this connection
+ */
+ protected $name;
+
+ /**
* @var array
*/
protected $configuration;
@@ -28,6 +33,22 @@ class ConnectionManagerSingle implements ConnectionManagerInterface
*/
protected $connection;
+ /**
+ * @param string $name The datasource name associated to this connection
+ */
+ public function setName($name)
+ {
+ $this->name = $name;
+ }
+
+ /**
+ * @return string The datasource name associated to this connection
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+
public function setConnection(ConnectionInterface $connection)
{
$this->setConfiguration(null);
@@ -49,6 +70,7 @@ public function getWriteConnection(AdapterInterface $adapter = null)
{
if (null === $this->connection) {
$this->connection = ConnectionFactory::create($this->configuration, $adapter);
+ $this->connection->setName($this->getName());
}
return $this->connection;
Oops, something went wrong.

0 comments on commit 5f2e787

Please sign in to comment.