Skip to content

Commit a933b64

Browse files
committed
Update logger documentation.
[skip ci]
1 parent 6203da7 commit a933b64

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

doc/01-utils.md

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,46 @@
11
## 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].
35

46
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.
1015

11-
### Debug
12-
Stores requests made to the Telegram API, useful for debugging:
16+
(in this example we're using [Monolog])
1317
```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+
);
1534
```
1635

1736
### 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-
```
2237
Why do I need to log the raw updates?
2338
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.
2439

2540
If you store the raw data you can import all updates on the newest table schema by simply using [this script](../utils/importFromLog.php).
2641
Remember to always backup first!!
2742

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-
```
3343

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

Comments
 (0)