Skip to content

Commit 26ae543

Browse files
committed
Fix logger to properly output text that contains '%' symbols.
Remove error suppression. Fixes #413
1 parent bc45e04 commit 26ae543

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/TelegramLog.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,11 @@ protected static function getLogText($text, array $args = [])
288288
// Pop the $text off the array, as it gets passed via func_get_args().
289289
array_shift($args);
290290

291-
// Suppress warning if placeholders don't match out.
292-
return @vsprintf($text, $args) ?: $text;
291+
// If no placeholders have been passed, don't parse the text.
292+
if (empty($args)) {
293+
return $text;
294+
}
295+
296+
return vsprintf($text, $args);
293297
}
294298
}

tests/unit/TelegramLogTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ public function testErrorStream()
8383
$this->assertFileNotExists($file);
8484
TelegramLog::initErrorLog($file);
8585
TelegramLog::error('my error');
86+
TelegramLog::error('my 50% error');
8687
TelegramLog::error('my %s error', 'placeholder');
8788
$this->assertFileExists($file);
8889
$error_log = file_get_contents($file);
8990
$this->assertContains('bot_log.ERROR: my error', $error_log);
91+
$this->assertContains('bot_log.ERROR: my 50% error', $error_log);
9092
$this->assertContains('bot_log.ERROR: my placeholder error', $error_log);
9193
}
9294

@@ -96,10 +98,12 @@ public function testDebugStream()
9698
$this->assertFileNotExists($file);
9799
TelegramLog::initDebugLog($file);
98100
TelegramLog::debug('my debug');
101+
TelegramLog::debug('my 50% debug');
99102
TelegramLog::debug('my %s debug', 'placeholder');
100103
$this->assertFileExists($file);
101104
$debug_log = file_get_contents($file);
102105
$this->assertContains('bot_log.DEBUG: my debug', $debug_log);
106+
$this->assertContains('bot_log.DEBUG: my 50% debug', $debug_log);
103107
$this->assertContains('bot_log.DEBUG: my placeholder debug', $debug_log);
104108
}
105109

@@ -109,10 +113,12 @@ public function testUpdateStream()
109113
$this->assertFileNotExists($file);
110114
TelegramLog::initUpdateLog($file);
111115
TelegramLog::update('my update');
116+
TelegramLog::update('my 50% update');
112117
TelegramLog::update('my %s update', 'placeholder');
113118
$this->assertFileExists($file);
114119
$debug_log = file_get_contents($file);
115120
$this->assertContains('my update', $debug_log);
121+
$this->assertContains('my 50% update', $debug_log);
116122
$this->assertContains('my placeholder update', $debug_log);
117123
}
118124

@@ -127,15 +133,19 @@ public function testExternalStream()
127133

128134
TelegramLog::initialize($external_monolog);
129135
TelegramLog::error('my error');
136+
TelegramLog::error('my 50% error');
130137
TelegramLog::error('my %s error', 'placeholder');
131138
TelegramLog::debug('my debug');
139+
TelegramLog::debug('my 50% debug');
132140
TelegramLog::debug('my %s debug', 'placeholder');
133141

134142
$this->assertFileExists($file);
135143
$file_contents = file_get_contents($file);
136144
$this->assertContains('bot_update_log.ERROR: my error', $file_contents);
145+
$this->assertContains('bot_update_log.ERROR: my 50% error', $file_contents);
137146
$this->assertContains('bot_update_log.ERROR: my placeholder error', $file_contents);
138147
$this->assertContains('bot_update_log.DEBUG: my debug', $file_contents);
148+
$this->assertContains('bot_update_log.DEBUG: my 50% debug', $file_contents);
139149
$this->assertContains('bot_update_log.DEBUG: my placeholder debug', $file_contents);
140150
}
141151
}

0 commit comments

Comments
 (0)