Documentation
Pretty Exception Handler
Introduction
Beautiful and powerful Tracy debugger pre-configured for you when you install the framework. By default environment is set to development meaning that debugger is enabled. This also means that when an error occurs you will be shown an error page with a detailed stack trace and error message. You may turn off the debugger by changing the environment as production in your src/Apps/Configs/application.php file. It is advisable to set the mode to production when you deploy the project to production server.
Activating Debugger
By default debugger is enabled for you in src/Apps/Configs/application.php file. You can change the configuration accordingly. In development environment when any error occurs it will display exception message with pretty stack trace panel. Below configuration key to activate or deactivate the debugger.
'environment' => 'development',//or production
All exceptions are handled by the Cygnite\Exception\ExceptionHandler class. This class is the wrapper for Tracy debugger.
[Note: We strongly recommend to turn off debugger in production server.]
Logging Exceptions
By default cygnite provides out of box configurations for you, you may also change the configuration based on the requirement. You may activate or deactivate the logger by changing the configuration true or false in the src/Apps/Configs/application.php file. You can also specify where logs should get stored and also turn on emailing if any error occurs in production mode.
'logs' => [
/*
|-------------------------------------------------------------
| Log application errors
|-------------------------------------------------------------
| If you activate (true/false) debugger will generate error logs
| into public/storage/logs/
|
*/
'activate' => true,
/*
|------------------------------------------------------------
| Logs Storage Location
|------------------------------------------------------------
| Set your log storage location here. By default we are using
| public/storage/logs/
|
*/
'path' => 'public.storage.logs',
/*
| Activate error emailing. When any error occur in production
| mode debugger will trigger email. Set true or false
*/
'error.emailing' => false,
/*
| We will make use of email address to send error log
| when application is in production mode.
|
*/
'email' => 'xyz@gmail.com',
],
All your exceptions occurred in your application will be stored as html file format in the public/storage/logs/ directory with date and time in the filename.
HTTP Exceptions
You may wish to generate a custom error page on HTTP errors. For example, it may be "page not found" error (404), an "unauthorized error" (401) or even a developer generated 500 error. For such case, use the following:
$app->abort(404);
or
$app->abort(404, "Abort!! 404 Page Not Found!"); // optional error message
$app->abort(403, 'Unauthorized action.');
Handling 404 Errors
For responding to HTTP errors there are view pages available already in src/Apps/Views/errors/ folder. For handling 404 error in production mode 404.view.php view page available in the same directory, you may customize based on the requirement. Similarly for other HTTP server error codes view pages available. Based on the exception code it will pick the custom error view page and display in the browser.
Dumping Variables In Debug Bar
As like firebug console you can also dump your data into debugger panel. Sometime you may wish to dump data or sql query in the debug bar. For that you can either use d() helper function or ExceptionHandler as below.
use Cygnite\Exception\ExceptionHandler as Debugger;
Debugger::dump('select * from user', 'SQL'); // Dumping Sql
Debugger::dump(['foo' => 'bar'], 'bar'); // Dumping Data into bar
or
d(['foo' => 'bar'], 'bar'[]);
d('select * from user', 'SQL');
Log
Use log method to log any exception.
use Cygnite\Exception\ExceptionHandler as Debugger;
try {
} catch (Exception $e) {
Debugger::log($e);
}
Or
logMessage($msg, $priority);
You may use popular Monolog library for better logging purpose.
FireLogger
You cannot always send debugging information to the browser window. Using ajax request you may want to display output. In such cases, you can send the messages by a separate channel into FireLogger.
fire_log($message);
In order to make it work make sure you have installed the extension in your browser.
Firefox: Firebug and FireLogger
turn on Firebug (using F12 key), enable tabs Net and Logger (stay on Logger)
Chrome: FireLogger for Chrome,
turn on Chrome DevTools (using Ctrl-Shift-I key) and open Console
Timing
Another useful function (time_bench) is stopwatch with a precision of microseconds:
time_bench();
// some code
$elapsed = time_bench();
Multiple Time Measurements
time_bench('page-loading');
// some code
time_bench('data-populating');
// some code
$rssElapsed = time_bench('data-populating');
$pageElapsed = time_bench('page-loading');