How to Use Telegram for Storing Laravel Logs

How to Use Telegram for Storing Laravel Logs

Introduction

There are many services for storing application logs on a large scale. They provide cool features, such as searching by keyword, date, etc. However, these services usually charge based on log volume, retention period, or number of access points which can add up to huge sums for large applications. Lately I’ve been searching for cheaper ways to store logs for one of the applications I was working on and discovered that it is possible to store logs in Telegram’s private channels, which allow up to 30 messages per second traffic to a single channel. In addition, these channels can store significant amounts of searchable messages for free. So I’ve decided to try it out on a Laravel application and here’s how you can do that too.

Creating a Private Channel

First off, you’ll need a private Telegram channel. Depending on your Telegram version, the setup can vary, but for me it looked something like this:

For more information, please refer to TELEGRAM FAQ.

Creating a Telegram Bot

Next, in order to send messages, you need a Telegram bot with administrator access to your channel. Open Telegram, search for the BotFather bot, and start a conversation. Send the /newbot command to BotFather and follow the instructions. You will need to choose a name and a username for your bot. After the bot is created, you will receive a token to access the HTTP API. Keep this token secure as it will be used in your Laravel application.

Once you’ve created a bot, you need to add it to your private channel:

Finding Out Chat ID

Next, you’ll need to find out the channel’s chat ID. Open Telegram and search for @getidsbot. You can forward a message from your private channel to it and it will give all the information available about the sender and channel. The channel’s chat ID will likely be a negative number looking something like -1001234567890.

Creating a Custom Monolog Handler

Now that you have all the necessary credentials, it’s time to create a new logging channel within your Laravel application. First, you create a custom handler in order to send logs to the Telegram channel. Since the logging involves an API call, I suggest to create a job that makes that call instead of blocking the main thread and waiting for the response:

This way you’ll have more control over the log traffic in case you exceed any rate limits. I created a separate queue for the log jobs with a small timeout allowance:

Next, create a new logging channel in config/logging.php:

Sending Logs

Once you’ve configured the logging channel, set LOG_CHANNEL to telegram in .env, request any endpoint that logs something, and see if you receive logs in the Telegram channel:

Note: Make sure you have the queued jobs executed by running php artisan queue:work or setting up Horizon for more control over the jobs.

Bugsnag

If you’re using Bugsnag in your Laravel application, you can pair up both channels by using the stack logging channel so your application can send exceptions to Bugsnag and regular logs to Telegram:

References