Skip to content

Fix logger to properly output text that contains '%' symbols. #414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/TelegramLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,11 @@ protected static function getLogText($text, array $args = [])
// Pop the $text off the array, as it gets passed via func_get_args().
array_shift($args);

// Suppress warning if placeholders don't match out.
return @vsprintf($text, $args) ?: $text;
// If no placeholders have been passed, don't parse the text.
if (empty($args)) {
return $text;
}

return vsprintf($text, $args);
}
}
10 changes: 10 additions & 0 deletions tests/unit/TelegramLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ public function testErrorStream()
$this->assertFileNotExists($file);
TelegramLog::initErrorLog($file);
TelegramLog::error('my error');
TelegramLog::error('my 50% error');
TelegramLog::error('my %s error', 'placeholder');
$this->assertFileExists($file);
$error_log = file_get_contents($file);
$this->assertContains('bot_log.ERROR: my error', $error_log);
$this->assertContains('bot_log.ERROR: my 50% error', $error_log);
$this->assertContains('bot_log.ERROR: my placeholder error', $error_log);
}

Expand All @@ -96,10 +98,12 @@ public function testDebugStream()
$this->assertFileNotExists($file);
TelegramLog::initDebugLog($file);
TelegramLog::debug('my debug');
TelegramLog::debug('my 50% debug');
TelegramLog::debug('my %s debug', 'placeholder');
$this->assertFileExists($file);
$debug_log = file_get_contents($file);
$this->assertContains('bot_log.DEBUG: my debug', $debug_log);
$this->assertContains('bot_log.DEBUG: my 50% debug', $debug_log);
$this->assertContains('bot_log.DEBUG: my placeholder debug', $debug_log);
}

Expand All @@ -109,10 +113,12 @@ public function testUpdateStream()
$this->assertFileNotExists($file);
TelegramLog::initUpdateLog($file);
TelegramLog::update('my update');
TelegramLog::update('my 50% update');
TelegramLog::update('my %s update', 'placeholder');
$this->assertFileExists($file);
$debug_log = file_get_contents($file);
$this->assertContains('my update', $debug_log);
$this->assertContains('my 50% update', $debug_log);
$this->assertContains('my placeholder update', $debug_log);
}

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

TelegramLog::initialize($external_monolog);
TelegramLog::error('my error');
TelegramLog::error('my 50% error');
TelegramLog::error('my %s error', 'placeholder');
TelegramLog::debug('my debug');
TelegramLog::debug('my 50% debug');
TelegramLog::debug('my %s debug', 'placeholder');

$this->assertFileExists($file);
$file_contents = file_get_contents($file);
$this->assertContains('bot_update_log.ERROR: my error', $file_contents);
$this->assertContains('bot_update_log.ERROR: my 50% error', $file_contents);
$this->assertContains('bot_update_log.ERROR: my placeholder error', $file_contents);
$this->assertContains('bot_update_log.DEBUG: my debug', $file_contents);
$this->assertContains('bot_update_log.DEBUG: my 50% debug', $file_contents);
$this->assertContains('bot_update_log.DEBUG: my placeholder debug', $file_contents);
}
}