Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
## [Unreleased]
### Added
### Changed
- `TelegramLog` now adheres to [PSR-3] `LoggerInterface` and allows custom logger implementations.
### Deprecated
- Old logging that uses Monolog still works but will be removed in the near future. Use `TelegramLog::initialize($logger, $update_logger);` from now on.
### Removed
### Fixed
### Security
Expand Down Expand Up @@ -280,6 +282,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
[0.45.0-bc-up-download-directory]: https://github.com/php-telegram-bot/core/wiki/Breaking-backwards-compatibility#up-download-directory
[0.44.0-bc-update-content-type]: https://github.com/php-telegram-bot/core/wiki/Breaking-backwards-compatibility#update-getupdatecontent
[example-bot]: https://github.com/php-telegram-bot/example-bot
[PSR-3]: https://www.php-fig.org/psr/psr-3

[Unreleased]: https://github.com/php-telegram-bot/core/compare/master...develop
[0.57.0]: https://github.com/php-telegram-bot/core/compare/0.56.0...0.57.0
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"psr/log": "^1.1",
"monolog/monolog": "^1.24",
"guzzlehttp/guzzle": "^6.3"
},
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 31 additions & 19 deletions doc/01-utils.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
## Logging
PHP Telegram Bot library features [Monolog](https://github.com/Seldaek/monolog) to store logs.
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][PSR-3-providers].

Logs are divided into the following streams:
### Error
Collects all the exceptions thrown by the library:
```php
TelegramLog::initErrorLog($path . '/' . $BOT_NAME . '_error.log');
```
- `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.

### Debug
Stores requests made to the Telegram API, useful for debugging:
(in this example we're using [Monolog])
```php
TelegramLog::initDebugLog($path . '/' . $BOT_NAME . '_debug.log');
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Longman\TelegramBot\TelegramLog;

TelegramLog::initialize(
// Main logger that handles all 'error' and 'debug' logs.
new Logger('telegram_bot', [
(new StreamHandler(__DIR__ . "/logs/{$bot_username}_debug.log", Logger::DEBUG))->setFormatter(new LineFormatter(null, null, true)),
(new StreamHandler(__DIR__ . "/logs/{$bot_username}_error.log", Logger::ERROR))->setFormatter(new LineFormatter(null, null, true)),
]),
// Updates logger for raw updates.
new Logger('telegram_bot_updates', [
(new StreamHandler(__DIR__ . "/logs/{$bot_username}_update.log", Logger::INFO))->setFormatter(new LineFormatter('%message%' . PHP_EOL)),
])
);
```

### Raw data
Incoming updates (JSON string from Webhook and getUpdates) get logged in a text file:
```php
TelegramLog::initUpdateLog($path . '/' . $BOT_NAME . '_update.log');
```
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](../utils/importFromLog.php).
Remember to always backup first!!

## Stream and external sources
Error and Debug streams rely on the `bot_log` instance that can be provided from an external source:
```php
TelegramLog::initialize($monolog);
```

Raw data relies on the `bot_update_log` instance that uses a custom format.
[PSR-3]: https://www.php-fig.org/psr/psr-3
[PSR-3-providers]: https://packagist.org/providers/psr/log-implementation
[Monolog]: https://github.com/Seldaek/monolog
4 changes: 2 additions & 2 deletions src/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ protected function sanitizeCommand($command)
public function enableAdmin($admin_id)
{
if (!is_int($admin_id) || $admin_id <= 0) {
TelegramLog::error('Invalid value "%s" for admin.', $admin_id);
TelegramLog::error('Invalid value "' . $admin_id . '" for admin.');
} elseif (!in_array($admin_id, $this->admins_list, true)) {
$this->admins_list[] = $admin_id;
}
Expand Down Expand Up @@ -632,7 +632,7 @@ public function isDbEnabled()
public function addCommandsPath($path, $before = true)
{
if (!is_dir($path)) {
TelegramLog::error('Commands path "%s" does not exist.', $path);
TelegramLog::error('Commands path "' . $path . '" does not exist.');
} elseif (!in_array($path, $this->commands_paths, true)) {
if ($before) {
array_unshift($this->commands_paths, $path);
Expand Down
Loading