Logo

dev-resources.site

for different kinds of informations.

Sending logs to Telegram. Module for Laravel

Published at
1/10/2025
Categories
php
laravel
telegram
programming
Author
progtime
Categories
4 categories in total
php
open
laravel
open
telegram
open
programming
open
Author
8 person written this
progtime
open
Sending logs to Telegram. Module for Laravel

Hello everyone! In this post, I would like to share with you a module that I have developed for Laravel.

https://github.com/prog-time/tg-logger

This is my first experience in developing public modules for Laravel, so please do not judge strictly!

This module allows you to easily and quickly set up the sending of logs and error messages to the Telegram community, where you can select a separate topic for each type of event. This way you can send messages about custom events, exceptions, and system errors.

Of course, there are more advanced logging solutions, but they require deeper configuration. I wanted to create a module that would be quick to set up and well suited for small and medium-sized projects.

Configuring the module

First, you need to create a Telegram bot that will be responsible for sending notifications. After that, we create a group, enable "Themes" in it and add the created bot to the group (necessarily with administrator rights).

After creating the bot, we write the bot token and the group id to the .env file.

TG_LOGGER_TOKEN="token_bot"
TG_LOGGER_CHAT_ID="id_group"
Enter fullscreen mode Exit fullscreen mode

We install the module via Composer.

composer require prog-time/tg-logger
Enter fullscreen mode Exit fullscreen mode

After installing the module, you need to create a configuration file. config/tg-logger.php and write in it the parameters for the operation of the module.

You can create the file manually or transfer the prepared version of the configuration, which is located inside the module, using the following command.

php artisan vendor:publish --tag=config
Enter fullscreen mode Exit fullscreen mode

Now let's fill in the created configuration file.

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',
        ]
    ]
];
Enter fullscreen mode Exit fullscreen mode

Configuration tg-logger.php It includes the following parameters:

  • token β€” a Telegram bot token
  • chat_id is the ID of the group where we will send logs to
  • topics β€” here we specify the names of the topics, the types of logs that the theme will accept, and the colors of the theme icons.

After setting up tg-logger.php you need to run the command that will create the topics in the group.

php artisan tglogger:create-topics
Enter fullscreen mode Exit fullscreen mode

*After running this command, the file tg-logger.php it will be overwritten and the topic IDs will be specified in it.

This completes the configuration of the module, below we will look at how to work with the TgLogger module.

Working with the TgLogger module

Catching system errors

To catch all types of errors, you need to change the basic log handler in the configuration file. config/logging.php by specifying the module classes as handlers.

'channels' => [
    ...
    'telegram' => [
        'driver' => 'monolog',
        'handler' => ProgTime\TgLogger\TgHandler::class,
        'formatter' => ProgTime\TgLogger\TgFormatter::class,
        'level' => 'debug',
    ],
    ...
],
Enter fullscreen mode Exit fullscreen mode

And in .env, change the LOG_CHANNEL parameter.

LOG_CHANNEL=telegram
Enter fullscreen mode Exit fullscreen mode

Sending messages via the TgLogger class

You can also send alerts directly using the TgLogger class and the static sendLog() method.

TgLogger::sendLog('Your message', 'level');
Enter fullscreen mode Exit fullscreen mode

Many thanks to those who read this article to the end! I will be very glad if you support this decision on GitHub and write your comment under this post.

php Article's
30 articles in total
Favicon
The Importance of Writing Meaningful Code and Documentation
Favicon
Filling a 10 Million Image Grid with PHP for Internet History
Favicon
Code Smell 286 - Overlapping Methods
Favicon
Example of using Late Static Binding in PHP.
Favicon
How to Resolve the 'Permission Denied' Error in PHP File Handling
Favicon
2429. Minimize XOR
Favicon
The Ultimate PHP QR Code Library
Favicon
Understanding PHP Development and Why It’s Still Relevant Today
Favicon
2657. Find the Prefix Common Array of Two Arrays
Favicon
Php Base64 encode/decode – best practices and use cases
Favicon
Laravel 11.30: A Leap Forward in Testing, Model IDs, and Authorization
Favicon
How to Effectively Manage Laravel Request Validation?
Favicon
3223. Minimum Length of String After Operations
Favicon
Author Bio Box CSS in WordPress
Favicon
[Boost]
Favicon
How to Image Upload with CKeditor in Laravel 11 Tutorial
Favicon
How to Install and Use Trix Editor in Laravel 11
Favicon
Testing Temporary URLs in Laravel Storage
Favicon
2116. Check if a Parentheses String Can Be Valid
Favicon
API Vulnerabilities in Laravel: Identify & Secure Your Endpoints
Favicon
Enforcing Strong Passwords in Laravel
Favicon
"PHP is dead⚰️" .. what's next? Is Laravel worth it? 😎
Favicon
LTS as a Business: How an Old Project Can Become the Foundation for a New Business Model
Favicon
Php
Favicon
How to Fix the "PHP Not Found" Error on macOS After Installing XAMPP
Favicon
The Hidden Bug That Crashed a Satellite: Lessons for Every Developer πŸš€
Favicon
Sending logs to Telegram. Module for Laravel
Favicon
Reflecting on 2024: From CodeIgniter to Laravel and Building Integrated Solutions
Favicon
Host Header Injection in Laravel: Risks and Prevention
Favicon
CodeIgniter Monitoring Library – Born from Understanding Real Developer Needs

Featured ones: