<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Илья Лящук</title>
    <description>The latest articles on DEV Community by Илья Лящук (@progtime).</description>
    <link>https://dev.to/progtime</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2686240%2Fb50b3fe3-378c-4f8e-b1cc-214cb229e482.jpeg</url>
      <title>DEV Community: Илья Лящук</title>
      <link>https://dev.to/progtime</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/progtime"/>
    <language>en</language>
    <item>
      <title>Sending logs to Telegram. Module for Laravel</title>
      <dc:creator>Илья Лящук</dc:creator>
      <pubDate>Fri, 10 Jan 2025 13:55:21 +0000</pubDate>
      <link>https://dev.to/progtime/sending-logs-to-telegram-module-for-laravel-4fb2</link>
      <guid>https://dev.to/progtime/sending-logs-to-telegram-module-for-laravel-4fb2</guid>
      <description>&lt;p&gt;Hello everyone! In this post, I would like to share with you a module that I have developed for Laravel.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/prog-time/tg-logger" rel="noopener noreferrer"&gt;https://github.com/prog-time/tg-logger&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is my first experience in developing public modules for Laravel, so please do not judge strictly!&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring the module
&lt;/h2&gt;

&lt;p&gt;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).&lt;/p&gt;

&lt;p&gt;After creating the bot, we write the bot token and the group id to the .env file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TG_LOGGER_TOKEN="token_bot"
TG_LOGGER_CHAT_ID="id_group"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We install the module via Composer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require prog-time/tg-logger
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;You can create the file manually or transfer the prepared version of the configuration, which is located inside the module, using the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan vendor:publish --tag=config
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's fill in the created configuration file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return [
    'token' =&amp;gt; env('TG_LOGGER_TOKEN'),
    'chat_id' =&amp;gt; env('TG_LOGGER_CHAT_ID'),
    'topics' =&amp;gt; [
        [
            'name' =&amp;gt; 'Debug messages',
            'icon_color' =&amp;gt; '9367192',
            'level' =&amp;gt; 'debug',
        ],
        [
            'name' =&amp;gt; 'Cron tasks',
            'icon_color' =&amp;gt; '9367192',
            'level' =&amp;gt; 'crone',
        ],
        [
            'name' =&amp;gt; 'Errors',
            'icon_color' =&amp;gt; '9367192',
            'level' =&amp;gt; 'error, notice, warning, emergency',
        ]
    ]
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Configuration &lt;strong&gt;tg-logger.php&lt;/strong&gt; It includes the following parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;token — a Telegram bot token&lt;/li&gt;
&lt;li&gt;chat_id is the ID of the group where we will send logs to&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After setting up &lt;strong&gt;tg-logger.php&lt;/strong&gt; you need to run the command that will create the topics in the group.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan tglogger:create-topics
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*After running this command, the file tg-logger.php it will be overwritten and the topic IDs will be specified in it.&lt;/p&gt;

&lt;p&gt;This completes the configuration of the module, below we will look at how to work with the TgLogger module.&lt;/p&gt;

&lt;h2&gt;
  
  
  Working with the TgLogger module
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Catching system errors
&lt;/h3&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'channels' =&amp;gt; [
    ...
    'telegram' =&amp;gt; [
        'driver' =&amp;gt; 'monolog',
        'handler' =&amp;gt; ProgTime\TgLogger\TgHandler::class,
        'formatter' =&amp;gt; ProgTime\TgLogger\TgFormatter::class,
        'level' =&amp;gt; 'debug',
    ],
    ...
],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in .env, change the &lt;strong&gt;LOG_CHANNEL&lt;/strong&gt; parameter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LOG_CHANNEL=telegram
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Sending messages via the TgLogger class
&lt;/h2&gt;

&lt;p&gt;You can also send alerts directly using the TgLogger class and the static sendLog() method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TgLogger::sendLog('Your message', 'level');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>telegram</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
