Skip to content

Commit 01ed365

Browse files
authored
fix: 🐛 Add option to ignore thrown exceptions using .env variable (#35)
1 parent 472ee85 commit 01ed365

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ You must add a new channel to your `config/logging.php` file:
5151
'via' => MarvinLabs\DiscordLogger\Logger::class,
5252
'level' => 'debug',
5353
'url' => env('LOG_DISCORD_WEBHOOK_URL'),
54+
'ignore_exceptions' => env('LOG_DISCORD_IGNORE_EXCEPTIONS', false),
5455
],
5556
];
5657
```

src/DiscordLogger/LogHandler.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use MarvinLabs\DiscordLogger\Converters\SimpleRecordConverter;
1010
use Monolog\Handler\AbstractProcessingHandler;
1111
use Monolog\Logger as Monolog;
12+
use Monolog\LogRecord;
1213
use RuntimeException;
1314
use function class_implements;
1415

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

24+
/** @var boolean */
25+
private $ignoreExceptions;
26+
2327
/** @throws \Illuminate\Contracts\Container\BindingResolutionException */
2428
public function __construct(Container $container, Repository $config, array $channelConfig)
2529
{
2630
parent::__construct(Monolog::toMonologLevel($channelConfig['level'] ?? Monolog::DEBUG));
2731

2832
$this->discord = $container->make(DiscordWebHook::class, ['url' => $channelConfig['url']]);
2933
$this->recordToMessage = $this->createRecordConverter($container, $config);
34+
35+
$this->ignoreExceptions = $channelConfig['ignore_exceptions'] ?? false;
3036
}
3137

32-
public function write(array $record): void
38+
public function write(array|LogRecord $record): void
3339
{
34-
foreach($this->recordToMessage->buildMessages($record) as $message)
35-
{
36-
$this->discord->send($message);
40+
if ($record instanceof LogRecord) {
41+
$record = $record->toArray();
42+
}
43+
foreach($this->recordToMessage->buildMessages($record) as $message) {
44+
try {
45+
$this->discord->send($message);
46+
} catch (\Exception $e) {
47+
if (!$this->ignoreExceptions) {
48+
throw $e;
49+
}
50+
}
3751
}
3852
}
3953

0 commit comments

Comments
 (0)