9
9
use MarvinLabs \DiscordLogger \Converters \SimpleRecordConverter ;
10
10
use Monolog \Handler \AbstractProcessingHandler ;
11
11
use Monolog \Logger as Monolog ;
12
+ use Monolog \LogRecord ;
12
13
use RuntimeException ;
13
14
use function class_implements ;
14
15
@@ -20,20 +21,33 @@ class LogHandler extends AbstractProcessingHandler
20
21
/** @var \MarvinLabs\DiscordLogger\Contracts\RecordToMessage */
21
22
private $ recordToMessage ;
22
23
24
+ /** @var boolean */
25
+ private $ ignoreExceptions ;
26
+
23
27
/** @throws \Illuminate\Contracts\Container\BindingResolutionException */
24
28
public function __construct (Container $ container , Repository $ config , array $ channelConfig )
25
29
{
26
30
parent ::__construct (Monolog::toMonologLevel ($ channelConfig ['level ' ] ?? Monolog::DEBUG ));
27
31
28
32
$ this ->discord = $ container ->make (DiscordWebHook::class, ['url ' => $ channelConfig ['url ' ]]);
29
33
$ this ->recordToMessage = $ this ->createRecordConverter ($ container , $ config );
34
+
35
+ $ this ->ignoreExceptions = $ channelConfig ['ignore_exceptions ' ] ?? false ;
30
36
}
31
37
32
- public function write (array $ record ): void
38
+ public function write (array | LogRecord $ record ): void
33
39
{
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
+ }
37
51
}
38
52
}
39
53
0 commit comments