dotkernel / dot-errorhandler
Logging Error Handler for Middleware Applications
Installs: 19 158
Dependents: 4
Suggesters: 0
Security: 0
Stars: 5
Watchers: 6
Forks: 1
Open Issues: 1
Requires
- php: ~8.2.0 || ~8.3.0
- dotkernel/dot-log: ^4.0.2
- laminas/laminas-diactoros: ^3.3
- laminas/laminas-stratigility: ^3.11
- mezzio/mezzio: ^3.19
- psr/http-message: ^1.0 || ^2.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- laminas/laminas-coding-standard: ^2.5
- mikey179/vfsstream: ^1.6.7
- phpunit/phpunit: ^10.5
- vimeo/psalm: ^5.22
README
Error Logging Handler for DotKernel
Adding the error handler
- Add the composer package:
composer require dotkernel/dot-errorhandler
-
Add the config provider
- in
config/config.php
add\Dot\ErrorHandler\ConfigProvider
- in
config/pipeline.php
add\Dot\ErrorHandler\ErrorHandlerInterface::class
- the interface is used as an alias to keep all error handling related configurations in one file
- IMPORTANT NOTE there should be no other error handlers after this one (only before) because the other error handler will catch the error causing dot-errorhandler not to catch any error, we recommend using just one error handler unless you have an error-specific handler
- in
-
Configure the error handler as shown below
config/autoload/error-handling.global.php
<?php use Dot\ErrorHandler\ErrorHandlerInterface; use Dot\ErrorHandler\LogErrorHandler; use Dot\ErrorHandler\ErrorHandler; return [ 'dependencies' => [ 'aliases' => [ ErrorHandlerInterface::class => LogErrorHandler::class, ] ], 'dot-errorhandler' => [ 'loggerEnabled' => true, 'logger' => 'dot-log.default_logger' ] ];
A configuration example for the default logger can be found in config/log.global.php.dist
.
When declaring the ErrorHandlerInterface
alias you can choose whether to log or not:
- for logging use
LogErrorHandler
- for the simple Zend Expressive handler user
ErrorHandler
The class Dot\ErrorHandler\ErrorHandler
is the same as the Zend Expressive error handling class
the only difference being the removal of the final
statement for making extension possible.
The class Dot\ErrorHandler\LogErrorHandler
is Dot\ErrorHandler\ErrorHandler
with
added logging support.
As a note: both LogErrorHandler
and ErrorHandler
have factories declared in the
package's ConfigProvider
. If you need a custom ErrorHandler it must have a factory
declared in the config, as in the example.
Example:
<?php use Dot\ErrorHandler\ErrorHandlerInterface; use Custom\MyErrorHandler; use Custom\MyErrorHandlerFactory; return [ 'dependencies' => [ 'factories' => [ MyErrorHandler::class => MyCustomHandlerFactory::class, ], 'aliases' => [ ErrorHandlerInterface::class => MyErrorHandler::class, ] ], 'dot-errorhandler' => [ 'loggerEnabled' => true, 'logger' => 'dot-log.default_logger' ] ];
Config examples can be found in this project's config
directory.