You can use any PSR-3 compatible logger to log requests to Telegram Bot API and response handling errors. For example, Monolog or Yii Log.
Pass logger to TelegramBotApi
constructor:
use Psr\Log\LoggerInterface;
use Vjik\TelegramBot\Api\Client\TelegramClientInterface;
use Vjik\TelegramBot\Api\TelegramBotApi;
/**
* @var TelegramClientInterface $telegramClient
* @var LoggerInterface $logger
*/
// API
$api = new TelegramBotApi($telegramClient, $logger);
You can use logger on create Update
object also:
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Vjik\TelegramBot\Api\Type\Update\Update;
/**
* @var ServerRequestInterface $request
* @var string $jsonString
* @var LoggerInterface $logger
*/
$update = Update::fromServerRequest($request, $logger);
$update = Update::fromJson($jsonString, $logger);
To logger passed 4 types of messages.
- On send request
info
message with context:
[
'type' => LogType::SEND_REQUEST,
'payload' => $payload, // Request data as JSON encoded string
'request' => $request, // `TelegramRequestInterface` implementation
]
- On success result
info
message with context:
[
'type' => LogType::SUCCESS_RESULT,
'payload' => $payload, // Response body as JSON encoded string
'request' => $request, // `TelegramRequestInterface` implementation
'response' => $response, // `TelegramResponse` object
'decodedResponse' => $decodedResponse, // Decoded response body as array
]
- On fail result
warning
message with context:
[
'type' => LogType::FAIL_RESULT,
'payload' => $payload, // Response body as JSON encoded string
'request' => $request, // `TelegramRequestInterface` implementation
'response' => $response, // `TelegramResponse` object
'decodedResponse' => $decodedResponse, // Decoded response body as array
]
- On parse result error
error
message with context:
[
'type' => LogType::PARSE_RESULT_ERROR,
'payload' => $payload, // Raw parsed data as string
]