Skip to content

Commit 617e524

Browse files
authored
Merge pull request #1089 from noplanman/1088-log_request_data
Allow logging request data in the debug log
2 parents 2022f06 + 1b5827b commit 617e524

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
1212
- Allow custom MySQL port to be defined for tests.
1313
- New static method `Entity::escapeMarkdownV2` for MarkdownV2.
1414
- Remove bot token from debug http logs, this can be disabled by setting `TelegramLog::$remove_bot_token` parameter to `false`
15+
- `TelegramLog::$always_log_request_and_response` parameter to force output of the request and response data to the debug log, also for successful requests
1516
### Changed
1617
- [:exclamation:][unreleased-bc-static-method-entityescapemarkdown] Made `Entity::escapeMarkdown` static, to not require an `Entity` object.
1718
### Deprecated

doc/01-utils.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ Telegram API changes continuously and it often happens that the database schema
4040
If you store the raw data you can import all updates on the newest table schema by simply using [this script](../utils/importFromLog.php).
4141
Remember to always backup first!!
4242

43+
### Always log request and response data
44+
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:
45+
```php
46+
\Longman\TelegramBot\TelegramLog::$always_log_request_and_response = true;
47+
```
48+
4349
### Hiding API token from the log
4450
By default, the API token is removed from the log, to prevent any mistaken leakage when posting logs online.
4551
This behaviour can be changed by setting the appropriate variable:

src/Request.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ public static function getCurrentAction()
482482
public static function execute($action, array $data = [])
483483
{
484484
$result = null;
485+
$response = null;
485486
$request_params = self::setUpRequestParams($data);
486487
$request_params['debug'] = TelegramLog::getDebugLogTempStream();
487488

@@ -497,10 +498,15 @@ public static function execute($action, array $data = [])
497498
TelegramLog::update($result);
498499
}
499500
} catch (RequestException $e) {
500-
$result = $e->getResponse() ? (string) $e->getResponse()->getBody() : '';
501+
$response = null;
502+
$result = $e->getResponse() ? (string) $e->getResponse()->getBody() : '';
501503
} finally {
502504
//Logging verbose debug output
503-
TelegramLog::endDebugLogTempStream('Verbose HTTP Request output:' . PHP_EOL . '%s' . PHP_EOL);
505+
if (TelegramLog::$always_log_request_and_response || $response === null) {
506+
TelegramLog::debug('Request data:' . PHP_EOL . print_r($data, true));
507+
TelegramLog::debug('Response data:' . PHP_EOL . $result);
508+
TelegramLog::endDebugLogTempStream('Verbose HTTP Request output:' . PHP_EOL . '%s' . PHP_EOL);
509+
}
504510
}
505511

506512
return $result;

src/TelegramLog.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ class TelegramLog
4343
*/
4444
protected static $update_logger;
4545

46+
/**
47+
* Always log the request and response data to the debug log, also for successful requests
48+
*
49+
* @var bool
50+
*/
51+
public static $always_log_request_and_response = false;
52+
4653
/**
4754
* Temporary stream handle for debug log
4855
*

0 commit comments

Comments
 (0)