3

I want to be able to log full SQL queries in error.log specifically when a SQL error occurs. My debug is set to 2 in core.php.

The output is appearing as follows:

2013-01-29 19:53:21 Error: SQLSTATE[HY000]: General error: 1364 Field 'street' doesn't have a default value
#0 D:\xampp\htdocs\my_project\lib\Cake\Model\Datasource\DboSource.php(459): PDOStatement->execute(Array)
#1 D:\xampp\htdocs\my_project\lib\Cake\Model\Datasource\DboSource.php(425): DboSource->_execute('INSERT INTO `st...', Array)
#2 D:\xampp\htdocs\my_project\lib\Cake\Model\Datasource\DboSource.php(1007): DboSource->execute('INSERT INTO `st...')

As you can see, the SQL statements are only logged partially with the rest being chopped-of with ellipsis.

I'm using DebugKit in conjunction with this, but even that doesn't produce full SQL logs at time in the DebugKit window.

I'm throwing exceptions and logging the getTraceAsString() here.

Any solutions will be greatly appreciated.

Thank you,
m^e

miCRoSCoPiC_eaRthLinG
  • 2,910
  • 4
  • 40
  • 56

3 Answers3

0

You can try to use the answer of this question. I know it is not the best answer, but in that particular question you can find this plugin that will output the full SQL for a log file.

Community
  • 1
  • 1
Frederico Schardong
  • 1,946
  • 6
  • 38
  • 62
0

I did the following (Cake 2.5.6)

  1. create custom error handler in

/app/Lib/Error/ErrorHandlerWithSqlLog.php

App::uses('ErrorHandler', 'Error');
class ErrorHandlerWithSqlLog{
    public static function handleException(Exception $exception) {
        if ($exception instanceof PDOException && !empty($exception->queryString)) {            
            CakeLog::write(LOG_ERR, "SQL query: ".$exception->queryString);
        }        
        ErrorHandler::handleException($exception);      
    }
}

add it to your bootstrap:

require_once(APP . 'Lib' . DS . 'Error' . DS . 'ErrorHandlerWithSqlLog.php');

and change yor default exception handler in core.php to the custom one:

Configure::write('Exception', array(
    //'handler' => 'ErrorHandler::handleException',
    'handler' => 'ErrorHandlerWithSqlLog::handleException',
    'renderer' => 'ExceptionRenderer',
    'log' => true
));
Panter4
  • 327
  • 2
  • 7
0

It more easy catch excetion,

PDOException has a propertie called queryString with the current query

you can see in source file DboSource.php in _execute Method

try {
$this->MyModel->find(....)
}catch(Exception $_ex) {
var_dump($_ex)
}

i hope help to you

tierrarara
  • 61
  • 8