Skip to content

Commit 97da665

Browse files
committed
init
1 parent b6ddc77 commit 97da665

File tree

11 files changed

+320
-46
lines changed

11 files changed

+320
-46
lines changed

README.md

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,121 @@
1-
# Laravel telegram logger
1+
# Laravel Telegram Logger
22

3-
## Installation
3+
A simple Laravel package to send log messages directly to a Telegram chat or group using the Telegram Bot API.
4+
5+
## Features
6+
7+
- Log messages from Laravel to Telegram in real-time.
8+
- Easy setup with simple configuration.
9+
- Customizable Telegram message format.
10+
11+
---
12+
13+
## Installation
14+
15+
```bash
16+
composer require c0dem1ner/laravel-telegram-logger
17+
```
18+
19+
## Configuration
20+
21+
1. Publish the config file:
22+
23+
```bash
24+
php artisan vendor:publish --tag=telegram-logger-config
25+
```
26+
27+
2. Update the `.env` file:
28+
29+
```
30+
TELEGRAM_BOT_TOKEN=your-telegram-bot-token
31+
TELEGRAM_CHAT_ID=your-telegram-chat-id
32+
```
33+
34+
3. Configure logging in `config/telegram-logger.php`:
35+
36+
```php
37+
'channels' => [
38+
// other channels...
39+
40+
'telegram' => [
41+
'driver' => 'custom',
42+
'level' => env('LOG_LEVEL', 'debug'),
43+
'via' => \C0deM1ner\LaravelTelegramLogger\Logger\TelegramLogger::class,
44+
'chat_id' => env('TELEGRAM_CHAT_ID', '1234567'),
45+
'token' => env('TELEGRAM_BOT_TOKEN', '')
46+
],
47+
48+
// You can also add multiple Telegram channels
49+
],
50+
],
51+
```
52+
53+
---
54+
55+
## Usage
56+
57+
You can log directly to Telegram using:
58+
59+
```php
60+
telegramLog()->info('Your message here');
61+
telegramLog()->alert('Your message here');
62+
telegramLog()->error('Your message here');
63+
```
64+
65+
Or use Laravel’s global logging:
66+
67+
```php
68+
logger()
69+
->channel('telegram')
70+
->debug('Your message here');
71+
```
72+
73+
> Any log message matching the configured level will be sent to Telegram.
74+
75+
---
76+
77+
## Customizing Messages
78+
79+
If needed, you can publish and edit the view for Telegram messages:
80+
81+
```bash
82+
php artisan vendor:publish --tag=telegram-logger-views
83+
```
84+
85+
Customize the message structure in `resources/views/vendor/telegram-logger/messages`.
86+
87+
---
88+
89+
## Testing
90+
91+
You can test your configuration by running:
92+
93+
```bash
94+
php artisan telegram-log:send-test-message
95+
```
96+
97+
---
98+
99+
## License
100+
101+
This package is open-sourced software licensed under the [MIT license](LICENSE).
102+
103+
---
104+
105+
## Credits
106+
107+
- Developed by C0deM1ner
108+
- Telegram Bot API
109+
110+
---
111+
112+
## Contributions
113+
114+
Contributions are welcome! Please open issues or submit PRs.
115+
116+
---
117+
118+
## Security
119+
120+
If you discover any security-related issues, please email [email protected] instead of using the issue tracker.
4121

5-
```composer require c0dem1ner/laravel-telegram-logger```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212
],
1313
"require": {
14-
"php": ">=7.1",
14+
"php": ">=7.4",
1515
"illuminate/support": "^7.0|^8.0|^9.0|^10.0|^11.0|^12.0"
1616
},
1717
"autoload": {

config/telegram-logger.php

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,72 @@
11
<?php
22

33
return [
4-
'log_errors' => env('TELEGRAM_LOG_ERRORS', '500,502,503,504'),
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Telegram Bot Token
8+
|--------------------------------------------------------------------------
9+
|
10+
| This option defines the Telegram Bot Token that gets used when sending
11+
| messages to the Telegram Bot API. You can get your Bot Token from
12+
| the BotFather on Telegram.
13+
|
14+
*/
15+
16+
'token' => env('TELEGRAM_BOT_TOKEN', ''),
17+
18+
/*
19+
|--------------------------------------------------------------------------
20+
| Telegram Chat ID
21+
|--------------------------------------------------------------------------
22+
|
23+
| This option defines the Telegram Chat ID that gets used when sending
24+
| messages to the Telegram Bot API. You can get your Chat ID by
25+
| sending a message to the bot and then visiting the following URL:
26+
| https://api.telegram.org/bot<YourBOTToken>/getUpdates
27+
|
28+
*/
529

630
'chat_id' => env('TELEGRAM_CHAT_ID', '1234567'),
731

8-
'token' => env('TELEGRAM_BOT_TOKEN', '')
32+
/*
33+
|--------------------------------------------------------------------------
34+
| Telegram Bot Parse Mode
35+
|--------------------------------------------------------------------------
36+
|
37+
| This option defines the default parse mode that gets used when sending
38+
| messages to the Telegram Bot API. The parse mode can be either "html"
39+
| or "markdown". The default value is "html".
40+
|
41+
*/
42+
43+
'parse_mode' => 'html',
44+
45+
/*
46+
|--------------------------------------------------------------------------
47+
| Default Log Type
48+
|--------------------------------------------------------------------------
49+
|
50+
| This option defines the default log type that gets used when sending
51+
| messages to the Telegram Bot API. The default value is "error".
52+
| can be either "info", "alert" or "error".
53+
|
54+
*/
55+
56+
'default_logger_type' => 'error',
57+
58+
/*
59+
|--------------------------------------------------------------------------
60+
| Errors to log
61+
|--------------------------------------------------------------------------
62+
|
63+
| This option defines the errors that should be logged to the Telegram Bot
64+
| API. The default value is "500". You can add more error codes to the
65+
| array to log more errors.
66+
|
67+
*/
68+
69+
'log_errors' => [
70+
'500'
71+
],
972
];

src/Console/Commands/SendTestMessageCommand.php

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

55
use Exception;
66
use Illuminate\Console\Command;
7+
use Throwable;
78

89
class SendTestMessageCommand extends Command
910
{
@@ -35,6 +36,7 @@ public function __construct()
3536
* Execute the console command.
3637
*
3738
* @return int
39+
* @throws Throwable
3840
*/
3941
public function handle(): int
4042
{

src/Logger/TelegramLoggerHandler.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace C0deM1ner\LaravelTelegramLogger\Logger;
44

5-
use C0deM1ner\LaravelTelegramLogger\Telegram\TelegramBotApi;
65
use Monolog\Handler\AbstractProcessingHandler;
76
use Monolog\Logger;
8-
use Monolog\LogRecord;
97

108
class TelegramLoggerHandler extends AbstractProcessingHandler
119
{
@@ -22,13 +20,13 @@ public function __construct(array $config)
2220
$this->chatId = $config['chat_id'];
2321
}
2422

25-
protected function write(LogRecord|array $record): void
23+
protected function write($record): void
2624
{
27-
app(TelegramBotApi::class)::sendMessage(
28-
$this->token,
29-
$this->chatId,
30-
config('app.name') . PHP_EOL .
31-
$record['formatted']
32-
);
25+
$type = config('telegram-logger.default_logger_type', 'error');
26+
27+
telegramLog()
28+
->setToken($this->token)
29+
->setChatId($this->chatId)
30+
->$type($record['formatted']);
3331
}
3432
}

src/Providers/TelegramLoggerServiceProvider.php

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

55
use C0deM1ner\LaravelTelegramLogger\Console\Commands\SendTestMessageCommand;
6-
use Carbon\CarbonInterval;
7-
use Illuminate\Foundation\Http\Kernel;
8-
use Illuminate\Support\Facades\DB;
96
use Illuminate\Support\ServiceProvider;
107

118
class TelegramLoggerServiceProvider extends ServiceProvider
@@ -17,28 +14,7 @@ class TelegramLoggerServiceProvider extends ServiceProvider
1714
*/
1815
public function register(): void
1916
{
20-
logger()
21-
->channel('telegram')
22-
->debug('Query is taking too long');
23-
24-
if (app()->isProduction()) {
25-
DB::listen(function ($query) {
26-
if ($query->time > 100) {
27-
logger()
28-
->channel('telegram')
29-
->debug('Query is taking too long' . $query->sql, $query->bindings);
30-
}
31-
});
32-
33-
app(Kernel::class)->whenRequestLifecycleIsLongerThan(
34-
CarbonInterval::second(4),
35-
function () {
36-
logger()
37-
->channel('telegram')
38-
->debug('Request is taking too long' . request()->url());
39-
}
40-
);
41-
}
17+
//
4218
}
4319

4420
/**
@@ -48,12 +24,19 @@ function () {
4824
*/
4925
public function boot(): void
5026
{
27+
$this->mergeConfigFrom(
28+
__DIR__ . '/../../config/telegram-logger.php', 'telegram-logger'
29+
);
30+
5131
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'telegram-logger');
5232

5333
$this->publishes([
54-
__DIR__ . '/../resources/views' => resource_path('views/vendor/telegram-logger'),
5534
__DIR__ . '/../../config/telegram-logger.php' => config_path('telegram-logger.php'),
56-
]);
35+
], 'telegram-logger-config');
36+
37+
$this->publishes([
38+
__DIR__ . '/../resources/views' => resource_path('views/vendor/telegram-logger'),
39+
], 'telegram-logger-views');
5740

5841
if ($this->app->runningInConsole()) {
5942
$this->commands([

src/Telegram/TelegramBotApi.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public static function sendMessage(string $token, string $chatId, string $text =
2323

2424
$response = Http::withoutVerifying()->post($url, [
2525
'chat_id' => $chatId,
26-
'text' => $text
26+
'text' => $text,
27+
'parse_mode' => 'HTML'
2728
])->throw()->json();
2829

2930
return $response['ok'] ?? false;

0 commit comments

Comments
 (0)