This Laravel module simplifies sending logs and error messages to Telegram. It's ideal for smaller projects needing a straightforward logging solution. While more advanced options exist, this module prioritizes ease of setup and configuration.
Module Setup
Create a Telegram Bot: Generate a bot and obtain its token.
Create a Telegram Group: Create a group, enable "Themes," and add your bot as an administrator.
Configure .env
: Add your bot's token and the group's ID to your .env
file:
<code>TG_LOGGER_TOKEN="your_bot_token" TG_LOGGER_CHAT_ID="your_group_id"</code>
<code class="language-bash">composer require prog-time/tg-logger</code>
<code class="language-bash">php artisan vendor:publish --tag=config</code>
config/tg-logger.php
: Populate the configuration file with your settings:<code class="language-php">return [ 'token' => env('TG_LOGGER_TOKEN'), 'chat_id' => env('TG_LOGGER_CHAT_ID'), 'topics' => [ [ 'name' => 'Debug messages', 'icon_color' => '9367192', 'level' => 'debug', ], [ 'name' => 'Cron tasks', 'icon_color' => '9367192', 'level' => 'crone', ], [ 'name' => 'Errors', 'icon_color' => '9367192', 'level' => 'error, notice, warning, emergency', ] ] ];</code>
The tg-logger.php
file uses these parameters:
token
: Your Telegram bot token.chat_id
: Your Telegram group ID.topics
: An array defining log topic names, icon colors, and associated log levels.<code class="language-bash">php artisan tglogger:create-topics</code>
This will overwrite tg-logger.php
and add the topic IDs.
Using the TgLogger Module
A. Handling System Errors:
Modify your config/logging.php
file to use the module's handlers:
<code class="language-php">'channels' => [ ... 'telegram' => [ 'driver' => 'monolog', 'handler' => ProgTime\TgLogger\TgHandler::class, 'formatter' => ProgTime\TgLogger\TgFormatter::class, 'level' => 'debug', ], ... ],</code>
Then, set LOG_CHANNEL=telegram
in your .env
file.
B. Sending Messages Directly:
Use the TgLogger
class to send messages directly:
<code class="language-php">TgLogger::sendLog('Your message', 'level');</code>
Your feedback and contributions on GitHub are welcome!
The above is the detailed content of Sending logs to Telegram. Module for Laravel. For more information, please follow other related articles on the PHP Chinese website!