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 handler Zend\Stratigility\Middleware\ErrorHandler (as it is final)
  • the logging LogErrorHandler – this class is like the above one, but with added Logging support (via container)

Both error handlers have factories for an easier usage with the Container.

 

To use dot-error handler in your project run the following command: composer require dotkernel/dot-errorhandler.

 

The Config Provider

When the dot-errorhandler config provider is invoked the following configuration is returned.

[
    'dependencies' => [
        'aliases' => [
            ErrorHandlerInterface::class => ErrorHandler::class,
        ],
        'factories' => [
            LogErrorHandler::class => LogErrorHandlerFactory::class,
            ErrorHandler::class => ErrorHandlerFactory::class,
        ]
    ],
];

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 the dot-log selector
    • to select['dot_log']['default_logger'] the container key asked for is dot-log.default_logger
  • 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

The steps to configuring are the following:

  • add the Dot\ErrorHandler\ConfigProvider in the project’s config/config.php file
  • write the error handler config

 

To use the logging error handler the following config must be used.

 

config/autoload/dot-errorhandler.global.php

use Dot\ErrorHandler\ErrorHandlerInterface;
use Dot\ErrorHandler\LogErrorHandler;

return [
    'dependencies' => [
        'aliases' => [
            ErrorHandlerInterface::class => LogErrorHandler::class,
        ]
    ],
    'dot-errorhandler' => [
        'loggerEnabled' => true,
        'logger' => 'dot-log.default_logger'
    ]
];

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’ . [‘world’])

 


Looking for PHP, Laminas or Mezzio Support?

As part of the Laminas Commercial Vendor Program, Apidemia offers expert technical support and services for:

  • Modernising Legacy Applications
  • Migration from any version of Zend Framework to Laminas
  • Migration from legacy Laminas API Tools (formerly Apigility) to Dotkernel API
  • Mezzio and Laminas Consulting and Technical Audit
  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>