samsonasik / error-hero-module
A Hero for your Laminas and Mezzio application to trap php errors & exceptions
Fund package maintenance!
samsonasik
Installs: 32 525
Dependents: 1
Suggesters: 0
Security: 0
Stars: 50
Watchers: 6
Forks: 7
Open Issues: 1
Requires
- php: ^8.1
- laminas/laminas-db: ^2.16.2
- laminas/laminas-diactoros: ^2.22
- laminas/laminas-log: ^2.16
- laminas/laminas-mail: ^2.21
- laminas/laminas-psr7bridge: ^1.8
- laminas/laminas-uri: ^2.10
- psr/container: ^1.1.2 || 2.0
- samsonasik/array-lookup: ^1.0
- seld/jsonlint: ^1.9
- webmozart/assert: ^1.11
Requires (Dev)
- aura/di: ^3.4 || ^4.2.1
- doctrine/dbal: ^3.5.1
- doctrine/doctrine-orm-module: ^4.2.1 || ^5.0
- kahlan/kahlan: ^5.2.2
- laminas/laminas-cli: ^1.7
- laminas/laminas-coding-standard: ^2.4
- laminas/laminas-form: ^2.16 || ^3.8
- laminas/laminas-mvc: ^3.6
- laminas/laminas-servicemanager: ^3.20
- mezzio/mezzio: ^3.14
- mezzio/mezzio-laminasviewrenderer: ^2.10
- php-coveralls/php-coveralls: ^2.5.3
- phpstan/phpstan: ^1.9.3
- phpstan/phpstan-webmozart-assert: ^1.2.2
- pimple/pimple: ^3.5
- rector/rector: dev-main
- symfony/dependency-injection: ^5.4.16
Suggests
- mezzio/mezzio-laminasviewrenderer: ^2.0 to be able to use laminas-view template engine in mezzio ^3.0
- mezzio/mezzio-platesrenderer: ^2.0 to be able to use plates template engine in mezzio ^3.0
- mezzio/mezzio-twigrenderer: ^2.0 to be able to use twig template engine in mezzio ^3.0
Conflicts
- doctrine/dbal: <3.0
- doctrine/doctrine-orm-module: <4.1
- laminas/laminas-mvc: <3.2 >=4.0
- laminas/laminas-mvc-console: *
- laminas/laminas-servicemanager: <3.10
- mezzio/mezzio: <3.3 >=4.0
- 5.1.0
- 5.0.1
- 5.0.0
- 4.x-dev
- 4.2.1
- 4.2.0
- 4.1.1
- 4.1.0
- 4.0.3
-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- 1.4.1
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.0.3
- 0.0.2
- 0.0.1
This package is auto-updated.
Last update: 2024-12-14 04:35:31 UTC
README
This is README for version ^5.0 which only support Laminas Mvc version 3 and Mezzio version 3 with php ^8.1.
For version ^4.0, you can read at version 4 readme which only support Laminas Mvc version 3 and Mezzio version 3 with php ^8.0.
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
- Save to DB with Db Writer Adapter.
- Log Exception (dispatch.error and render.error) and PHP Errors in all events process.
- Support excludes PHP E_* Error (eg: exclude E_USER_DEPRECATED or specific E_USER_DEPRECATED with specific message) in config settings.
- Support excludes PHP Exception (eg: Exception class or classes that extends it or specific exception class with specific message) in config settings.
- Handle only once log error for same error per configured time range.
- Set default page (web access) or default message (console access) for error if configured 'display_errors' = 0.
- Set default content when request is XMLHttpRequest via 'ajax' configuration.
- Set default content when there is no template service via 'no_template' configuration (Mezzio 3).
- Provide request information ( http method, raw data, body data, query data, files data, cookie data, and ip address).
- Send Mail
- many receivers to listed configured email
- 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
log
table 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
db
ordoctrine
config.
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
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
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 need a layout definition // as layout defined in the view 'layout' => 'layout/layout', 'view' => 'error-hero-module/error-default' ], // for Mezzio, when container doesn't has \Mezzio\Template\TemplateRendererInterface service // if enable, and display_errors = 0, then show a message under no_template config 'no_template' => [ 'message' => <<<json { "type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html", "title": "Internal Server Error", "status": 500, "detail": "We have encountered a problem and we can not fulfill your request. An error report has been generated and sent to the support team and someone will attend to this problem urgently. Please try again later. Thank you for your patience." } json ], // if enable and display_errors = 0, the console will bring message for laminas-mvc 'console' => [ 'message' => 'We have encountered a problem and we can not fulfill your request. An error report has been generated and sent to the support team and someone will attend to this problem urgently. Please try again later. Thank you for your patience.', ], // if enable, display_errors = 0, and request XMLHttpRequest // on this case, the "template" key will be ignored. 'ajax' => [ 'message' => <<<json { "type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html", "title": "Internal Server Error", "status": 500, "detail": "We have encountered a problem and we can not fulfill your request. An error report has been generated and sent to the support team and someone will attend to this problem urgently. Please try again later. Thank you for your patience." } json ], ], 'logging-settings' => [ // time range for same error, file, line, url, message to be re-logged // in seconds range, 86400 means 1 day 'same-error-log-time-range' => 86400, ], 'email-notification-settings' => [ // set to true to activate email notification on log error event 'enable' => false, // Laminas\Mail\Message instance registered at service manager 'mail-message' => 'YourMailMessageService', // Laminas\Mail\Transport\TransportInterface instance registered at service manager 'mail-transport' => 'YourMailTransportService', // email sender 'email-from' => 'Sender Name <sender@host.com>', // to include or not $_FILES on send mail 'include-files-to-attachments' => true, 'email-to-send' => [ 'developer1@foo.com', 'developer2@foo.com', ], ], ], // ... ];
5. Lastly, enable it
a. For Laminas Mvc application
// config/modules.config.php or config/application.config.php return [ 'Application', 'ErrorHeroModule', // <-- register here ],
b. For Mezzio application
For laminas-mezzio-skeleton ^3.0.0, you need to open config/pipeline.php
and add the ErrorHeroModule\Middleware\Mezzio::class
middleware after default ErrorHandler::class
registration:
$app->pipe(ErrorHandler::class); $app->pipe(ErrorHeroModule\Middleware\Mezzio::class); // here
and also add error-preview
routes in config/routes.php
(optional) :
// for use laminas-router $app->get('/error-preview[/:action]', ErrorHeroModule\Middleware\Routed\Preview\ErrorPreviewAction::class, 'error-preview'); // for use FastRoute $app->get('/error-preview[/{action}]', ErrorHeroModule\Middleware\Routed\Preview\ErrorPreviewAction::class, 'error-preview');
to enable error preview page. To disable error preview page, just remove it from routes.
Give it a try!
Web Access
You will get the following page if display_errors config is 0:
Console Access
You can use this module in
laminas-cli
, you can install:
composer require laminas/laminas-cli --sort-packages
then you can see the error-preview console:
You will get the following page if display_errors config is 0:
You can use the error handling in your console application, by extends BaseLoggingCommand
, like below:
namespace Application\Command; use ErrorHeroModule\Command\BaseLoggingCommand; use Exception; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; final class HelloWorld extends BaseLoggingCommand { protected function execute(InputInterface $input, OutputInterface $output): int { throw new Exception('some exception logged to DB'); } }
and register to your services like in the documentation.
For production env, you can disable error-preview sample page with set
['error-hero-module']['enable-error-preview-page']
to false.
Contributing
Contributions are very welcome. Please read CONTRIBUTING.md