Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
gaowei committed May 27, 2022
1 parent fee020b commit b7abad5
Show file tree
Hide file tree
Showing 14 changed files with 448 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false

[*.{vue,js,scss}]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
* text=auto

/tests export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.scrutinizer.yml export-ignore
.travis.yml export-ignore
phpunit.php export-ignore
phpunit.xml.dist export-ignore
phpunit.xml export-ignore
.php_cs export-ignore
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.idea
*.DS_Store
/vendor
/coverage
sftp-config.json
composer.lock
.subsplit
.php_cs.cache
log
.env
27 changes: 27 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
$header = <<<EOF
This file is part of the gaowei-space/error-handler.
(c) gaowei <[email protected]>
This source file is subject to the MIT license that is bundled.
EOF;

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules(array(
'@Symfony' => true,
'header_comment' => array('header' => $header),
'array_syntax' => array('syntax' => 'short'),
'ordered_imports' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'php_unit_construct' => true,
'php_unit_strict' => true,
))
->setFinder(
PhpCsFixer\Finder::create()
->exclude('vendor')
->in(__DIR__)
)
;
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<h1 align="center"> 🌈 error-handler </h1>

<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.

## Installing

```shell
$ composer require gaowei-space/error-handler -vvv
```

## Usage

### 1. sentry
```php
$options = [
'report_level' => E_ALL,
'display_errors' => true, // prite errors
'handler' => 'sentry', // sentry or logger
'sentry_options' => [
'dsn' => 'http:https://[email protected]/3', // sentry website dsn
'environment' => 'test',
'sample_rate' => 1, // report rate, float range 0-1
'http_timeout' => 0.5,
],
];
ErrorHandler::init($options);
```
- [sentry doc](https://docs.sentry.io/platforms/php/)
- [sentry options](https://docs.sentry.io/platforms/php/configuration/options/)


### 2. monolog
```php
$logger = new Logger("php_errors");
$log_name = sprintf('php_errors_%s.log', date('Ymd'));
$logger->pushHandler(new StreamHandler("./log/" . $log_name, Logger::DEBUG, true, 0666));

$options = [
'report_level' => E_ALL, // error report level
'display_errors' => true, // prite errors
'handler' => 'logger', // sentry or logger
'logger' => $logger, // monolog loogger
];
ErrorHandler::init($options);
```

## Test

### 1. cp env file
```
cp examples/.env.example examples/.env
```
### 2. edit env file
```
SENTRY_DSN = "http:https://[email protected]/3"
```
### 3. run test code
```php
php index.php examples/ErrorHandlerTest.php
```

## Contributing

You can contribute in one of three ways:

1. File bug reports using the [issue tracker](https://github.com/gaowei-space/error-handler/issues).
2. Answer questions or fix bugs on the [issue tracker](https://github.com/gaowei-space/error-handler/issues).

_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
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "gaowei-space\/error-handler",
"description": "ErrorHandler for catch all php running time errors.",
"license": "MIT",
"authors": [
{
"name": "gaowei",
"email": "[email protected]"
}
],
"require": {
"sentry/sdk": "^3.2",
"psr/log": "^1.1"
},
"autoload": {
"psr-4": {
"GaoweiSpace\\ErrorHandler\\": "src"
}
},
"require-dev": {
"monolog/monolog": "^2.6",
"vlucas/phpdotenv": "^5.4"
}
}
1 change: 1 addition & 0 deletions examples/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SENTRY_DSN = "http:https://[email protected]/3"
5 changes: 5 additions & 0 deletions examples/BaseExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
require __DIR__ . '/../vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
64 changes: 64 additions & 0 deletions examples/ErrorHandlerExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace GaoweiSpace\ErrorHandler\Examples;

use GaoweiSpace\ErrorHandler\ErrorHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

require __DIR__ . '/BaseExample.php';

class ErrorHandlerExample
{
public function testInitLogger($code)
{
$logger = new Logger("errors");
$log_name = sprintf('%s/log/errors_%s.log', __DIR__, date('Ymd'));
$logger->pushHandler(new StreamHandler($log_name, Logger::DEBUG, true, 0666));

$options = [
'report_level' => E_ALL, // error report level
'display_errors' => true, // prite errors
'handler' => 'logger', // sentry or logger
'logger' => $logger, // monolog loogger
];
ErrorHandler::init($options);

$this->_test_sentry($code);
}

public function testInitForSentry($code)
{
$options = [
'report_level' => E_ALL,
'display_errors' => true, // prite errors
'handler' => 'sentry', // sentry or logger
'sentry_options' => [
'dsn' => $_ENV['SENTRY_DSN'], // eg: http:https://[email protected]/3
'environment' => 'test',
'sample_rate' => 1, // report rate, float range 0-1
'http_timeout' => 0.5,
],
];
ErrorHandler::init($options);

$this->_test_sentry($code);
}

private function _test_sentry($code)
{
switch ($code) {
case 1:
$this->testError2(); // E_ERROR
break;
case 2:
explode('ddd', []); // E_ERROR
break;
case 3:
echo $dd; // E_NOTICE
break;
}

print_r("\n\n####### test done ####### \n");
}
}
8 changes: 8 additions & 0 deletions examples/Monolog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use GaoweiSpace\ErrorHandler\Examples\ErrorHandlerExample;

require __DIR__ . '/ErrorHandlerExample.php';

$test = new ErrorHandlerExample();
$test->testInitLogger(3);
8 changes: 8 additions & 0 deletions examples/Sentry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use GaoweiSpace\ErrorHandler\Examples\ErrorHandlerExample;

require __DIR__ . '/ErrorHandlerExample.php';

$test = new ErrorHandlerExample();
$test->testInitForSentry(1);
21 changes: 21 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
Empty file added src/.gitkeep
Empty file.
Loading

0 comments on commit b7abad5

Please sign in to comment.