22
33namespace Nutgram \Laravel \Log ;
44
5+ use InvalidArgumentException ;
56use Monolog \Formatter \FormatterInterface ;
67use Monolog \Formatter \LineFormatter ;
78use Monolog \Handler \AbstractProcessingHandler ;
89use Monolog \Logger ;
910use Monolog \LogRecord ;
1011use SergiX44 \Nutgram \Nutgram ;
12+ use SergiX44 \Nutgram \Telegram \Properties \ParseMode ;
1113
1214class LoggerHandler extends AbstractProcessingHandler
1315{
14- protected Nutgram $ bot ;
15-
16- protected string |int $ chatId ;
16+ protected string |int |null $ chatId ;
1717
1818 public function __construct (array $ config )
1919 {
20- parent ::__construct (Logger::toMonologLevel ($ config ['level ' ]), true );
20+ parent ::__construct (Logger::toMonologLevel ($ config ['level ' ]));
2121
22- $ this ->bot = app (Nutgram::class);
2322 $ this ->chatId = $ config ['chat_id ' ];
2423 }
2524
@@ -30,10 +29,14 @@ protected function getDefaultFormatter(): FormatterInterface
3029
3130 protected function write (LogRecord $ record ): void
3231 {
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 (
3437 text: $ this ->formatText ($ record ),
3538 chat_id: $ this ->chatId ,
36- parse_mode: ' html ' ,
39+ parse_mode: ParseMode:: HTML ,
3740 );
3841 }
3942
@@ -42,9 +45,9 @@ protected function formatText(LogRecord $record): string
4245 return sprintf (
4346 "<b>%s %s</b> (%s): \n<pre>%s</pre> " ,
4447 config ('app.name ' ),
45- $ record[ ' level_name ' ] ,
48+ $ record-> level -> getName () ,
4649 config ('app.env ' ),
47- $ record[ ' formatted ' ]
50+ $ record-> formatted ?? '' ,
4851 );
4952 }
5053}
0 commit comments