Skip to content

Commit 0e5821b

Browse files
authored
Merge pull request #964 from noplanman/psr3-logger
Use PSR-3 LoggerInterface to allow custom logging implementations.
2 parents c8329d6 + 377173e commit 0e5821b

File tree

7 files changed

+232
-164
lines changed

7 files changed

+232
-164
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
1111
- Use PSR-12 for code style.
1212
- Some general housekeeping. (#972)
1313
- [:exclamation:][unreleased-bc] Return an empty array for Entity properties with no items, instead of `null`. (#969)
14+
- `TelegramLog` now adheres to [PSR-3] `LoggerInterface` and allows custom logger implementations.
1415
### Deprecated
16+
- Old logging that uses Monolog still works but will be removed in the near future. Use `TelegramLog::initialize($logger, $update_logger);` from now on.
1517
### Removed
1618
- Botan.io integration completely removed.
1719
### Fixed
@@ -290,6 +292,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
290292
[0.45.0-bc-up-download-directory]: https://github.com/php-telegram-bot/core/wiki/Breaking-backwards-compatibility#up-download-directory
291293
[0.44.0-bc-update-content-type]: https://github.com/php-telegram-bot/core/wiki/Breaking-backwards-compatibility#update-getupdatecontent
292294
[example-bot]: https://github.com/php-telegram-bot/example-bot
295+
[PSR-3]: https://www.php-fig.org/psr/psr-3
293296

294297
[Unreleased]: https://github.com/php-telegram-bot/core/compare/master...develop
295298
[0.57.0]: https://github.com/php-telegram-bot/core/compare/0.56.0...0.57.0

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"ext-curl": "*",
2929
"ext-json": "*",
3030
"ext-mbstring": "*",
31+
"psr/log": "^1.1",
3132
"monolog/monolog": "^1.24",
3233
"guzzlehttp/guzzle": "^6.3"
3334
},

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

src/Telegram.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ protected function sanitizeCommand($command)
538538
public function enableAdmin($admin_id)
539539
{
540540
if (!is_int($admin_id) || $admin_id <= 0) {
541-
TelegramLog::error('Invalid value "%s" for admin.', $admin_id);
541+
TelegramLog::error('Invalid value "' . $admin_id . '" for admin.');
542542
} elseif (!in_array($admin_id, $this->admins_list, true)) {
543543
$this->admins_list[] = $admin_id;
544544
}
@@ -631,7 +631,7 @@ public function isDbEnabled()
631631
public function addCommandsPath($path, $before = true)
632632
{
633633
if (!is_dir($path)) {
634-
TelegramLog::error('Commands path "%s" does not exist.', $path);
634+
TelegramLog::error('Commands path "' . $path . '" does not exist.');
635635
} elseif (!in_array($path, $this->commands_paths, true)) {
636636
if ($before) {
637637
array_unshift($this->commands_paths, $path);

0 commit comments

Comments
 (0)