CakePHP 2: Get complete SQL error log in error.log
I want to be able to log complete SQL queries to error.log when an SQL error occurs. My debug is set to 2 in core.php.
The output looks like this:
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 partially logged and the rest are sliced with ellipsis.
I am using DebugKit in combination with this, but even this does not give full SQL logs at a point in time in the DebugKit window.
I am throwing exceptions and writing getTraceAsString () here.
Any solutions are greatly appreciated.
Thank you,
t ^ e
source to share
You can try using this question . I know this is not the best answer, but in this particular question, you can find this plugin that will output the full SQL for the log file.
source to share
I did the following (Cake 2.5.6)
- create your own 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 boot tray:
require_once(APP . 'Lib' . DS . 'Error' . DS . 'ErrorHandlerWithSqlLog.php');
and change the default yor exception handler in core.php to a custom one:
Configure::write('Exception', array(
//'handler' => 'ErrorHandler::handleException',
'handler' => 'ErrorHandlerWithSqlLog::handleException',
'renderer' => 'ExceptionRenderer',
'log' => true
));
source to share