Skip to content

Commit

Permalink
Moving some code around so that queries are logged despite errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jun 19, 2014
1 parent 2da943f commit b79f255
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/Database/Log/LoggedQuery.php
Expand Up @@ -50,6 +50,13 @@ class LoggedQuery {
*/
public $numRows = 0;

/**
* The exception that was thrown by the exceution of this query
*
* @var \Exception
*/
public $error;

/**
* Returns the string representation of this logged query
*
Expand Down
29 changes: 24 additions & 5 deletions src/Database/Log/LoggingStatement.php
Expand Up @@ -46,16 +46,35 @@ class LoggingStatement extends StatementDecorator {
*/
public function execute($params = null) {
$t = microtime(true);
$result = parent::execute($params);

$query = new LoggedQuery;
$query->took = round((microtime(true) - $t) * 1000, 0);

try {
$result = parent::execute($params);
} catch (\Exception $e) {
$query->error = $e;
$this->_log($query, $params, $t);
throw $e;
}

$this->_log($query, $params, $t);
return $result;
}

/**
* Copies the logging data to the passed LoggedQuery and sends it
* to the logging system.
*
* @param \Cake\Database\Log\LoggedQuery $query
* @param array $params list of values to be bound to query
* @param float $startTime the microtime when the query was executed
* @return void
*/
protected function _log($query, $params, $startTime) {
$query->took = round((microtime(true) - $startTime) * 1000, 0);
$query->numRows = $this->rowCount();
$query->params = $params ?: $this->_compiledParams;
$query->query = $this->queryString;
$this->logger()->log($query);

return $result;
}

/**
Expand Down

0 comments on commit b79f255

Please sign in to comment.