Skip to content

Commit 52f6985

Browse files
committed
refactory
1 parent 1beb306 commit 52f6985

File tree

7 files changed

+386
-112
lines changed

7 files changed

+386
-112
lines changed

README.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
# this is about monitoring your logs in telegram
1+
# Telegram Logs Monitor for Laravel
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/uzhlaravel/telegramlogs.svg?style=flat-square)](https://packagist.org/packages/uzhlaravel/telegramlogs)
44
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/uzhlaravel/telegramlogs/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/uzhlaravel/telegramlogs/actions?query=workflow%3Arun-tests+branch%3Amain)
55
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/uzhlaravel/telegramlogs/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/uzhlaravel/telegramlogs/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
66
[![Total Downloads](https://img.shields.io/packagist/dt/uzhlaravel/telegramlogs.svg?style=flat-square)](https://packagist.org/packages/uzhlaravel/telegramlogs)
77

8-
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
8+
Get real-time Laravel application logs directly in your Telegram channel. This package provides instant monitoring of your application's critical events through Telegram messages, with support for threaded discussions and Markdown formatting.
99

10-
## Support us
1110

12-
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/telegramlogs.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/telegramlogs)
11+
## Features
1312

14-
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
15-
16-
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
13+
- 📨 Instant delivery of logs to Telegram
14+
- 🔔 Configurable log levels (emergency to debug)
15+
- 🧵 Support for Telegram topic threads
16+
- ✏️ MarkdownV2 formatted messages
17+
- 📦 Automatic splitting of long messages
18+
- ⏱ Configurable timeout for API calls
19+
- 🛠 Test command to verify your setup
1720

1821
## Installation
1922

@@ -23,13 +26,6 @@ You can install the package via composer:
2326
composer require uzhlaravel/telegramlogs
2427
```
2528

26-
You can publish and run the migrations with:
27-
28-
```bash
29-
php artisan vendor:publish --tag="telegramlogs-migrations"
30-
php artisan migrate
31-
```
32-
3329
You can publish the config file with:
3430

3531
```bash
@@ -40,13 +36,20 @@ This is the contents of the published config file:
4036

4137
```php
4238
return [
39+
'bot_token' => env('TELEGRAM_BOT_TOKEN'),
40+
'chat_id' => env('TELEGRAM_CHAT_ID'),
41+
'topic_id' => env('TELEGRAM_TOPIC_ID'),
4342
];
4443
```
4544

46-
Optionally, you can publish the views using
45+
This is the contents in .env:
4746

48-
```bash
49-
php artisan vendor:publish --tag="telegramlogs-views"
47+
```php
48+
TELEGRAM_LOGS_BOT_TOKEN=your_bot_token_here
49+
TELEGRAM_LOGS_CHAT_ID=your_chat_id_here
50+
# Optional:
51+
TELEGRAM_LOGS_TOPIC_ID=your_thread_id_here
52+
TELEGRAM_LOGS_LEVEL=error
5053
```
5154

5255
## Usage

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"laravel",
77
"telegramlogs"
88
],
9-
"homepage": "https://github.com/uzhlaravel/telegramlogs",
9+
"homepage": "https://github.com/uzziahlukeka/telegramlogs",
1010
"license": "MIT",
1111
"authors": [
1212
{

config/telegramlogs.php

Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,140 @@
11
<?php
22

3-
// config for Uzhlaravel/Telegramlogs
43
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Telegram Bot Token
7+
|--------------------------------------------------------------------------
8+
|
9+
| Your Telegram bot API token. Create a new bot by talking to @BotFather
10+
| on Telegram and paste the token here.
11+
|
12+
*/
513
'bot_token' => env('TELEGRAM_BOT_TOKEN'),
14+
15+
/*
16+
|--------------------------------------------------------------------------
17+
| Telegram Chat ID
18+
|--------------------------------------------------------------------------
19+
|
20+
| The chat ID where logs will be sent. For channels use @channelname,
21+
| for groups use the numeric ID (with negative sign for supergroups).
22+
|
23+
*/
624
'chat_id' => env('TELEGRAM_CHAT_ID'),
7-
'topic_id' => env('TELEGRAM_TOPIC_ID'), // Optional for group topics
825

26+
/*
27+
|--------------------------------------------------------------------------
28+
| Telegram Topic ID
29+
|--------------------------------------------------------------------------
30+
|
31+
| Optional thread ID in a forum-style group where logs will be sent.
32+
| Only applicable for groups that have topics enabled.
33+
|
34+
*/
35+
'topic_id' => env('TELEGRAM_TOPIC_ID', null),
36+
37+
/*
38+
|--------------------------------------------------------------------------
39+
| Default Logging Level
40+
|--------------------------------------------------------------------------
41+
|
42+
| The minimum log level that will be sent to Telegram. Available levels:
43+
| emergency, alert, critical, error, warning, notice, info, debug
44+
|
45+
*/
46+
'level' => env('TELEGRAM_LOG_LEVEL', 'critical'),
47+
48+
/*
49+
|--------------------------------------------------------------------------
50+
| HTTP Timeout
51+
|--------------------------------------------------------------------------
52+
|
53+
| Timeout in seconds for requests to Telegram's API.
54+
|
55+
*/
56+
'timeout' => env('TELEGRAM_TIMEOUT', 10),
57+
58+
/*
59+
|--------------------------------------------------------------------------
60+
| Message Formatting
61+
|--------------------------------------------------------------------------
62+
*/
63+
'formatting' => [
64+
/*
65+
|----------------------------------------------------------------------
66+
| Parse Mode
67+
|----------------------------------------------------------------------
68+
|
69+
| Telegram message formatting style. Supported values:
70+
| 'MarkdownV2', 'HTML', or null for plain text
71+
|
72+
*/
73+
'parse_mode' => 'MarkdownV2',
74+
75+
/*
76+
|----------------------------------------------------------------------
77+
| Include Context
78+
|----------------------------------------------------------------------
79+
|
80+
| Whether to include context data with log messages.
81+
|
82+
*/
83+
'include_context' => true,
84+
85+
/*
86+
|----------------------------------------------------------------------
87+
| Include Stack Trace
88+
|----------------------------------------------------------------------
89+
|
90+
| Whether to include stack traces for error-level logs.
91+
|
92+
*/
93+
'include_stack_trace' => true,
94+
],
95+
96+
/*
97+
|--------------------------------------------------------------------------
98+
| Message Handling
99+
|--------------------------------------------------------------------------
100+
*/
101+
'message' => [
102+
/*
103+
|----------------------------------------------------------------------
104+
| Split Long Messages
105+
|----------------------------------------------------------------------
106+
|
107+
| Whether to automatically split messages longer than Telegram's 4096
108+
| character limit into multiple messages.
109+
|
110+
*/
111+
'split_long_messages' => true,
112+
113+
/*
114+
|----------------------------------------------------------------------
115+
| Max Message Length
116+
|----------------------------------------------------------------------
117+
|
118+
| Maximum length for each message before splitting occurs.
119+
|
120+
*/
121+
'max_length' => 4000, // Slightly under 4096 for safety
122+
],
123+
124+
/*
125+
|--------------------------------------------------------------------------
126+
| Logging Channels Configuration
127+
|--------------------------------------------------------------------------
128+
|
129+
| Pre-configured logging channel for Laravel's logging system.
130+
|
131+
*/
9132
'channels' => [
10133
'telegram' => [
11134
'driver' => 'custom',
12-
'via' => Uzhlaravel\Telegramlogs\Telegramlogs::class,
13-
'level' => 'critical',
135+
'via' => \Uzhlaravel\Telegramlogs\Telegramlogs::class,
136+
'level' => env('TELEGRAM_LOG_LEVEL', 'critical'),
137+
'ignore_empty_messages' => true,
14138
],
15139
],
16140
];

src/Commands/TelegramlogsCommand.php

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,103 @@
33
namespace Uzhlaravel\Telegramlogs\Commands;
44

55
use Illuminate\Console\Command;
6+
use Uzhlaravel\Telegramlogs\Facades\Telegramlogs;
67

78
class TelegramlogsCommand extends Command
89
{
9-
public $signature = 'telegramlogs';
10+
protected $signature = 'telegramlogs:test
11+
{--message= : Custom test message to send}
12+
{--level= : Log level (debug, info, notice, warning, error, critical, alert, emergency)}
13+
{--list : List available log levels}
14+
{--config : Show current configuration}';
1015

11-
public $description = 'My command';
16+
protected $description = 'Test and manage Telegram logs integration';
17+
18+
protected array $logLevels = [
19+
'debug',
20+
'info',
21+
'notice',
22+
'warning',
23+
'error',
24+
'critical',
25+
'alert',
26+
'emergency',
27+
];
1228

1329
public function handle(): int
1430
{
15-
$this->comment('All done');
31+
if ($this->option('list')) {
32+
return $this->listLogLevels();
33+
}
34+
35+
if ($this->option('config')) {
36+
return $this->showConfig();
37+
}
38+
39+
return $this->sendTestMessage();
40+
}
41+
42+
protected function listLogLevels(): int
43+
{
44+
$this->info('Available log levels:');
45+
$this->newLine();
46+
47+
foreach ($this->logLevels as $level) {
48+
$this->line("- {$level}");
49+
}
50+
51+
return self::SUCCESS;
52+
}
53+
54+
protected function showConfig(): int
55+
{
56+
$this->info('Current Telegram Logs Configuration:');
57+
$this->table(
58+
['Setting', 'Value'],
59+
[
60+
['Bot Token', config('telegram-logger.bot_token') ? '****'.substr(config('telegram-logger.bot_token'), -4) : 'Not set'],
61+
['Chat ID', config('telegram-logger.chat_id') ?? 'Not set'],
62+
['Topic ID', config('telegram-logger.topic_id') ?? 'Not set'],
63+
['Default Level', config('telegram-logger.level', 'error')],
64+
]
65+
);
1666

1767
return self::SUCCESS;
1868
}
69+
70+
protected function sendTestMessage(): int
71+
{
72+
$message = $this->option('message') ?? 'This is a test message from Telegramlogs command';
73+
$level = strtolower($this->option('level') ?? 'info');
74+
75+
if (! in_array($level, $this->logLevels)) {
76+
$this->error("Invalid log level: {$level}");
77+
$this->line('Use --list option to see available levels');
78+
79+
return self::FAILURE;
80+
}
81+
82+
$this->info('Sending test message to Telegram...');
83+
$this->newLine();
84+
$this->line("Level: {$level}");
85+
$this->line("Message: {$message}");
86+
87+
try {
88+
Telegramlogs::log($level, $message, [
89+
'command' => 'telegramlogs:test',
90+
'timestamp' => now()->toDateTimeString(),
91+
]);
92+
93+
$this->newLine();
94+
$this->info('Message sent successfully!');
95+
96+
return self::SUCCESS;
97+
} catch (\Exception $e) {
98+
$this->newLine();
99+
$this->error('Failed to send message:');
100+
$this->error($e->getMessage());
101+
102+
return self::FAILURE;
103+
}
104+
}
19105
}

src/Facades/Telegramlogs.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,25 @@
33
namespace Uzhlaravel\Telegramlogs\Facades;
44

55
use Illuminate\Support\Facades\Facade;
6+
use Monolog\Logger;
7+
use Uzhlaravel\Telegramlogs\Telegramlogs as TelegramLoggerInstance;
68

79
/**
10+
* @method static void emergency(string $message, array $context = [])
11+
* @method static void alert(string $message, array $context = [])
12+
* @method static void critical(string $message, array $context = [])
13+
* @method static void error(string $message, array $context = [])
14+
* @method static void warning(string $message, array $context = [])
15+
* @method static void notice(string $message, array $context = [])
16+
* @method static void info(string $message, array $context = [])
17+
* @method static void debug(string $message, array $context = [])
18+
* @method static void log($level, string $message, array $context = [])
19+
* @method static Logger channel(string $channel = null)
20+
* @method static TelegramLoggerInstance setBotToken(string $token)
21+
* @method static TelegramLoggerInstance setChatId(string $chatId)
22+
* @method static TelegramLoggerInstance setTopicId(?string $topicId)
23+
* @method static TelegramLoggerInstance setTimeout(int $timeout)
24+
*
825
* @see \Uzhlaravel\Telegramlogs\Telegramlogs
926
*/
1027
class Telegramlogs extends Facade

0 commit comments

Comments
 (0)