Skip to content

Commit

Permalink
Merge pull request #1089 from noplanman/1088-log_request_data
Browse files Browse the repository at this point in the history
Allow logging request data in the debug log
  • Loading branch information
noplanman committed May 4, 2020
2 parents 2022f06 + 1b5827b commit 617e524
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
- Allow custom MySQL port to be defined for tests.
- New static method `Entity::escapeMarkdownV2` for MarkdownV2.
- Remove bot token from debug http logs, this can be disabled by setting `TelegramLog::$remove_bot_token` parameter to `false`
- `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
6 changes: 6 additions & 0 deletions doc/01-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ 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;
```

### Hiding API token from the log
By default, the API token is removed from the log, to prevent any mistaken leakage when posting logs online.
This behaviour can be changed by setting the appropriate variable:
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

0 comments on commit 617e524

Please sign in to comment.