Error handling, debugging and logging
This page is about how Kiwa handles errors, and how you can debug your website and store logs.
Table of contents
Environment
The behaviour of the error handling depends on the APP_ENV environment variable, which is read from the .env file (see also Environment variables):
- In a development environment (
dev), all errors are displayed and reported. - In a production environment (
prod) or in tests (test), error reporting is disabled so no internal information leaks to the visitor. Errors are still logged.
Error handling
Kiwa registers a custom error handler on every request. It does the following things:
- It logs all errors with the logger.
- It stores every error in a persistent file, so errors can be inspected even after the request has been finished.
- It renders a styled HTML error page for the visitor.
Custom error pages
Errors with an HTTP status code are rendered with a file named after the status code inside the html folder. The website skeleton already ships a 404.phtml, and you can create any other status page, for example 500.phtml.
If a phtml file throws an exception while being rendered, Kiwa responds with a 500 and tries to load html/500.phtml. If a page doesn't exist, Kiwa responds with a 404 and loads html/404.phtml. If no custom error page is available, a built-in error page is loaded instead.
Logging
Kiwa uses a PSR-3 compatible logger, which is accessible through the DI class:
<?php
use Kiwa\DI;
DI::getLog()->info('This is an info message.');
DI::getLog()->debug('This is a debug message.');
DI::getLog()->error('This is an error message.');
The logger writes to a file inside the var folder. All log levels of the PSR-3 standard are available: emergency(), alert(), critical(), error(), warning(), notice(), info() and debug().
In a development environment, log messages are also printed directly to the command line when using the Kiwa Console.
Debugging
The error pages and the log files are stored inside the var folder of your project. You can inspect them with the tools of your choice, for example:
$ tail -f var/log/debug.log
If you want to see all available PHP errors in your local development environment, make sure the value APP_ENV is set to dev in your .env.local file. Read more about the environment variables under the section The configuration.