Skip to content

Commit

Permalink
Merge pull request #32 from TahsinGokalp/development
Browse files Browse the repository at this point in the history
Merge development to master
  • Loading branch information
TahsinGokalp committed Mar 22, 2023
2 parents 1e58d21 + 1c445f3 commit 6bd5a26
Show file tree
Hide file tree
Showing 28 changed files with 277 additions and 132 deletions.
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
0b4771d854aa254e7e618e02e82d8f909aa3a003
3bd1a83d82ecd2ba335c084e85a837aa81bdf5ab
66cd5bd2cfb3463dfd7da224a15b043d0173a10e
602b3bf2418f7402e994cd95618b687179095413
3aba90e94c459bc1e87b456b36822c8d1b53b64b
c232ff39eeccb9a5c578d839e2264d44e2ff608d
34 changes: 2 additions & 32 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,6 @@ And adjust config file (`config/lett.php`) with your desired settings.

Note: by default only production environments will report errors. To modify this edit your Let configuration.

## Installation on lumen
You can install the package through Composer.
```bash
composer require tahsingokalp/lett
```

Copy the config file (`lett.php`) to lumen config directory.
```bash
php -r "file_exists('config/') || mkdir('config/'); copy('vendor/tahsingokalp/lett/config/lett.php', 'config/lett.php');"
```
And adjust config file (`config/lett.php`) with your desired settings.

In `bootstrap/app.php` you will need to:
- Uncomment this line:
```php
$app->withFacades();
```
- Register the lett config file:
```php
$app->configure('lett');
```
- Register lett service provider:
```php
$app->register(Lett\LettServiceProvider::class);
```

## Configuration variables
All that is left to do is to define two env configuration variables.
```
Expand All @@ -64,7 +38,7 @@ LETT_PROJECT_KEY=

`LETT_PROJECT_KEY` is your project API key which you've received when creating a project.

Install lett-tracker to your host and get the variables
Install lett-tracker to your host and get the variables.

## Reporting unhandled exceptions
You can use lett as a log-channel by adding the following config to the `channels` section in `config/logging.php`:
Expand All @@ -87,11 +61,7 @@ After that you can add it to the stack section:
],
```

PS: If you're using lumen, it could be that you don't have the `logging.php` file. So, you can use default logging file from
framework core and make changes above.
```bash
php -r "file_exists('config/') || mkdir('config/'); copy('vendor/laravel/lumen-framework/config/logging.php', 'config/logging.php');"
```
Now all unhandled exceptions will be reported to Lett.

## License
The Let package is open source software licensed under the [license MIT](http://opensource.org/licenses/MIT)
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"php": "^8.0",
"guzzlehttp/guzzle": "^7.5",
"illuminate/contracts": "^9.0 || ^10.0",
"illuminate/support": "^9.0 || ^10.0"
"illuminate/support": "^9.0 || ^10.0",
"tahsingokalp/lett-constants": "^1.0"
},
"require-dev": {
"roave/security-advisories": "dev-latest",
Expand Down
10 changes: 10 additions & 0 deletions config/lett.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@

'sleep' => env('LETT_SLEEP', 60),

/*
|--------------------------------------------------------------------------
| Client timeout
|--------------------------------------------------------------------------
|
| This setting determines the timeout for the client.
|
*/

'timeout' => env('LETT_CLIENT_TIMEOUT', 15),

/*
Expand Down Expand Up @@ -130,6 +139,7 @@
| Never turn SSL verification off on production instances
|
*/

'verify_ssl' => env('LETT_VERIFY_SSL', true),

];
13 changes: 13 additions & 0 deletions lang/en/lett.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [
'found_api_key' => 'Found API key',
'could_not_find_your_api_key_set_this_in_your_env' => 'Could not find your API key, set this in your .env',
'found_project_key' => 'Found project key',
'could_not_find_your_project_key_set_this_in_your_env' => 'Could not find your project key, set this in your .env',
'correct_environment_found_environment' => 'Correct environment found (:environment)',
'environment_environment_not_allowed_to_send_errors_to_lett_set_this_in_your_config' => 'Environment (:environment) not allowed to send errors to Lett, set this in your config',
'sent_exception_to_lett' => 'Sent exception to lett',
'failed_to_send_exception_to_lett' => 'Failed to send exception to lett',
'this_is_a_test_exception_from_the_lett_console' => 'This is a test exception from the Lett console',
];
13 changes: 13 additions & 0 deletions lang/tr/lett.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [
'found_api_key' => 'API anahtarı bulundu',
'could_not_find_your_api_key_set_this_in_your_env' => 'API anahtarı bulunamadı, .env dosyasından ekleyebilirsiniz',
'found_project_key' => 'Proje anahtarı bulundu',
'could_not_find_your_project_key_set_this_in_your_env' => 'Proje anahtarı bulunamadı, .env dosyasından ekleyebilirsiniz',
'correct_environment_found_environment' => 'Geçerli ortam bulundu (:environment)',
'environment_environment_not_allowed_to_send_errors_to_lett_set_this_in_your_config' => '(:environment) ortamı Lett\'e hata gönderimi yapamaz, config dosyanızdan düzenleyebilirsiniz',
'sent_exception_to_lett' => 'Exception Lett\'e gönderildi',
'failed_to_send_exception_to_lett' => 'Exception Lett\'e gönderilemedi',
'this_is_a_test_exception_from_the_lett_console' => 'Bu exception Lett konsolundan oluşturulmuştur',
];
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ includes:
- phpstan-baseline.neon

parameters:
level: 6
level: 8
paths:
- src
- config
Expand Down
12 changes: 5 additions & 7 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,31 @@
namespace TahsinGokalp\Lett;

use Exception;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
use Psr\Http\Message\ResponseInterface;

class Client
{
protected PendingRequest|ClientInterface $client;
protected PendingRequest $client;

protected string $login;

protected string $project;

private int $timeout;

public function __construct(string $login, string $project, PendingRequest|ClientInterface $client = null)
public function __construct(string $login, string $project, PendingRequest $client = null)
{
$this->login = $login;
$this->project = $project;
$this->timeout = config('lett.timeout');
$this->client = $client ?: Http::timeout($this->timeout);
}

public function report(array $exception): PromiseInterface|ResponseInterface|null
public function report(array $exception): ?ResponseInterface
{
try {
return $this->getHttpClient()
Expand Down Expand Up @@ -60,14 +58,14 @@ function ($client) {
}
}

public function getHttpClient(): PendingRequest|ClientInterface
public function getHttpClient(): PendingRequest
{
return $this->client;
}

public function setHttpClient(\GuzzleHttp\Client $client): self
{
$this->client = Http::timeout($this->timeout)->setClient($client)->buildClient();
$this->client = Http::timeout($this->timeout)->setClient($client);

return $this;
}
Expand Down
24 changes: 18 additions & 6 deletions src/Commands/DoctorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
namespace TahsinGokalp\Lett\Commands;

use Illuminate\Console\Command;
use TahsinGokalp\Lett\Events\ApiKeyNotFound;
use TahsinGokalp\Lett\Events\EnvironmentNotFound;
use TahsinGokalp\Lett\Events\FoundApiKey;
use TahsinGokalp\Lett\Events\FoundEnvironment;
use TahsinGokalp\Lett\Events\FoundProjectKey;
use TahsinGokalp\Lett\Events\ProjectKeyNotFound;

class DoctorCommand extends Command
{
Expand All @@ -13,21 +19,27 @@ class DoctorCommand extends Command
public function handle(): int
{
if (config('lett.login_key')) {
$this->info('✓ [Lett] Found login key');
$this->info(trans('lett::lett.found_api_key'));
event(new FoundApiKey);
} else {
$this->error('✗ [Lett] Could not find your login key, set this in your .env');
$this->error(trans('lett::lett.could_not_find_api_key'));
event(new ApiKeyNotFound);
}

if (config('lett.project_key')) {
$this->info('✓ [Lett] Found project key');
$this->info(trans('lett::lett.found_project_key'));
event(new FoundProjectKey);
} else {
$this->error('✗ [Lett] Could not find your project key, set this in your .env');
$this->error(trans('lett::lett.could_not_find_project_key'));
event(new ProjectKeyNotFound);
}

if (in_array((string) config('app.env'), config('lett.environments'), true)) {
$this->info('✓ [Lett] Correct environment found (' . config('app.env') . ')');
$this->info(trans('lett::lett.correct_environment_found_environment', ['environment' => config('app.env')]));
event(new FoundEnvironment(config('app.env')));
} else {
$this->error('✗ [Lett] Environment (' . config('app.env') . ') not allowed to send errors to Lett, set this in your config');
$this->error(trans('lett::lett.environment_environment_not_allowed_to_send_errors_to_lett_set_this_in_your_config', ['environment' => config('app.env')]));
event(new EnvironmentNotFound(config('app.env')));
}

return self::SUCCESS;
Expand Down
25 changes: 17 additions & 8 deletions src/Commands/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
use Exception;
use Illuminate\Console\Command;
use RuntimeException;
use TahsinGokalp\Lett\Events\FailedToSentExceptionToLett;
use TahsinGokalp\Lett\Events\SentExceptionToLett;
use TahsinGokalp\Lett\Lett;
use TahsinGokalp\LettConstants\Enum\ApiResponseCodeEnum;
use Throwable;

class TestCommand extends Command
{
Expand All @@ -24,28 +28,33 @@ public function handle(): int
);

if (is_null($response)) {
$this->info('✓ [Lett] Sent exception to lett!');
$this->info(trans('lett::lett.sent_exception_to_lett'));
event(new SentExceptionToLett);
} elseif (! is_bool($response)) {
$response = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
if ($response[0] === 'OK') {
$this->info('✓ [Lett] Sent exception to lett!');
if ((int) $response['code'] === ApiResponseCodeEnum::Success->value) {
$this->info(trans('lett::lett.sent_exception_to_lett'));
event(new SentExceptionToLett);
} else {
$this->error('✗ [Lett] Failed to send exception to lett');
$this->error(trans('lett::lett.failed_to_send_exception_to_lett'));
event(new FailedToSentExceptionToLett);
}
} else {
$this->error('✗ [Lett] Failed to send exception to lett');
$this->error(trans('lett::lett.failed_to_send_exception_to_lett'));
event(new FailedToSentExceptionToLett);
}
} catch (Exception $ex) {
$this->error("✗ [Lett] Failed to send {$ex->getMessage()}");
$this->error(trans('lett::lett.failed_to_send_exception_to_lett') . " {$ex->getMessage()}");
event(new FailedToSentExceptionToLett);
}

return self::SUCCESS;
}

public function generateException(): ?Exception
public function generateException(): Throwable
{
try {
throw new RuntimeException('This is a test exception from the Lett console');
throw new RuntimeException(trans('lett::lett.this_is_a_test_exception_from_the_lett_console'));
} catch (RuntimeException $ex) {
return $ex;
}
Expand Down
7 changes: 7 additions & 0 deletions src/Events/ApiKeyNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace TahsinGokalp\Lett\Events;

class ApiKeyNotFound
{
}
10 changes: 10 additions & 0 deletions src/Events/EnvironmentNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace TahsinGokalp\Lett\Events;

class EnvironmentNotFound
{
public function __construct(public string $environment)
{
}
}
7 changes: 7 additions & 0 deletions src/Events/FailedToSentExceptionToLett.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace TahsinGokalp\Lett\Events;

class FailedToSentExceptionToLett
{
}
7 changes: 7 additions & 0 deletions src/Events/FoundApiKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace TahsinGokalp\Lett\Events;

class FoundApiKey
{
}
10 changes: 10 additions & 0 deletions src/Events/FoundEnvironment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace TahsinGokalp\Lett\Events;

class FoundEnvironment
{
public function __construct(public string $environment)
{
}
}
7 changes: 7 additions & 0 deletions src/Events/FoundProjectKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace TahsinGokalp\Lett\Events;

class FoundProjectKey
{
}
7 changes: 7 additions & 0 deletions src/Events/JsonDecodeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace TahsinGokalp\Lett\Events;

class JsonDecodeException
{
}
7 changes: 7 additions & 0 deletions src/Events/ProjectKeyNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace TahsinGokalp\Lett\Events;

class ProjectKeyNotFound
{
}
7 changes: 7 additions & 0 deletions src/Events/SentExceptionToLett.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace TahsinGokalp\Lett\Events;

class SentExceptionToLett
{
}
7 changes: 7 additions & 0 deletions src/Events/SkipEnvironment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace TahsinGokalp\Lett\Events;

class SkipEnvironment
{
}
7 changes: 7 additions & 0 deletions src/Events/SkipException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace TahsinGokalp\Lett\Events;

class SkipException
{
}
7 changes: 7 additions & 0 deletions src/Events/SleepingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace TahsinGokalp\Lett\Events;

class SleepingException
{
}

0 comments on commit 6bd5a26

Please sign in to comment.