Skip to content

Latest commit

 

History

History
118 lines (95 loc) · 3.64 KB

README_EN.md

File metadata and controls

118 lines (95 loc) · 3.64 KB

🎯 Error-Handler

中文 | English

GitHub branch checks state Latest Release StyleCI build status PHP Version License

Error-Handler is used to catch all php runtime errors and supports reporting to monolog or sentry.

Compared with the official instantiation method of sentry, it consumes less server resources because it instantiates sentry and reports the exception only when an exception is caught, which is why this package was born.

Installing

composer require gaowei-space/error-handler

Usage

1. sentry

$options = [
    'report_level'   => E_ALL,
    'display_errors' => true,
    'handler'        => 'sentry', // sentry or logger
    'sentry_options' => [
        'dsn'          => 'http://0c2f5aaca4a14eaf958a050157843090@sentry.yoursentrysite.com/3',
        'environment'  => 'test',
        'sample_rate'  => 1,
        'http_timeout' => 0.5,
    ],
    'scope_user' => [
        'id'       => 10,
        'username' => 'dan',
    ],
    'scope_tags' => [
        'game_role' => 'rookie',
    ],
];
ErrorHandler::init($options);

2. monolog

$logger = new Logger("errors");
$logger->pushHandler(new StreamHandler(sprintf('%s/log/errors_%s.log', __DIR__, date('Ymd')), Logger::DEBUG, true, 0666));

$options = [
    'report_level'   => E_ALL,
    'display_errors' => true,
    'handler'        => 'logger', // sentry or logger
    'logger'         => $logger,
];
ErrorHandler::init($options);

Test

1. install develop packages

composer require gaowei-space/error-handler --dev

2. cp env file

cp examples/.env.example examples/.env

3. edit env file

SENTRY_DSN = "http://0c2f5aaca4a14eaf958a050157843090@sentry.yoursentrysite.com/3"

4. run examples

// monolog
php examples/Monolog.php
// sentry
php examples/Sentry.php

Sentry initialization time-consuming comparison

$options = [
    'report_level'   => E_ALL, // error report level
    'display_errors' => true, // prite errors
    'handler'        => 'sentry', // sentry or logger
    'sentry_options' => [
        'dsn'          => 'http://0c2f5aaca4a14eaf958a050157843090@sentry.yoursentrysite.com/3', // sentry website dsn
        'environment'  => 'test',
        'sample_rate'  => 1, // report rate, float range 0-1
        'http_timeout' => 0.5,
    ],
];

Self:
ErrorHandler::init($options); // time consuming: 0.001616

Sentry:
\Sentry\init($options['sentry_options']); // time consuming: 0.146600

Contributing

You can contribute in one of three ways:

  1. File bug reports using the issue tracker.
  2. Answer questions or fix bugs on the issue tracker.

The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines.

License

MIT