Skip to content

Commit

Permalink
fix for laravel 10
Browse files Browse the repository at this point in the history
  • Loading branch information
grkamil authored Feb 27, 2023
1 parent c5e2f35 commit 7f457d3
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/TelegramHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Logger;

use Exception;
use Illuminate\View\View;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Logger;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\LogRecord;

/**
* Class TelegramHandler
Expand All @@ -16,7 +18,7 @@ class TelegramHandler extends AbstractProcessingHandler
{
/**
* Logger config
*
*
* @var array
*/
private $config;
Expand Down Expand Up @@ -60,7 +62,7 @@ public function __construct(array $config)
parent::__construct($level, true);

// define variables for making Telegram request
$this->config = $config;
$this->config = $config;
$this->botToken = $this->getConfigValue('token');
$this->chatId = $this->getConfigValue('chat_id');

Expand All @@ -72,9 +74,9 @@ public function __construct(array $config)
/**
* @param array $record
*/
public function write(array $record): void
public function write($record): void
{
if(!$this->botToken || !$this->chatId) {
if (!$this->botToken || !$this->chatId) {
throw new \InvalidArgumentException('Bot token or chat id is not defined for Telegram logger');
}

Expand All @@ -99,31 +101,40 @@ protected function getDefaultFormatter(): FormatterInterface
}

/**
* @param array $record
* @param $record
* @return string
*/
private function formatText(array $record): string
private function formatText($record): string
{
if ($template = config('telegram-logger.template')) {
if ($record instanceof LogRecord) {
return view($template, array_merge($record->toArray(), [
'appName' => $this->appName,
'appEnv' => $this->appEnv,
'formatted' => $record->formatted,
])
)->render();
}

return view($template, array_merge($record, [
'appName' => $this->appName,
'appEnv' => $this->appEnv,
])
);
)->render();
}

return sprintf("<b>%s</b> (%s)\n%s", $this->appName, $record['level_name'], $record['formatted']);
}

/**
* @param string $text
* @param string $text
*/
private function sendMessage(string $text): void
{
$httpQuery = http_build_query(array_merge(
[
'text' => $text,
'chat_id' => $this->chatId,
'text' => $text,
'chat_id' => $this->chatId,
'parse_mode' => 'html',
],
config('telegram-logger.options', [])
Expand All @@ -137,7 +148,7 @@ private function sendMessage(string $text): void
$context = stream_context_create([
'http' => [
'proxy' => $proxy,
]
],
]);
file_get_contents($url, false, $context);
} else {
Expand All @@ -155,7 +166,7 @@ private function getConfigValue($key, $defaultConfigKey = null): ?string
if (isset($this->config[$key])) {
return $this->config[$key];
}

return config($defaultConfigKey ?: "telegram-logger.$key");
}
}

0 comments on commit 7f457d3

Please sign in to comment.