Skip to content

Commit 4f7cb95

Browse files
committed
Fix possible infinite loop + chat_id validation
1 parent b6848b2 commit 4f7cb95

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/Log/LoggerHandler.php

+12-9
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@
22

33
namespace Nutgram\Laravel\Log;
44

5+
use InvalidArgumentException;
56
use Monolog\Formatter\FormatterInterface;
67
use Monolog\Formatter\LineFormatter;
78
use Monolog\Handler\AbstractProcessingHandler;
89
use Monolog\Logger;
910
use Monolog\LogRecord;
1011
use SergiX44\Nutgram\Nutgram;
12+
use SergiX44\Nutgram\Telegram\Properties\ParseMode;
1113

1214
class 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

Comments
 (0)