From 08b5a820749be6f6f347a33b5f53036f2f834a82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Sat, 2 May 2020 23:22:12 +0200 Subject: [PATCH 1/4] Allow logging request data in the debug log --- CHANGELOG.md | 1 + src/Request.php | 4 ++++ src/TelegramLog.php | 7 +++++++ 3 files changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9660c8094..563a924d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c ### Added - Replaced 'generic' and 'genericmessage' strings with Telegram::GENERIC_COMMAND and Telegram::GENERIC_MESSAGE_COMMAND constants (@1int) - Bot API 4.8 (Extra Poll and Dice features). +- `TelegramLog::$log_request_data` parameter to output the request data to the debug log. ### Changed ### Deprecated ### Removed diff --git a/src/Request.php b/src/Request.php index 60477a3bc..197c63738 100644 --- a/src/Request.php +++ b/src/Request.php @@ -486,6 +486,10 @@ public static function execute($action, array $data = []) $request_params['debug'] = TelegramLog::getDebugLogTempStream(); try { + if (TelegramLog::$log_request_data) { + TelegramLog::debug('Request Data: ' . print_r($data, true)); + } + $response = self::$client->post( '/bot' . self::$telegram->getApiKey() . '/' . $action, $request_params diff --git a/src/TelegramLog.php b/src/TelegramLog.php index c565c0163..1f98d1750 100644 --- a/src/TelegramLog.php +++ b/src/TelegramLog.php @@ -43,6 +43,13 @@ class TelegramLog */ protected static $update_logger; + /** + * Log the request data to the debug log + * + * @var bool + */ + public static $log_request_data = false; + /** * Temporary stream handle for debug log * From a62e60e2e423a5c68188382b5bd88d906ab9abae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Sun, 3 May 2020 12:47:26 +0200 Subject: [PATCH 2/4] Write the request data just before writing the debug response. --- src/Request.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Request.php b/src/Request.php index 197c63738..1481bff23 100644 --- a/src/Request.php +++ b/src/Request.php @@ -486,10 +486,6 @@ public static function execute($action, array $data = []) $request_params['debug'] = TelegramLog::getDebugLogTempStream(); try { - if (TelegramLog::$log_request_data) { - TelegramLog::debug('Request Data: ' . print_r($data, true)); - } - $response = self::$client->post( '/bot' . self::$telegram->getApiKey() . '/' . $action, $request_params @@ -504,6 +500,9 @@ public static function execute($action, array $data = []) $result = $e->getResponse() ? (string) $e->getResponse()->getBody() : ''; } finally { //Logging verbose debug output + if (TelegramLog::$log_request_data) { + TelegramLog::debug('Request Data:' . PHP_EOL . print_r($data, true)); + } TelegramLog::endDebugLogTempStream('Verbose HTTP Request output:' . PHP_EOL . '%s' . PHP_EOL); } From c1a72a160572ed16c51c1ec2a58a08b200055274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Sun, 3 May 2020 13:18:49 +0200 Subject: [PATCH 3/4] Add documentation entry for new `TelegramLog::$log_request_data` variable --- doc/01-utils.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/01-utils.md b/doc/01-utils.md index 9051e211a..39eef6ef4 100644 --- a/doc/01-utils.md +++ b/doc/01-utils.md @@ -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!! +### Request data +If you'd like to also log the request data to your debug log, you can set the appropriate variable: +```php +\Longman\TelegramBot\TelegramLog::$log_request_data = true; +``` [PSR-3]: https://www.php-fig.org/psr/psr-3 [PSR-3-providers]: https://packagist.org/providers/psr/log-implementation From 449bd650ec33a6117d977e31baf9b39c1ca0398e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Mon, 4 May 2020 11:14:24 +0200 Subject: [PATCH 4/4] Always output request and response data to debug log on failed request and add optional switch to also output it for successful requests. --- CHANGELOG.md | 2 +- doc/01-utils.md | 6 +++--- src/Request.php | 11 +++++++---- src/TelegramLog.php | 4 ++-- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 563a924d1..00454c3e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c ### Added - Replaced 'generic' and 'genericmessage' strings with Telegram::GENERIC_COMMAND and Telegram::GENERIC_MESSAGE_COMMAND constants (@1int) - Bot API 4.8 (Extra Poll and Dice features). -- `TelegramLog::$log_request_data` parameter to output the request data to the debug log. +- `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 ### Deprecated ### Removed diff --git a/doc/01-utils.md b/doc/01-utils.md index 39eef6ef4..eac7170a3 100644 --- a/doc/01-utils.md +++ b/doc/01-utils.md @@ -40,10 +40,10 @@ 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!! -### Request data -If you'd like to also log the request data to your debug log, you can set the appropriate variable: +### 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::$log_request_data = true; +\Longman\TelegramBot\TelegramLog::$always_log_request_and_response = true; ``` [PSR-3]: https://www.php-fig.org/psr/psr-3 diff --git a/src/Request.php b/src/Request.php index 1481bff23..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,13 +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 - if (TelegramLog::$log_request_data) { - TelegramLog::debug('Request Data:' . PHP_EOL . print_r($data, true)); + 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); } - TelegramLog::endDebugLogTempStream('Verbose HTTP Request output:' . PHP_EOL . '%s' . PHP_EOL); } return $result; diff --git a/src/TelegramLog.php b/src/TelegramLog.php index 1f98d1750..650cd2bff 100644 --- a/src/TelegramLog.php +++ b/src/TelegramLog.php @@ -44,11 +44,11 @@ class TelegramLog protected static $update_logger; /** - * Log the request data to the debug log + * Always log the request and response data to the debug log, also for successful requests * * @var bool */ - public static $log_request_data = false; + public static $always_log_request_and_response = false; /** * Temporary stream handle for debug log