mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
Improve messages logged for exceptions
This commit is contained in:
parent
676872d623
commit
d8551c49e5
2 changed files with 29 additions and 7 deletions
|
@ -22,6 +22,7 @@
|
|||
App::uses('Debugger', 'Utility');
|
||||
App::uses('CakeLog', 'Log');
|
||||
App::uses('ExceptionRenderer', 'Error');
|
||||
App::uses('Router', 'Routing');
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -109,12 +110,7 @@ class ErrorHandler {
|
|||
public static function handleException(Exception $exception) {
|
||||
$config = Configure::read('Exception');
|
||||
if (!empty($config['log'])) {
|
||||
$message = sprintf("[%s] %s\n%s",
|
||||
get_class($exception),
|
||||
$exception->getMessage(),
|
||||
$exception->getTraceAsString()
|
||||
);
|
||||
CakeLog::write(LOG_ERR, $message);
|
||||
CakeLog::write(LOG_ERR, self::_getMessage($exception));
|
||||
}
|
||||
$renderer = $config['renderer'];
|
||||
if ($renderer !== 'ExceptionRenderer') {
|
||||
|
@ -136,6 +132,32 @@ class ErrorHandler {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a formatted error message
|
||||
* @param Exception $exception Exception instance
|
||||
* @return string Formatted message
|
||||
*/
|
||||
protected function _getMessage($exception) {
|
||||
$message = sprintf("[%s] %s",
|
||||
get_class($exception),
|
||||
$exception->getMessage()
|
||||
);
|
||||
if (method_exists($exception, 'getAttributes')) {
|
||||
$attributes = $exception->getAttributes();
|
||||
if ($attributes) {
|
||||
$message .= "\nException Attributes: " . var_export($exception->getAttributes(), true);
|
||||
}
|
||||
}
|
||||
if (php_sapi_name() != 'cli') {
|
||||
$request = Router::getRequest();
|
||||
if ($request) {
|
||||
$message .= "\nRequest URL: " . $request->here();
|
||||
}
|
||||
}
|
||||
$message .= "\nStack Trace:\n" . $exception->getTraceAsString();
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set as the default error handler by CakePHP. Use Configure::write('Error.handler', $callback), to use your own
|
||||
* error handling methods. This function will use Debugger to display errors when debug > 0. And
|
||||
|
|
|
@ -224,7 +224,7 @@ class ErrorHandlerTest extends CakeTestCase {
|
|||
|
||||
$log = file(LOGS . 'error.log');
|
||||
$this->assertRegExp('/\[NotFoundException\] Kaboom!/', $log[0], 'message missing.');
|
||||
$this->assertRegExp('/\#0.*ErrorHandlerTest->testHandleExceptionLog/', $log[1], 'Stack trace missing.');
|
||||
$this->assertRegExp('/\#0.*ErrorHandlerTest->testHandleExceptionLog/', $log[2], 'Stack trace missing.');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue