DEV Community

Cover image for Sending logs to Telegram. Module for Laravel
Илья Лящук
Илья Лящук

Posted on

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.

Top comments (0)