ErrorHeroModule
:gem: A Hero for your Zend Framework/Laminas, and Expressive/Mezzio application to log ( DB and Mail ) and handle php errors & exceptions during Mvc process/between request and response
Install / Use
/learn @samsonasik/ErrorHeroModuleREADME
ErrorHeroModule
This is README for version ^7.0 which bump laminas-diactoros to ^3.8 and bump doctrine-orm-module to ^6.3, and only support Laminas Mvc version 3 and Mezzio version 3 with php ^8.3
For version ^6.0, you can read at version 6 readme which only support Laminas Mvc version 3 and Mezzio version 3 with php ^8.2.
For version ^5.0, you can read at version 5 readme which only support Laminas Mvc version 3 and Mezzio version 3 with php ^8.1.
Introduction
ErrorHeroModule is a module for Error Logging (DB and Mail) your Laminas Mvc 3 Application, and Mezzio 3 for Exceptions in 'dispatch.error' or 'render.error' or during request and response, and PHP E_* Error.
Features
- [x] Save to DB with Db Writer Adapter.
- [x] Log Exception (dispatch.error and render.error) and PHP Errors in all events process.
- [x] Support excludes PHP E_* Error (eg: exclude E_USER_DEPRECATED or specific E_USER_DEPRECATED with specific message) in config settings.
- [x] Support excludes PHP Exception (eg: Exception class or classes that extends it or specific exception class with specific message) in config settings.
- [x] Handle only once log error for same error per configured time range.
- [x] Set default page (web access) or default message (console access) for error if configured 'display_errors' = 0.
- [x] Set default content when request is XMLHttpRequest via 'ajax' configuration.
- [x] Set default content when there is no template service via 'no_template' configuration (Mezzio 3).
- [x] Provide request information ( http method, raw data, body data, query data, files data, cookie data, and ip address).
- [x] Send Mail
- [x] many receivers to listed configured email
- [x] with include $_FILES into attachments on upload error (configurable to be included or not).
Installation
1. Import the following SQL for Mysql
DROP TABLE IF EXISTS `log`;
CREATE TABLE `log` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`type` int(11) NOT NULL,
`event` text NOT NULL,
`url` varchar(2000) NOT NULL,
`file` varchar(2000) NOT NULL,
`line` int(11) NOT NULL,
`error_type` varchar(255) NOT NULL,
`trace` text NULL,
`request_data` text NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
If you use other RDBMS, you may follow the
logtable structure above.
2. Setup your Laminas\Db\Adapter\AdapterInterface service or your Doctrine\ORM\EntityManager service config
You can use 'db' (with Laminas\Db) config or 'doctrine' (with DoctrineORMModule) config that will be transformed to be usable with Laminas\Log\Writer\Db.
<?php
// config/autoload/local.php
return [
'db' => [
'username' => 'mysqluser',
'password' => 'mysqlpassword',
'driver' => 'pdo_mysql',
'database' => 'mysqldbname',
'host' => 'mysqlhost',
'driver_options' => [
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
],
],
];
OR
<?php
// config/autoload/local.php
return [
'doctrine' => [
'connection' => [
'orm_default' => [
'driverClass' => 'Doctrine\DBAL\Driver\PDO\MySql\Driver',
'params' => [
'user' => 'mysqluser',
'password' => 'mysqlpassword',
'dbname' => 'mysqldbname',
'host' => 'mysqlhost',
'port' => '3306',
'driverOptions' => [
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
],
],
],
],
]
];
If you use other RDBMS, you may configure your own
dbordoctrineconfig.
3. Require this module uses composer.
composer require samsonasik/error-hero-module
4. Copy config
a. For Laminas Mvc application, copy error-hero-module.local.php.dist config to your local's autoload and configure it
| source | destination | |------------------------------------------------------------------------------|---------------------------------------------| | vendor/samsonasik/error-hero-module/config/error-hero-module.local.php.dist | config/autoload/error-hero-module.local.php |
Or run copy command:
cp vendor/samsonasik/error-hero-module/config/error-hero-module.local.php.dist config/autoload/error-hero-module.local.php
b. For Mezzio application, copy mezzio-error-hero-module.local.php.dist config to your local's autoload and configure it
| source | destination | |-----------------------------------------------------------------------------------------|--------------------------------------------------------| | vendor/samsonasik/error-hero-module/config/mezzio-error-hero-module.local.php.dist | config/autoload/mezzio-error-hero-module.local.php |
Or run copy command:
cp vendor/samsonasik/error-hero-module/config/mezzio-error-hero-module.local.php.dist config/autoload/mezzio-error-hero-module.local.php
When done, you can modify logger service named ErrorHeroModuleLogger and error-hero-module config in your's local config:
<?php
// config/autoload/error-hero-module.local.php or config/autoload/mezzio-error-hero-module.local.php
use Laminas\Db\Adapter\AdapterInterface;
return [
'log' => [
'ErrorHeroModuleLogger' => [
'writers' => [
[
'name' => 'db',
'options' => [
'db' => AdapterInterface::class,
'table' => 'log',
'column' => [
'timestamp' => 'date',
'priority' => 'type',
'message' => 'event',
'extra' => [
'url' => 'url',
'file' => 'file',
'line' => 'line',
'error_type' => 'error_type',
'trace' => 'trace',
'request_data' => 'request_data'
],
],
'formatter' => [
'name' => 'db',
'options' => [
'dateTimeFormat' => 'Y-m-d H:i:s',
],
],
],
],
],
],
],
'error-hero-module' => [
// it's for the enable/disable the logger functionality
'enable' => true,
// default to true, if set to true, then you can see sample:
// 1. /error-preview page ( ErrorHeroModule\Controller\ErrorPreviewController )
// 2. errorheromodule:preview command ( ErrorHeroModule\Command\Preview\ErrorPreviewConsoleCommand ) via
// php public/index.php error-preview
//
// for Mezzio ^3.0.0, the disable error-preview page is by unregister 'error-preview' from config/routes
//
//
// otherwise(false), you can't see them, eg: on production env.
'enable-error-preview-page' => true,
'display-settings' => [
// excluded php errors ( http://www.php.net/manual/en/errorfunc.constants.php )
'exclude-php-errors' => [
// can be specific error
\E_USER_DEPRECATED,
// can be specific error with specific message
[\E_WARNING, 'specific error message'],
],
// excluded exceptions
'exclude-exceptions' => [
// can be an Exception class or class extends Exception class
\App\Exception\MyException::class,
// can be specific exception with specific message
[\RuntimeException::class, 'specific exception message'],
// or specific Error class with specific message
[\Error::class, 'specific error message'],
],
// show or not error
'display_errors' => 0,
// if enable and display_errors = 0, the page will bring layout and view
'template' => [
// non laminas-view (plates, twig) for Mezzio not ne
