Skip to content

Latest commit

 

History

History
83 lines (63 loc) · 1.89 KB

File metadata and controls

83 lines (63 loc) · 1.89 KB

Logging

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.

General usage

Pass logger to TelegramBotApi constructor:

use Psr\Log\LoggerInterface;
use Phptg\BotApi\TelegramBotApi;

/**
 * @var string $token
 * @var LoggerInterface $logger
 */

// API
$api = new TelegramBotApi($token, logger: $logger);

You can use logger on create Update object also:

use Psr\Log\LoggerInterface;
use Phptg\BotApi\Type\Update\Update;

/**
 * @var string $jsonString
 * @var LoggerInterface $logger
 */

$update = Update::fromJson($jsonString, $logger);

Log context

To logger passed 4 types of messages.

  1. On send request info message with context:
[
    'type' => LogType::SEND_REQUEST,
    'payload' => $payload, // Request data as array with string keys
    'method' => $method, // `MethodInterface` implementation
]
  1. On success result info message with context:
[
    'type' => LogType::SUCCESS_RESULT,
    'payload' => $payload, // Decoded response body as array
    'method' => $method, // `MethodInterface` implementation
    'response' => $response, // `ApiResponse` object
    'decodedResponse' => $decodedResponse, // Decoded response body as array 
]
  1. On fail result warning message with context:
[
    'type' => LogType::FAIL_RESULT,
    'payload' => $payload, // Response body as string
    'method' => $method, // `MethodInterface` implementation
    'response' => $response, // `ApiResponse` object
    'decodedResponse' => $decodedResponse, // Decoded response body as array 
]
  1. On parse result error error message with context:
[
    'type' => LogType::PARSE_RESULT_ERROR,
    'payload' => $payload, // Raw parsed data as string
]