Home >Backend Development >PHP Tutorial >Sending logs to Telegram. Module for Laravel

Sending logs to Telegram. Module for Laravel

Linda Hamilton
Linda HamiltonOriginal
2025-01-10 22:04:43969browse

Sending logs to Telegram. Module for Laravel

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.

GitHub Repository

Module Setup

  1. Create a Telegram Bot: Generate a bot and obtain its token.

  2. Create a Telegram Group: Create a group, enable "Themes," and add your bot as an administrator.

  3. 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>
  1. Install via Composer:
<code class="language-bash">composer require prog-time/tg-logger</code>
  1. Publish Configuration: Create or publish the configuration file:
<code class="language-bash">php artisan vendor:publish --tag=config</code>
  1. Configure 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.
  1. Create Telegram Topics: Run this artisan command to create the topics within your Telegram group:
<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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn