Flexible error handling and logging

Most applications are evolving over time with new functionality being added or changed as the business requirements change over time. The toKernel framework allows applications to be run in two modes, production and development.

When an error occurs in production mode, execution will be redirected to an error page that will display the error message you have written when configuring the application (together with the languages the application should support)

However, in development mode, execution will be redirected to detailed error page, where you can see all available information about error type, message, file, line.

You can configure error handling by your choosing the appropriate level: errors, warnings, notices. All events also will be logged with the level of details you have specified.

How can you report errors you detected?

There are several ways of triggering an error, including the simple trigger_error() function. For example:

a. trigger_error('error message here', E_USER_ERROR);
b. $this->app->error(E_USER_ERROR, 'error message here', [file], [line]);
c. tk_e::error(E_USER_ERROR, 'error message here', [file], [line]);

What about error 404 ?

Just call:


and the application will be redirected to 404 error page. Of course, you can design your own error 404 page.

How can you log errors?

Frankly, this is quite easy too, since toKernel provides a log class and all addons inherit their own 'log' object.

For example, in any part of an addon class or modules you can call:

$this->log->write('some message');

To create a new instance for the log object, call:

$this->lib->log->instance('log/file/path', [max_size_in_bytes], [log/files/path]);

This way you can start logging events or errors to you own dedicated file.

This log class brings also you many other advantages. For example, if the log file maximum size is reached, the class will automatically take the actions to gzip it and create and open a new empty log file. It includes other functions like compress, uncompress, files_list, files_clean and more to make the management of log files fast and easy.

See also: log class library.