From 4f7cb953e6738946ffecfb0b1b695d65bf3a055d Mon Sep 17 00:00:00 2001 From: Luca Patera Date: Wed, 2 Oct 2024 19:59:06 +0200 Subject: [PATCH] Fix possible infinite loop + chat_id validation --- src/Log/LoggerHandler.php | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Log/LoggerHandler.php b/src/Log/LoggerHandler.php index ee26077..b10ef4e 100644 --- a/src/Log/LoggerHandler.php +++ b/src/Log/LoggerHandler.php @@ -2,24 +2,23 @@ namespace Nutgram\Laravel\Log; +use InvalidArgumentException; use Monolog\Formatter\FormatterInterface; use Monolog\Formatter\LineFormatter; use Monolog\Handler\AbstractProcessingHandler; use Monolog\Logger; use Monolog\LogRecord; use SergiX44\Nutgram\Nutgram; +use SergiX44\Nutgram\Telegram\Properties\ParseMode; class LoggerHandler extends AbstractProcessingHandler { - protected Nutgram $bot; - - protected string|int $chatId; + protected string|int|null $chatId; public function __construct(array $config) { - parent::__construct(Logger::toMonologLevel($config['level']), true); + parent::__construct(Logger::toMonologLevel($config['level'])); - $this->bot = app(Nutgram::class); $this->chatId = $config['chat_id']; } @@ -30,10 +29,14 @@ protected function getDefaultFormatter(): FormatterInterface protected function write(LogRecord $record): void { - $this->bot->sendChunkedMessage( + if ($this->chatId === null) { + throw new InvalidArgumentException('You must specify a chat_id via the NUTGRAM_LOG_CHAT_ID environment variable.'); + } + + app(Nutgram::class)->sendChunkedMessage( text: $this->formatText($record), chat_id: $this->chatId, - parse_mode: 'html', + parse_mode: ParseMode::HTML, ); } @@ -42,9 +45,9 @@ protected function formatText(LogRecord $record): string return sprintf( "%s %s (%s):\n
%s
", config('app.name'), - $record['level_name'], + $record->level->getName(), config('app.env'), - $record['formatted'] + $record->formatted ?? '', ); } }