Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix possible infinite loop + chat_id validation #26

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/Log/LoggerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

Expand All @@ -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,
);
}

Expand All @@ -42,9 +45,9 @@ protected function formatText(LogRecord $record): string
return sprintf(
"<b>%s %s</b> (%s):\n<pre>%s</pre>",
config('app.name'),
$record['level_name'],
$record->level->getName(),
config('app.env'),
$record['formatted']
$record->formatted ?? '',
);
}
}
Loading