-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
gaowei
committed
Aug 1, 2022
1 parent
577c317
commit b9786e6
Showing
1 changed file
with
13 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +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]> | ||
* | ||
* @version 1.0.2 | ||
* | ||
* @copyright gaowei | ||
* @created_at 2022-05-26 | ||
* @updated_at 2022-08-01 | ||
|
@@ -18,18 +20,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 = []) | ||
|
@@ -45,6 +47,7 @@ public static function init(array $options = []) | |
public static function create(array $options = []) | ||
{ | ||
self::_setDefaultOptions($options); | ||
|
||
return new static($options); | ||
} | ||
|
||
|
@@ -104,9 +107,9 @@ public function captureMessage(string $message, string $level) | |
|
||
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) | ||
|
@@ -116,7 +119,7 @@ private function _log($type, $message) | |
$this->_logWrite($message, $level); | ||
|
||
if ($this->display_errors) { | ||
print "{$level}: {$message}"; | ||
echo "{$level}: {$message}"; | ||
} | ||
} | ||
|
||
|