Skip to content

Latest commit

 

History

History
46 lines (36 loc) · 2 KB

01-utils.md

File metadata and controls

46 lines (36 loc) · 2 KB

Logging

PHP Telegram Bot library features PSR-3 compatible logging to store logs.

You can find a list of compatible packages that can be used on Packagist.

Logs are divided into the following streams:

  • error: Collects all the exceptions thrown by the library.
  • debug: Stores requests made to the Telegram API, useful for debugging.
  • update: Incoming raw updates (JSON string from Webhook and getUpdates).

Initialisation

To initialise the logger, you can pass any LoggerInterface objects to the TelegramLog::initialize method.

The first parameter is the main logger, the second one is used for the raw updates.

(in this example we're using Monolog)

use Longman\TelegramBot\TelegramLog;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

TelegramLog::initialize(
    // Main logger that handles all 'debug' and 'error' logs.
    new Logger('telegram_bot', [
        (new StreamHandler($path_to_debug_log_file, Logger::DEBUG))->setFormatter(new LineFormatter(null, null, true)),
        (new StreamHandler($path_to_error_log_file, Logger::ERROR))->setFormatter(new LineFormatter(null, null, true)),
    ]),
    // Updates logger for raw updates.
    new Logger('telegram_bot_updates', [
        (new StreamHandler($path_to_updates_log_file, Logger::INFO))->setFormatter(new LineFormatter('%message%' . PHP_EOL)),
    ])
);

Raw data

Why do I need to log the raw updates? Telegram API changes continuously and it often happens that the database schema is not up to date with new entities/features. So it can happen that your table schema doesn't allow storing new valuable information coming from Telegram.

If you store the raw data you can import all updates on the newest table schema by simply using this script. Remember to always backup first!!