2017 © Pedro Peláez
 

library reporting-logger

A middleware to log client-side reportings

image

middlewares/reporting-logger

A middleware to log client-side reportings

  • Sunday, June 10, 2018
  • by oscarotero
  • Repository
  • 1 Watchers
  • 3 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

middlewares/reporting-logger

Latest Version on Packagist ![Software License][ico-license] Testing ![Total Downloads][ico-downloads], (*1)

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., (*2)

Requirements

Installation

This package is installable and autoloadable via Composer as middlewares/reporting-logger., (*3)

composer require middlewares/reporting-logger

Example

Register a error handler in your javascript code:, (*4)

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::run([
    new Middlewares\JsonPayload(),
    new Middlewares\ReportingLogger($logger)
]);

Usage

You need a Psr\Log\LoggerInterface instance to handle the logs, for example, monolog, (*5)

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$logger = new Logger('access');
$logger->pushHandler(new StreamHandler('data/logs.txt'));

Dispatcher::run([
    new Middlewares\ReportingLogger($logger)
]);

Optionally, you can provide a Psr\Http\Message\ResponseFactoryInterface as the second argument, that will be used to create the responses returned after handle the reporting. If it's not defined, Middleware\Utils\Factory will be used to detect it automatically., (*6)

$responseFactory = new MyOwnResponseFactory();

$reporting = new Middlewares\ReportingLogger($logger, $responseFactory);

path

The uri path where the logs will be reported. By default is /report., (*7)

// In front-end: send the error to "/log-reporting" path
navigator.sendBeacon('/log-reporting', error);
// In back-end: configure to collect all reportings send to the same path
$reporting = (new Middlewares\ReportingLogger($logger))->path('/log-reporting')

message

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

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

Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details., (*9)

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

The Versions