Skip to content

Commit f03f76d

Browse files
committed
refactor: update error and alert templates, change default logger type, and enhance exception handling
1 parent c5b8860 commit f03f76d

File tree

10 files changed

+88
-21
lines changed

10 files changed

+88
-21
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "c0dem1ner/laravel-telegram-logger",
3-
"description": "description",
3+
"description": "Telegram Log integration for Laravel",
44
"minimum-stability": "stable",
55
"license": "MIT",
66
"authors": [

src/Logger/TelegramLoggerHandler.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class TelegramLoggerHandler extends AbstractProcessingHandler
99
{
1010
public string $token;
1111
public string $chatId;
12+
public string $type;
1213

1314
public function __construct(array $config)
1415
{
@@ -18,15 +19,16 @@ public function __construct(array $config)
1819

1920
$this->token = $config['token'];
2021
$this->chatId = $config['chat_id'];
22+
$this->type = $config['type'];
2123
}
2224

2325
protected function write($record): void
2426
{
25-
$type = config('telegram-logger.default_logger_type', 'error');
27+
$this->type = $this->type ?? config('telegram-logger.default_logger_type', 'error');
2628

2729
telegramLog()
2830
->setToken($this->token)
2931
->setChatId($this->chatId)
30-
->$type($record['formatted']);
32+
->$this->type($record['formatted']);
3133
}
3234
}

src/Providers/TelegramLoggerServiceProvider.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace C0deM1ner\LaravelTelegramLogger\Providers;
44

55
use C0deM1ner\LaravelTelegramLogger\Console\Commands\SendTestMessageCommand;
6+
use C0deM1ner\LaravelTelegramLogger\Types\FormatExceptionForTelegramType;
67
use Illuminate\Contracts\Debug\ExceptionHandler;
78
use Illuminate\Support\ServiceProvider;
89
use Throwable;
@@ -53,13 +54,20 @@ public function boot(): void
5354
app(ExceptionHandler::class)->reportable(function (Throwable $e) use ($errorCodes) {
5455
if (method_exists($e, 'getStatusCode')) {
5556
$code = $e->getStatusCode();
56-
5757
} else {
5858
$code = 500;
5959
}
6060

6161
if (in_array($code, $errorCodes)) {
62-
telegramLog()->error($e->getMessage());
62+
$additionalData = [
63+
'Request Method' => request()->method(),
64+
'Request URL' => request()->url()
65+
];
66+
67+
telegramLog()->error(
68+
(new FormatExceptionForTelegramType())
69+
->execute($e, $additionalData)
70+
);
6371
}
6472
});
6573
}

src/Telegram/TelegramBotApi.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use C0deM1ner\LaravelTelegramLogger\Telegram\Exceptions\TelegramBotApiException;
66
use Illuminate\Support\Facades\Http;
7-
use Illuminate\Support\Facades\RateLimiter;
87
use Throwable;
98

109
class TelegramBotApi
@@ -19,15 +18,6 @@ class TelegramBotApi
1918
*/
2019
public static function sendMessage(string $token, string $chatId, string $text = ''): bool
2120
{
22-
$key = 'telegram_logger';
23-
24-
if (RateLimiter::tooManyAttempts($key, 5)) {
25-
info('Telegram rate limit exceeded');
26-
return false;
27-
}
28-
29-
RateLimiter::hit($key);
30-
3121
try {
3222
$url = self::HOST . $token . '/sendMessage';
3323

src/Telegram/TelegramLog.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace C0deM1ner\LaravelTelegramLogger\Telegram;
44

5+
use Illuminate\Support\Facades\RateLimiter;
56
use Throwable;
67

78
class TelegramLog
@@ -42,6 +43,20 @@ public function setChatId(string $chatId): self
4243
return $this;
4344
}
4445

46+
protected function rateLimiter($message): bool
47+
{
48+
$key = md5('telegram_logger' . $message);
49+
50+
if (RateLimiter::tooManyAttempts($key, 5)) {
51+
info('Telegram rate limit exceeded');
52+
return false;
53+
}
54+
55+
RateLimiter::hit($key);
56+
57+
return true;
58+
}
59+
4560
/**
4661
* @param string $message
4762
* @return void
@@ -80,6 +95,10 @@ public function error(string $message): void
8095
*/
8196
private function send($type, $message): void
8297
{
98+
if (!$this->rateLimiter($message)) {
99+
return;
100+
}
101+
83102
$chunks = $this->chunkMessage(
84103
$this->formatText($type, $message)
85104
);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace C0deM1ner\LaravelTelegramLogger\Types;
4+
5+
use Throwable;
6+
7+
class FormatExceptionForTelegramType
8+
{
9+
/**
10+
* @throws Throwable
11+
*/
12+
public function execute(Throwable $exception, $additionalData = []): string
13+
{
14+
return view('telegram-logger::types.exception', [
15+
'exception' => $exception,
16+
'additionalData' => $additionalData,
17+
'trace' => $this->formatTrace($exception),
18+
])->render();
19+
}
20+
21+
protected function formatTrace($exception): string
22+
{
23+
return collect($exception->getTrace())
24+
->map(function ($trace, $index) {
25+
$file = $trace['file'] ?? '[internal function]';
26+
$line = $trace['line'] ?? '';
27+
$function = $trace['function'] ?? '';
28+
return ($index + 1) . ". `{$file}:{$line}` - {$function}";
29+
})
30+
->take(5)
31+
->implode("\n");
32+
}
33+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<b>🟨 Alert 🟨</b>
2-
<b>App Name </b>{{ $appName }}
2+
<b>App Name: </b>{{ $appName }}
33

4-
{{ $message }}
4+
{!! $message !!}
55

66
# ##########################################################################
77
{{ $time ?? now() }}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<b>🟥 Error 🟥</b>
2-
<b>App Name </b>{{ $appName }}
2+
<b>App Name: </b>{{ $appName }}
33

4-
{{ $message }}
4+
{!! $message !!}
55

66
# ##########################################################################
77
{{ $time ?? now() }}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<b>🟦 Information 🟦</b>
2-
<b>App Name </b>{{ $appName }}
2+
<b>App Name: </b>{{ $appName }}
33

4-
{{ $message }}
4+
{!! $message !!}
55

66
# ##########################################################################
77
{{ $time ?? now() }}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<b>Type:</b> <code>{{ get_class($exception) }}</code>
2+
<b>Message:</b> <i>{{ $exception->getMessage() }}</i>
3+
<b>File:</b> <code>{{ $exception->getFile() }}</code>
4+
<b>Line:</b> <code>{{ $exception->getLine() }}</code>
5+
6+
@if (!empty($additionalData))
7+
<b>Additional Data:</b>
8+
@foreach ($additionalData as $key => $value)
9+
<b>{{ $key }}:</b> <code>{{ $value }}</code>
10+
@endforeach
11+
@endif
12+
13+
14+
<b>Trace:</b>
15+
<pre>{{ $trace }}</pre>

0 commit comments

Comments
 (0)