This article is a follow-up for: Logging with dot-log in Zend Expressive and DotKernel, the mentioned article is a guide to using dot-log.
This article explains the usage of dotkernel/dot-errorhandler with dotkernel/dot-log or zendframework/zend-log to log errors in Zend Expressive applications.
This can be considered as a guide on how the dot-errorhandler was made and how it's meant to be used.
As a first note Dot Error Handler provides two kinds of error handlers:
- the plain
ErrorHandler- this class is a copy of Zend Expressive's error handlerZend\Stratigility\Middleware\ErrorHandler(as it is final) - the logging
LogErrorHandler- this class is like the above one, but with added Logging support (via container)
composer require dotkernel/dot-errorhandler.
The Config Provider
When the dot-errorhandler config provider is invoked the following configuration is returned.,
'factories' =>
],
];
Both the error handlers have the factories registered, and an alias to switch between them is added.
As a fallback case the plain error handler is selected by default and can be overwritten through the config file.
Configuration
IMPORTANT NOTES:- Assuming the project in hand has a configured logger as per this article and the logger name is default_logger (as provided in the package's config example).
- Although the key is
dot_log, when selecting a logger the dot log abstract factory responds to thedot-logselector- to select
the container key asked for isdot-log.default_logger
- to select
- The dot-errorhandler was meant as a silent logger for staging and production environments doesn't block whoops
- to test it the development mode should be disabled, otherwise whoops will catch the errors and show them to the developer
- you can use any custom implemented error handler as long as it implements the provided
ErrorHandlerInterface
- add the
Dot\ErrorHandler\ConfigProviderin the project'sconfig/config.phpfile - write the error handler config
config/autoload/dot-errorhandler.global.php
use Dot\ErrorHandler\ErrorHandlerInterface;
use Dot\ErrorHandler\LogErrorHandler;
return
],
'dot-errorhandler' =>
];
The logger key in dot-error handler should reflect your logger configuration in config/autoload/log.global.php
To use the default logger an out-of-the-box config was provided within the error handler's config directory.
Usage / Triggering errors
The tests we have made were the following:- throwing Exceptions - the most common
- raising errors such as triggering warning/error messages:
- by dividing numbers to zero (eg.: 16/0)
- by casting arrays to strings ($string = 'hello' . )