-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Declaration of Handler::write(array $record): void must be compatible with AbstractProcessingHandler::write(Monolog\LogRecord $record): void #81
Comments
I've changed in Handler.php following: use Monolog\LogRecord; public function write(LogRecord $record): void private function formatText(LogRecord $record): string return view('telebot::log', array_merge(['formatted' => $record->formatted], $record->toArray(), [ Here is entire updated class: <?php
namespace WeStacks\TeleBot\Laravel\Log;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\LogRecord;
use Monolog\Logger;
use WeStacks\TeleBot\TeleBot;
class Handler extends AbstractProcessingHandler
{
/**
* Bot instance.
*
* @var TeleBot
*/
protected $bot;
/**
* Chat id to send log message.
*
* @var string
*/
protected $chat_id;
/**
* App name.
*
* @var string
*/
protected $app;
/**
* App env.
*
* @var string
*/
protected $env;
public function __construct(array $config)
{
$level = Logger::toMonologLevel($config['level']);
parent::__construct($level, true);
// define variables for making Telegram request
$this->bot = app('telebot')->bot($config['bot'] ?? null);
$this->chat_id = $config['chat_id'];
// define variables for text message
$this->app = config('app.name');
$this->env = config('app.env');
}
public function write(LogRecord $record): void
{
$textChunks = str_split($this->formatText($record), 4096);
foreach ($textChunks as $textChunk) {
$this->sendMessage($textChunk);
}
}
/**
* {@inheritDoc}
*/
protected function getDefaultFormatter(): FormatterInterface
{
return new LineFormatter("%message% %context% %extra%\n");
}
private function formatText(LogRecord $record): string
{
return view('telebot::log', array_merge(['formatted' => $record->formatted], $record->toArray(), [
'app' => $this->app,
'env' => $this->env,
]))->render();
}
private function sendMessage(string $text): void
{
$this->bot->exceptions(false)->async(false)->sendMessage([
'chat_id' => $this->chat_id,
'parse_mode' => 'html',
'text' => $text,
]);
}
} |
Here is my logging config 'telegram' => [
'formatter' => Monolog\Formatter\LineFormatter::class,
'driver' => 'custom',
'via' => TelegramLogger::class,
'level' => 'debug',
'bot' => 'bot',
'chat_id' => env('TELEGRAM_LOG_CHAT_ID')
] Idk if |
This is just a hotfix for my version, please do not copy&paste. |
Hello, thanks a lot for your bug request. This seems to be changed in Monolog v3. Fixed in fa70e8c |
You're welcome! Nice bot integration repo! I've used it in my diploma project in university:) |
Small notice about your change commit. You've written type hint for array and LogRecord object, but you do ->toArray() for object, but when array will be passed it would throw an error that it is not possible to call toArray method for array. |
There is a check if passed record is an instance of if (is_a($record, LogRecord::class)) {
$record = array_merge($record->toArray(), ['formatted' => $record->formatted]);
} |
Describe the bug
Broken symfony logging bundle
To Reproduce
Steps to reproduce the behavior:
laravel
Expected behavior
Message sent to the chat id
Screenshots
![image](https://private-user-images.githubusercontent.com/7810087/249160841-47ae9d76-edde-41aa-affc-bbc7499ddd71.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI5ODI2NjgsIm5iZiI6MTcyMjk4MjM2OCwicGF0aCI6Ii83ODEwMDg3LzI0OTE2MDg0MS00N2FlOWQ3Ni1lZGRlLTQxYWEtYWZmYy1iYmM3NDk5ZGRkNzEucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgwNiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MDZUMjIxMjQ4WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZjA1MGMxNGFlMWMxOTU0YjQwYzc3ODlhZjZiMWM4OTFjMGFmZjMzNjI4YWQxZTZjMTRkNjdkMjUwODdhNjExOCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.NalA9C6180J1vhrhUZiHhZpW6dEYB9axPuWTpMFugX4)
Additional context
Bundle v2.7.1
The text was updated successfully, but these errors were encountered: