Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow logging request data in the debug log #1089

Merged
merged 6 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
- Bot API 4.8 (Extra Poll and Dice features).
- Allow custom MySQL port to be defined for tests.
- New static method `Entity::escapeMarkdownV2` for MarkdownV2.
- `TelegramLog::$always_log_request_and_response` parameter to force output of the request and response data to the debug log, also for successful requests
### Changed
- [:exclamation:][unreleased-bc-static-method-entityescapemarkdown] Made `Entity::escapeMarkdown` static, to not require an `Entity` object.
### Deprecated
Expand Down
5 changes: 5 additions & 0 deletions doc/01-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ Telegram API changes continuously and it often happens that the database schema
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!!

### Always log request and response data
If you's like to always log the request and response data to the debug log, also for successful requests, you can set the appropriate variable:
```php
\Longman\TelegramBot\TelegramLog::$always_log_request_and_response = true;
```

[PSR-3]: https://www.php-fig.org/psr/psr-3
[PSR-3-providers]: https://packagist.org/providers/psr/log-implementation
Expand Down
10 changes: 8 additions & 2 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ public static function getCurrentAction()
public static function execute($action, array $data = [])
{
$result = null;
$response = null;
$request_params = self::setUpRequestParams($data);
$request_params['debug'] = TelegramLog::getDebugLogTempStream();

Expand All @@ -497,10 +498,15 @@ public static function execute($action, array $data = [])
TelegramLog::update($result);
}
} catch (RequestException $e) {
$result = $e->getResponse() ? (string) $e->getResponse()->getBody() : '';
$response = null;
$result = $e->getResponse() ? (string) $e->getResponse()->getBody() : '';
} finally {
//Logging verbose debug output
TelegramLog::endDebugLogTempStream('Verbose HTTP Request output:' . PHP_EOL . '%s' . PHP_EOL);
if (TelegramLog::$always_log_request_and_response || $response === null) {
TelegramLog::debug('Request data:' . PHP_EOL . print_r($data, true));
TelegramLog::debug('Response data:' . PHP_EOL . $result);
TelegramLog::endDebugLogTempStream('Verbose HTTP Request output:' . PHP_EOL . '%s' . PHP_EOL);
}
}

return $result;
Expand Down
7 changes: 7 additions & 0 deletions src/TelegramLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class TelegramLog
*/
protected static $update_logger;

/**
* Always log the request and response data to the debug log, also for successful requests
*
* @var bool
*/
public static $always_log_request_and_response = false;

/**
* Temporary stream handle for debug log
*
Expand Down