middlewares/reporting-logger

A middleware to log client-side reportings

v1.1.0 2018-08-04 10:41 UTC

README

Latest Version on Packagist Software License Build Status Quality Score Total Downloads

Middleware to log server-side reportings, like CSP messages or any javascript error. More info about how collect javascript errors. You may need also the middlewares/payload (or any other middleware with the same purpose) to parse the json of the body.

Requirements

Installation

This package is installable and autoloadable via Composer as middlewares/reporting-logger.

composer require middlewares/reporting-logger

Example

Register a error handler in your javascript code:

window.onerror = function (message, file, lineNo, colNo) {
    const error = { message, file, lineNo, colNo };
    const blob = new Blob([ JSON.stringify(error) ], { type: 'application/json' });

    navigator.sendBeacon('/report', blob);
}
$dispatcher = new Dispatcher([
    new Middlewares\JsonPayload(),
    new Middlewares\ReportingLogger($logger)
]);

$response = $dispatcher->dispatch(new ServerRequest());

Options

__construct(Psr\Log\LoggerInterface $log)

A PSR logger implementation used to save the logs.

path(string $path)

The path where the logs will be reported. By default is /report.

message(string $message)

The message used to save the logs. You can use the strings %{varname} to generate dinamic messages using the reporting data. For example:

$dispatcher = new Dispatcher([
    new Middlewares\JsonPayload(),
    (new Middlewares\ReportingLogger($logger))
        ->message('New error: "%{message}" in line %{lineNumber}, column %{colNumber}')
]);

responseFactory(Psr\Http\Message\ResponseFactoryInterface $responseFactory)

A PSR-17 factory to create the responses.

Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.

The MIT License (MIT). Please see LICENSE for more information.