Skip to content

Commit

Permalink
opt: add tag
Browse files Browse the repository at this point in the history
  • Loading branch information
gaowei committed May 28, 2022
1 parent e544ed4 commit 74ea995
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 13 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<h1 align="center"> 🌈 error-handler </h1>

![GitHub branch checks state](https://img.shields.io/github/checks-status/gaowei-space/error-handler/main)
[![Latest Release](https://img.shields.io/github/v/release/gaowei-space/error-handler)](https://github.com/gaowei-space/error-handler/releases)
![StyleCI build status](https://github.styleci.io/repos/496875473/shield)
[![PHP Version](https://img.shields.io/packagist/php-v/gaowei-space/error-handler)](https://www.php.net/)
[![License](https://img.shields.io/github/license/gaowei-space/error-handler)](https://github.com/gaowei-space/error-handler/LICENSE)

<p> ErrorHandler is used to catch all php runtime errors and supports reporting to monolog or sentry. </p>

> 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.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
}
],
"require": {
"php": "^7.2|^8.0",
"sentry/sdk": "^3.2",
"psr/log": "^1.1",
"monolog/monolog": "^2.6"
Expand Down
1 change: 1 addition & 0 deletions examples/BaseExample.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
Expand Down
2 changes: 1 addition & 1 deletion examples/ErrorHandlerExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ErrorHandlerExample
{
public function testInitLogger($code)
{
$logger = new Logger("errors");
$logger = new Logger('errors');
$logger->pushHandler(new StreamHandler(sprintf('%s/log/errors_%s.log', __DIR__, date('Ymd')), Logger::DEBUG, true, 0666));

$options = [
Expand Down
2 changes: 1 addition & 1 deletion examples/Monolog.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

require __DIR__ . '/ErrorHandlerExample.php';

$test = new ErrorHandlerExample();
$test = new ErrorHandlerExample();
$test->testInitLogger(3);
22 changes: 11 additions & 11 deletions src/ErrorHandler.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php

/**
* ErrorHandler is used to catch all php runtime errors and supports reporting to monolog or sentry
* ErrorHandler is used to catch all php runtime errors and supports reporting to monolog or sentry.
*
* @author gaowei <[email protected]>
* @date 2022-05-26
*
* @copyright gaowei
*
*/

namespace GaoweiSpace\ErrorHandler;
Expand All @@ -17,18 +16,18 @@
class ErrorHandler
{
public $logger;
public $handler = 'logger'; // logger | sentry
public $handler = 'logger'; // logger | sentry
public $display_errors = false;
public $sentry_options = [];
public $report_level = E_ALL;
public $report_level = E_ALL;

public function __construct(array $options = [])
{
$this->display_errors = $options['display_errors'];
$this->handler = $options['handler'];
$this->logger = $options['logger'];
$this->handler = $options['handler'];
$this->logger = $options['logger'];
$this->sentry_options = $options['sentry_options'];
$this->report_level = $options['report_level'];
$this->report_level = $options['report_level'];
}

public static function init(array $options = [])
Expand Down Expand Up @@ -91,9 +90,9 @@ public function handleException(\Throwable $exception)

private function _register()
{
register_shutdown_function(array($this, 'handleFatalError'));
set_error_handler(array($this, 'handleError'));
set_exception_handler(array($this, 'handleException'));
register_shutdown_function([$this, 'handleFatalError']);
set_error_handler([$this, 'handleError']);
set_exception_handler([$this, 'handleException']);
}

private function _log($type, $message)
Expand All @@ -109,7 +108,7 @@ private function _log($type, $message)
}

if ($this->display_errors) {
print "{$level}: {$message}";
echo "{$level}: {$message}";
}
}

Expand All @@ -130,6 +129,7 @@ private function _sentry(\Throwable $exception)
private function _formatMessage($message, $file, $line, $trace = '')
{
$message = "{$file}#{$line}: {$message}";

return <<<MSG
$message
$trace
Expand Down

0 comments on commit 74ea995

Please sign in to comment.