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

Sending logs to Telegram. Module for Laravel

Linda Hamilton
Release: 2025-01-10 22:04:43
Original
906 people have browsed it

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>
Copy after login
  1. Install via Composer:
<code class="language-bash">composer require prog-time/tg-logger</code>
Copy after login
  1. Publish Configuration: Create or publish the configuration file:
<code class="language-bash">php artisan vendor:publish --tag=config</code>
Copy after login
  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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template