Skip to content

Commit 1b5827b

Browse files
committed
Merge branch 'upstream/develop' into 1088-log_request_data
2 parents add4b88 + 2022f06 commit 1b5827b

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
1111
- Bot API 4.8 (Extra Poll and Dice features).
1212
- Allow custom MySQL port to be defined for tests.
1313
- New static method `Entity::escapeMarkdownV2` for MarkdownV2.
14+
- Remove bot token from debug http logs, this can be disabled by setting `TelegramLog::$remove_bot_token` parameter to `false`
1415
- `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.

doc/01-utils.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ If you's like to always log the request and response data to the debug log, also
4646
\Longman\TelegramBot\TelegramLog::$always_log_request_and_response = true;
4747
```
4848

49+
### Hiding API token from the log
50+
By default, the API token is removed from the log, to prevent any mistaken leakage when posting logs online.
51+
This behaviour can be changed by setting the appropriate variable:
52+
``php
53+
\Longman\TelegramBot\TelegramLog::$remove_bot_token = false;
54+
```
55+
4956
[PSR-3]: https://www.php-fig.org/psr/psr-3
5057
[PSR-3-providers]: https://packagist.org/providers/psr/log-implementation
5158
[Monolog]: https://github.com/Seldaek/monolog

src/TelegramLog.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ class TelegramLog
5757
*/
5858
protected static $debug_log_temp_stream_handle;
5959

60+
/**
61+
* Remove bot token from debug stream
62+
*
63+
* @var bool
64+
*/
65+
public static $remove_bot_token = true;
66+
6067
/**
6168
* Initialise logging.
6269
*
@@ -92,7 +99,13 @@ public static function endDebugLogTempStream($message = '%s')
9299
{
93100
if (is_resource(self::$debug_log_temp_stream_handle)) {
94101
rewind(self::$debug_log_temp_stream_handle);
95-
self::debug(sprintf($message, stream_get_contents(self::$debug_log_temp_stream_handle)));
102+
$stream_contents = stream_get_contents(self::$debug_log_temp_stream_handle);
103+
104+
if (self::$remove_bot_token) {
105+
$stream_contents = preg_replace('/\/bot(\d+)\:[\w\-]+\//', '/botBOT_TOKEN_REMOVED/', $stream_contents);
106+
}
107+
108+
self::debug(sprintf($message, $stream_contents));
96109
fclose(self::$debug_log_temp_stream_handle);
97110
self::$debug_log_temp_stream_handle = null;
98111
}

0 commit comments

Comments
 (0)