|
1 | 1 | ## Logging |
2 | | -PHP Telegram Bot library features [Monolog](https://github.com/Seldaek/monolog) to store logs. |
| 2 | +PHP Telegram Bot library features [PSR-3] compatible logging to store logs. |
| 3 | + |
| 4 | +You can find a list of compatible packages that can be used on [Packagist][PSR-3-providers]. |
3 | 5 |
|
4 | 6 | Logs are divided into the following streams: |
5 | | -### Error |
6 | | -Collects all the exceptions thrown by the library: |
7 | | -```php |
8 | | -TelegramLog::initErrorLog($path . '/' . $BOT_NAME . '_error.log'); |
9 | | -``` |
| 7 | +- `error`: Collects all the exceptions thrown by the library. |
| 8 | +- `debug`: Stores requests made to the Telegram API, useful for debugging. |
| 9 | +- `update`: Incoming raw updates (JSON string from Webhook and getUpdates). |
| 10 | + |
| 11 | +### Initialisation |
| 12 | +To initialise the logger, you can pass any `LoggerInterface` objects to the `TelegramLog::initialize` method. |
| 13 | + |
| 14 | +The first parameter is the main logger, the second one is used for the raw updates. |
10 | 15 |
|
11 | | -### Debug |
12 | | -Stores requests made to the Telegram API, useful for debugging: |
| 16 | +(in this example we're using [Monolog]) |
13 | 17 | ```php |
14 | | -TelegramLog::initDebugLog($path . '/' . $BOT_NAME . '_debug.log'); |
| 18 | +use Monolog\Formatter\LineFormatter; |
| 19 | +use Monolog\Handler\StreamHandler; |
| 20 | +use Monolog\Logger; |
| 21 | +use Longman\TelegramBot\TelegramLog; |
| 22 | + |
| 23 | +TelegramLog::initialize( |
| 24 | + // Main logger that handles all 'error' and 'debug' logs. |
| 25 | + new Logger('telegram_bot', [ |
| 26 | + (new StreamHandler(__DIR__ . "/logs/{$bot_username}_debug.log", Logger::DEBUG))->setFormatter(new LineFormatter(null, null, true)), |
| 27 | + (new StreamHandler(__DIR__ . "/logs/{$bot_username}_error.log", Logger::ERROR))->setFormatter(new LineFormatter(null, null, true)), |
| 28 | + ]), |
| 29 | + // Updates logger for raw updates. |
| 30 | + new Logger('telegram_bot_updates', [ |
| 31 | + (new StreamHandler(__DIR__ . "/logs/{$bot_username}_update.log", Logger::INFO))->setFormatter(new LineFormatter('%message%' . PHP_EOL)), |
| 32 | + ]) |
| 33 | +); |
15 | 34 | ``` |
16 | 35 |
|
17 | 36 | ### Raw data |
18 | | -Incoming updates (JSON string from Webhook and getUpdates) get logged in a text file: |
19 | | -```php |
20 | | -TelegramLog::initUpdateLog($path . '/' . $BOT_NAME . '_update.log'); |
21 | | -``` |
22 | 37 | Why do I need to log the raw updates? |
23 | 38 | 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. |
24 | 39 |
|
25 | 40 | If you store the raw data you can import all updates on the newest table schema by simply using [this script](../utils/importFromLog.php). |
26 | 41 | Remember to always backup first!! |
27 | 42 |
|
28 | | -## Stream and external sources |
29 | | -Error and Debug streams rely on the `bot_log` instance that can be provided from an external source: |
30 | | -```php |
31 | | -TelegramLog::initialize($monolog); |
32 | | -``` |
33 | 43 |
|
34 | | -Raw data relies on the `bot_update_log` instance that uses a custom format. |
| 44 | +[PSR-3]: https://www.php-fig.org/psr/psr-3 |
| 45 | +[PSR-3-providers]: https://packagist.org/providers/psr/log-implementation |
| 46 | +[Monolog]: https://github.com/Seldaek/monolog |
0 commit comments