2
2
3
3
namespace Nutgram \Laravel \Log ;
4
4
5
+ use InvalidArgumentException ;
5
6
use Monolog \Formatter \FormatterInterface ;
6
7
use Monolog \Formatter \LineFormatter ;
7
8
use Monolog \Handler \AbstractProcessingHandler ;
8
9
use Monolog \Logger ;
9
10
use Monolog \LogRecord ;
10
11
use SergiX44 \Nutgram \Nutgram ;
12
+ use SergiX44 \Nutgram \Telegram \Properties \ParseMode ;
11
13
12
14
class LoggerHandler extends AbstractProcessingHandler
13
15
{
14
- protected Nutgram $ bot ;
15
-
16
- protected string |int $ chatId ;
16
+ protected string |int |null $ chatId ;
17
17
18
18
public function __construct (array $ config )
19
19
{
20
- parent ::__construct (Logger::toMonologLevel ($ config ['level ' ]), true );
20
+ parent ::__construct (Logger::toMonologLevel ($ config ['level ' ]));
21
21
22
- $ this ->bot = app (Nutgram::class);
23
22
$ this ->chatId = $ config ['chat_id ' ];
24
23
}
25
24
@@ -30,10 +29,14 @@ protected function getDefaultFormatter(): FormatterInterface
30
29
31
30
protected function write (LogRecord $ record ): void
32
31
{
33
- $ this ->bot ->sendChunkedMessage (
32
+ if ($ this ->chatId === null ) {
33
+ throw new InvalidArgumentException ('You must specify a chat_id via the NUTGRAM_LOG_CHAT_ID environment variable. ' );
34
+ }
35
+
36
+ app (Nutgram::class)->sendChunkedMessage (
34
37
text: $ this ->formatText ($ record ),
35
38
chat_id: $ this ->chatId ,
36
- parse_mode: ' html ' ,
39
+ parse_mode: ParseMode:: HTML ,
37
40
);
38
41
}
39
42
@@ -42,9 +45,9 @@ protected function formatText(LogRecord $record): string
42
45
return sprintf (
43
46
"<b>%s %s</b> (%s): \n<pre>%s</pre> " ,
44
47
config ('app.name ' ),
45
- $ record[ ' level_name ' ] ,
48
+ $ record-> level -> getName () ,
46
49
config ('app.env ' ),
47
- $ record[ ' formatted ' ]
50
+ $ record-> formatted ?? '' ,
48
51
);
49
52
}
50
53
}
0 commit comments