Skip to content

Commit

Permalink
Merge pull request #414 from noplanman/413-fix_logger
Browse files Browse the repository at this point in the history
Fix logger to properly output text that contains '%' symbols.
  • Loading branch information
noplanman authored Feb 20, 2017
2 parents bc45e04 + 26ae543 commit 2c9f65e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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);
}
}

0 comments on commit 2c9f65e

Please sign in to comment.