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

Already on GitHub? Sign in to your account

RedBean_ILogger for debug output #143

Merged
merged 3 commits into from Apr 11, 2012
View
@@ -28,6 +28,12 @@ class RedBean_Driver_PDO implements RedBean_Driver {
*/
protected $debug = false;
+ /**
+ * Holds an instance of ILogger implementation.
+ * @var RedBean_ILogger
+ */
+ protected $logger = NULL;
+
/**
* Holds the PDO instance.
* @var PDO
@@ -182,8 +188,8 @@ protected function bindParams($s,$aValues) {
*/
protected function runQuery($sql,$aValues) {
$this->connect();
- if ($this->debug) {
- echo '<HR>' . $sql.print_r($aValues,1);
+ if ($this->debug && $this->logger) {
+ $this->logger->log($sql, $aValues);
}
try {
if (strpos('pgsql',$this->dsn)===0) {
@@ -197,7 +203,7 @@ protected function runQuery($sql,$aValues) {
$this->affected_rows = $s->rowCount();
if ($s->columnCount()) {
$this->rs = $s->fetchAll();
- if ($this->debug) echo '<br><b style="color:green">resultset: ' . count($this->rs) . ' rows</b>';
+ if ($this->debug && $this->logger) $this->logger->log('resultset: ' . count($this->rs) . ' rows');
}
else {
$this->rs = array();
@@ -333,15 +339,39 @@ public function Affected_Rows() {
* passes on to the screen for inspection.
* This method has no return value.
*
+ * Additionally you can inject RedBean_ILogger implementation
+ * where you can define your own log() method
+ *
* @param boolean $trueFalse turn on/off
+ * @param RedBean_ILogger $logger
*
* @return void
*/
- public function setDebugMode( $tf ) {
+ public function setDebugMode( $tf, $logger = NULL ) {
$this->connect();
$this->debug = (bool)$tf;
+ if ($this->debug and !$logger) $logger = new RedBean_Logger();
+ $this->setLogger($logger);
+ }
+
+
+ /**
+ * Injects RedBean_ILogger object.
+ *
+ * @param RedBean_ILogger $logger
+ */
+ public function setLogger( RedBean_ILogger $logger ) {
+ $this->logger = $logger;
}
+ /**
+ * Gets RedBean_ILogger object.
+ *
+ * @return RedBean_ILogger
+ */
+ public function getLogger() {
+ return $this->logger;
+ }
/**
* Starts a transaction.
View
@@ -143,12 +143,13 @@ public static function selectDatabase($key) {
/**
* Toggles DEBUG mode.
* In Debug mode all SQL that happens under the hood will
- * be printed to the screen.
+ * be printed to the screen or logged by provided logger.
*
* @param boolean $tf
+ * @param RedBean_ILogger $logger
*/
- public static function debug( $tf = true ) {
- self::$adapter->getDatabase()->setDebugMode( $tf );
+ public static function debug( $tf = true, $logger = NULL ) {
+ self::$adapter->getDatabase()->setDebugMode( $tf, $logger );
}
/**
View
@@ -0,0 +1,25 @@
+<?php
+/**
+ * RedBean interface for Logging
+ *
+ * @name RedBean ILogger
+ * @file RedBean/ILogger.php
+ * @author Gabor de Mooij
+ * @license BSD
+ *
+ *
+ * copyright (c) G.J.G.T. (Gabor) de Mooij
+ * This source file is subject to the BSD/GPLv2 License that is bundled
+ * with this source code in the file license.txt.
+ */
+interface RedBean_ILogger {
+
+ /**
+ * Redbean will call this method to log your data
+ *
+ * @param ...
+ */
+ public function log();
+
+
+}
View
@@ -0,0 +1,32 @@
+<?php
+/**
+ * RedBean class for Logging
+ *
+ * @name RedBean ILogger
+ * @file RedBean/ILogger.php
+ * @author Gabor de Mooij
+ * @license BSD
+ *
+ *
+ * copyright (c) G.J.G.T. (Gabor) de Mooij
+ * This source file is subject to the BSD/GPLv2 License that is bundled
+ * with this source code in the file license.txt.
+ */
+class RedBean_Logger implements RedBean_ILogger {
+
+ /**
+ * Default logger method logging to STDOUT
+ *
+ * @param ...
+ */
+ public function log() {
+ if (func_num_args() > 0) {
+ foreach (func_get_args() as $argument) {
+ echo print_r($argument,1);
+ }
+ echo "\n";
+ }
+ }
+}
+
+
@@ -188,7 +188,7 @@ public function scanType( $value, $flagSpecial=false ) {
if (preg_match('/^\d{4}\-\d\d-\d\d$/',$value)) {
return RedBean_QueryWriter_PostgreSQL::C_DATATYPE_SPECIAL_DATE;
}
- if (preg_match('/^\d{4}\-\d\d-\d\d\s\d\d:\d\d:\d\d$/',$value)) {
+ if (preg_match('/^\d{4}\-\d\d-\d\d\s\d\d:\d\d:\d\d(\.\d{1,6})?$/',$value)) {
return RedBean_QueryWriter_PostgreSQL::C_DATATYPE_SPECIAL_DATETIME;
}
if (strpos($value,'POINT(')===0) {
View
@@ -32,6 +32,8 @@
<item type="php">RedBean/ExtAssociationManager.php</item>
<item type="php">RedBean/Setup.php</item>
<item type="php">RedBean/IModelFormatter.php</item>
+ <item type="php">RedBean/ILogger.php</item>
+ <item type="php">RedBean/Logger.php</item>
<item type="php">RedBean/IBeanHelper.php</item>
<item type="php">RedBean/BeanHelperFacade.php</item>
<item type="php">RedBean/SimpleModel.php</item>