diff --git a/CHANGELOG.md b/CHANGELOG.md index e87dc00e6..cde273fce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/doc/01-utils.md b/doc/01-utils.md index 96001129b..145338272 100644 --- a/doc/01-utils.md +++ b/doc/01-utils.md @@ -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: diff --git a/src/Request.php b/src/Request.php index 60477a3bc..42e3dd0f1 100644 --- a/src/Request.php +++ b/src/Request.php @@ -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(); @@ -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; diff --git a/src/TelegramLog.php b/src/TelegramLog.php index 4dd9f2392..8082aa122 100644 --- a/src/TelegramLog.php +++ b/src/TelegramLog.php @@ -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 *