Skip to content

Commit 7f457d3

Browse files
authored
fix for laravel 10
1 parent c5e2f35 commit 7f457d3

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

src/TelegramHandler.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
namespace Logger;
44

55
use Exception;
6+
use Illuminate\View\View;
67
use Monolog\Formatter\FormatterInterface;
78
use Monolog\Formatter\LineFormatter;
89
use Monolog\Logger;
910
use Monolog\Handler\AbstractProcessingHandler;
11+
use Monolog\LogRecord;
1012

1113
/**
1214
* Class TelegramHandler
@@ -16,7 +18,7 @@ class TelegramHandler extends AbstractProcessingHandler
1618
{
1719
/**
1820
* Logger config
19-
*
21+
*
2022
* @var array
2123
*/
2224
private $config;
@@ -60,7 +62,7 @@ public function __construct(array $config)
6062
parent::__construct($level, true);
6163

6264
// define variables for making Telegram request
63-
$this->config = $config;
65+
$this->config = $config;
6466
$this->botToken = $this->getConfigValue('token');
6567
$this->chatId = $this->getConfigValue('chat_id');
6668

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

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

101103
/**
102-
* @param array $record
104+
* @param $record
103105
* @return string
104106
*/
105-
private function formatText(array $record): string
107+
private function formatText($record): string
106108
{
107109
if ($template = config('telegram-logger.template')) {
110+
if ($record instanceof LogRecord) {
111+
return view($template, array_merge($record->toArray(), [
112+
'appName' => $this->appName,
113+
'appEnv' => $this->appEnv,
114+
'formatted' => $record->formatted,
115+
])
116+
)->render();
117+
}
118+
108119
return view($template, array_merge($record, [
109120
'appName' => $this->appName,
110121
'appEnv' => $this->appEnv,
111122
])
112-
);
123+
)->render();
113124
}
114125

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

118129
/**
119-
* @param string $text
130+
* @param string $text
120131
*/
121132
private function sendMessage(string $text): void
122133
{
123134
$httpQuery = http_build_query(array_merge(
124135
[
125-
'text' => $text,
126-
'chat_id' => $this->chatId,
136+
'text' => $text,
137+
'chat_id' => $this->chatId,
127138
'parse_mode' => 'html',
128139
],
129140
config('telegram-logger.options', [])
@@ -137,7 +148,7 @@ private function sendMessage(string $text): void
137148
$context = stream_context_create([
138149
'http' => [
139150
'proxy' => $proxy,
140-
]
151+
],
141152
]);
142153
file_get_contents($url, false, $context);
143154
} else {
@@ -155,7 +166,7 @@ private function getConfigValue($key, $defaultConfigKey = null): ?string
155166
if (isset($this->config[$key])) {
156167
return $this->config[$key];
157168
}
158-
169+
159170
return config($defaultConfigKey ?: "telegram-logger.$key");
160171
}
161172
}

0 commit comments

Comments
 (0)