arhx / telegram-log-channel
A simple Laravel package to send log messages to a Telegram chat.
Installs: 13
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:laravel-package
pkg:composer/arhx/telegram-log-channel
Requires
- php: ^8.0
- guzzlehttp/guzzle: ^7.0
- illuminate/console: ^8.0|^9.0|^10.0|^11.0|^12.0
- illuminate/log: ^8.0|^9.0|^10.0|^11.0|^12.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0|^12.0
- monolog/monolog: ^2.0|^3.0
Requires (Dev)
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0|^10.0
README
A simple Laravel package to send log messages to a Telegram chat.
Installation
You can install the package via composer:
composer require arhx/telegram-log-channel
The service provider will be automatically registered.
Configuration
-
Add the necessary environment variables to your
.envfile:TELEGRAM_LOG_BOT_TOKEN=your_bot_token_here TELEGRAM_LOG_CHAT_ID=your_chat_id_here
TELEGRAM_LOG_BOT_TOKEN: Your Telegram bot's token.TELEGRAM_LOG_CHAT_ID: The ID of the chat where logs should be sent.TELEGRAM_LOG_LEVEL: (Optional) The minimum log level to be sent (defaults toerror).
-
(Optional) The package comes with a default configuration for the
telegramlog channel. If you need to customize it, you can add your own channel configuration toconfig/logging.php:'channels' => [ // ... other channels 'telegram' => [ 'driver' => 'telegram', 'token' => env('TELEGRAM_LOG_BOT_TOKEN'), 'chat_id' => env('TELEGRAM_LOG_CHAT_ID'), 'level' => env('TELEGRAM_LOG_LEVEL', 'debug'), // Example of overriding the level ], ],
Usage
To receive Telegram notifications for your logs, add the telegram channel to your chosen logging stack in config/logging.php.
If the environment variables TELEGRAM_LOG_BOT_TOKEN and TELEGRAM_LOG_CHAT_ID are not set, the telegram channel will gracefully fallback to a NullHandler, meaning no logs will be sent to Telegram and the application will not crash.
For example, to add it to the default stack channel:
'stack' => [ 'driver' => 'stack', 'channels' => ['daily', 'telegram'], // Add 'telegram' here 'ignore_exceptions' => false, ],
Now, any log message that meets the configured level will be sent to your Telegram chat.
Tip for Laravel 12: You might just need to update your
.envfile to includetelegramin the logging stack:LOG_STACK=daily,telegram
Testing
In a Laravel Application
After installing the package in your Laravel app, you can test your configuration by running:
php artisan telegram-log:test
Note: This command sends a message with the
errorlevel. If yourTELEGRAM_LOG_LEVELis set to a higher level (e.g.,emergency), the test message will not be sent.
During Package Development
If you are developing the package, you can run the command directly from the root:
- Copy
.env.example(if available) to.envor create it manually with your bot token and chat ID. - Run:
php artisan telegram-log:test