congnv/monolog-telegram-handler

Telegram Handler for Monolog.

1.0.1 2024-05-06 03:41 UTC

This package is auto-updated.

Last update: 2024-05-06 03:44:52 UTC


README

Telegram handler for Monolog.

Install

composer require congnv/monolog-telegram-handler

Declaring handler object

To declare this handler, you need to know the bot token and the chat identifier(chat_id) to which the log will be sent.

$handler = new \TelegramLog\TelegramHandler('<token>', '<chat_id>');

Using with Laravel

Add telegram channel to config/logging.php

'telegram' => [
    'driver'  => 'custom',
    'via' => \TelegramLog\TelegramLogger::class,
    'level' => env('LOG_LEVEL', 'debug'),
    'bot_token' => env('TELEGRAM_BOT_TOKEN'),
    'chat_id' => env('TELEGRAM_CHAT_ID'),
],

Custom message formatter

Create new class implements \TelegramLog\TelegramMessageFormatterInterface

Example

<?php

namespace App\Logging\Formatter;

use Monolog\LogRecord;
use TelegramLog\TelegramMessageFormatterInterface;

class TelegramMessageFormatter implements TelegramMessageFormatterInterface
{
    public function format(LogRecord $record, int $maxStackLine): string
    {
        return [{$record->datetime->format('Y-m-d H:i:s')}] {$record->message}";
    }
}

Modify telegram channel

'telegram' => [
    'driver'  => 'custom',
    'via' => \TelegramLog\TelegramLogger::class,
    'formatter' => \App\Logging\Formatter\TelegramMessageFormatter::class,
    'level' => env('LOG_LEVEL', 'debug'),
    'bot_token' => env('TELEGRAM_BOT_TOKEN'),
    'chat_id' => env('TELEGRAM_CHAT_ID'),
],