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

RedBean_ILogger for debug output #143

Merged
merged 3 commits into from
Apr 11, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions RedBean/Driver/PDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions RedBean/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down
25 changes: 25 additions & 0 deletions RedBean/ILogger.php
Original file line number Diff line number Diff line change
@@ -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();


}
32 changes: 32 additions & 0 deletions RedBean/Logger.php
Original file line number Diff line number Diff line change
@@ -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";
}
}
}


2 changes: 1 addition & 1 deletion RedBean/QueryWriter/PostgreSQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions replica.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down