John Davidson

php - Combination of monolog telegram handlers

0 comments
Message:


I have some issues to send log to two Telegram private channels.
I want to send "debug" and "info" messages to my channel "debug", and only "info" message to my channel "prod"


See my services.yaml file


services:
telegramHandlerDebug:
class: App\Formatter\TelegramHandler
arguments:
- '%env(TELEGRAM_TOKEN)%'
- '%env(TELEGRAM_CHANNEL_DEBUG)%'
calls:
- method: setFormatter
arguments:
- '@telegramFormatter'

telegramHandlerProd:
class: App\Formatter\TelegramHandler
arguments:
- '%env(TELEGRAM_TOKEN)%'
- '%env(TELEGRAM_CHANNEL_PROD)%'
calls:
- method: setFormatter
arguments:
- '@telegramFormatter'

telegramFormatter:
class: App\Formatter\TelegramFormatter
arguments:
$html: true
$format: '%%message%%'
$separator: ''

my monolog.yaml


monolog:
channels: ['telegram']
handlers:
telegram:
type: buffer
channels: telegram
handler: grouped_telegram

grouped_telegram:
type: group
members: [handler_telegram_debug, handler_telegram_prod]
formatter: telegramFormatter

handler_telegram_debug:
type: fingers_crossed
action_level: debug
level: debug
channels: telegram
handler: service_telegram_debug
handler_telegram_prod:
type: fingers_crossed
action_level: info
level: info
channels: telegram
handler: service_telegram_prod

service_telegram_debug:
type: service
id: telegramHandlerDebug
service_telegram_prod:
type: service
id: telegramHandlerProd


and extract of my crontroller


   public function index(LoggerInterface $telegramLogger)
{
$telegramLogger->log('debug', 'Message type "debug" : channels debug');
$telegramLogger->log('info', 'Message type "info" : channels debug + prod');
}

When i run my controller, i received the two messages in my two channels, without distinctions.
I'm not familiar with key "action_level" and "type:finger_crossed", maybe issue is here.



My Answer:
logs to two different Telegram private channels using Monolog handlers in PHP. Here is an example code snippet that demonstrates how you can achieve this:

php
use Monolog\Logger;
use Monolog\Handler\TelegramBotHandler;
use Monolog\Handler\TelegramBotHandler\TelegramBotHandler;

// Create a Monolog logger instance
$logger = new Logger('my_logger');

// Create a TelegramBotHandler instance for the first channel
$telegramHandler1 = new TelegramBotHandler('YOUR_BOT_TOKEN', 'YOUR_CHAT_ID_1', Logger::DEBUG);
$logger->pushHandler($telegramHandler1);

// Create a TelegramBotHandler instance for the second channel
$telegramHandler2 = new TelegramBotHandler('YOUR_BOT_TOKEN', 'YOUR_CHAT_ID_2', Logger::DEBUG);
$logger->pushHandler($telegramHandler2);

// Log a message to both channels
$logger->info('This is a test message for both channels');


In this code snippet, we first create a Monolog logger instance with the name 'my_logger'. Then, we create two TelegramBotHandler instances for each of the private channels we want to send logs to. We push these handlers to the logger instance using the `pushHandler` method.

Finally, we log a test message using the `info` method of the logger, which will be sent to both Telegram channels.

Make sure to replace 'YOUR_BOT_TOKEN', 'YOUR_CHAT_ID_1', and 'YOUR_CHAT_ID_2' with your actual Telegram bot token and chat IDs for the respective channels.

I hope this helps! Let me know if you have any further questions.

Rate this post

5 of 5 based on 9290 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us