Skip to content

Commit

Permalink
fix: 🐛 Add option to ignore thrown exceptions using .env variable (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xinecraft authored Feb 28, 2023
1 parent 472ee85 commit 01ed365
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ You must add a new channel to your `config/logging.php` file:
'via' => MarvinLabs\DiscordLogger\Logger::class,
'level' => 'debug',
'url' => env('LOG_DISCORD_WEBHOOK_URL'),
'ignore_exceptions' => env('LOG_DISCORD_IGNORE_EXCEPTIONS', false),
],
];
```
Expand Down
22 changes: 18 additions & 4 deletions src/DiscordLogger/LogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use MarvinLabs\DiscordLogger\Converters\SimpleRecordConverter;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger as Monolog;
use Monolog\LogRecord;
use RuntimeException;
use function class_implements;

Expand All @@ -20,20 +21,33 @@ class LogHandler extends AbstractProcessingHandler
/** @var \MarvinLabs\DiscordLogger\Contracts\RecordToMessage */
private $recordToMessage;

/** @var boolean */
private $ignoreExceptions;

/** @throws \Illuminate\Contracts\Container\BindingResolutionException */
public function __construct(Container $container, Repository $config, array $channelConfig)
{
parent::__construct(Monolog::toMonologLevel($channelConfig['level'] ?? Monolog::DEBUG));

$this->discord = $container->make(DiscordWebHook::class, ['url' => $channelConfig['url']]);
$this->recordToMessage = $this->createRecordConverter($container, $config);

$this->ignoreExceptions = $channelConfig['ignore_exceptions'] ?? false;
}

public function write(array $record): void
public function write(array|LogRecord $record): void
{
foreach($this->recordToMessage->buildMessages($record) as $message)
{
$this->discord->send($message);
if ($record instanceof LogRecord) {
$record = $record->toArray();
}
foreach($this->recordToMessage->buildMessages($record) as $message) {
try {
$this->discord->send($message);
} catch (\Exception $e) {
if (!$this->ignoreExceptions) {
throw $e;
}
}
}
}

Expand Down

0 comments on commit 01ed365

Please sign in to comment.