Commit 2c9f65e2 authored by Armando Lüscher's avatar Armando Lüscher Committed by GitHub

Merge pull request #414 from noplanman/413-fix_logger

Fix logger to properly output text that contains '%' symbols.
parents bc45e044 26ae543f
...@@ -288,7 +288,11 @@ class TelegramLog ...@@ -288,7 +288,11 @@ class TelegramLog
// Pop the $text off the array, as it gets passed via func_get_args(). // Pop the $text off the array, as it gets passed via func_get_args().
array_shift($args); array_shift($args);
// Suppress warning if placeholders don't match out. // If no placeholders have been passed, don't parse the text.
return @vsprintf($text, $args) ?: $text; if (empty($args)) {
return $text;
}
return vsprintf($text, $args);
} }
} }
...@@ -83,10 +83,12 @@ class TelegramLogTest extends TestCase ...@@ -83,10 +83,12 @@ class TelegramLogTest extends TestCase
$this->assertFileNotExists($file); $this->assertFileNotExists($file);
TelegramLog::initErrorLog($file); TelegramLog::initErrorLog($file);
TelegramLog::error('my error'); TelegramLog::error('my error');
TelegramLog::error('my 50% error');
TelegramLog::error('my %s error', 'placeholder'); TelegramLog::error('my %s error', 'placeholder');
$this->assertFileExists($file); $this->assertFileExists($file);
$error_log = file_get_contents($file); $error_log = file_get_contents($file);
$this->assertContains('bot_log.ERROR: my error', $error_log); $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); $this->assertContains('bot_log.ERROR: my placeholder error', $error_log);
} }
...@@ -96,10 +98,12 @@ class TelegramLogTest extends TestCase ...@@ -96,10 +98,12 @@ class TelegramLogTest extends TestCase
$this->assertFileNotExists($file); $this->assertFileNotExists($file);
TelegramLog::initDebugLog($file); TelegramLog::initDebugLog($file);
TelegramLog::debug('my debug'); TelegramLog::debug('my debug');
TelegramLog::debug('my 50% debug');
TelegramLog::debug('my %s debug', 'placeholder'); TelegramLog::debug('my %s debug', 'placeholder');
$this->assertFileExists($file); $this->assertFileExists($file);
$debug_log = file_get_contents($file); $debug_log = file_get_contents($file);
$this->assertContains('bot_log.DEBUG: my debug', $debug_log); $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); $this->assertContains('bot_log.DEBUG: my placeholder debug', $debug_log);
} }
...@@ -109,10 +113,12 @@ class TelegramLogTest extends TestCase ...@@ -109,10 +113,12 @@ class TelegramLogTest extends TestCase
$this->assertFileNotExists($file); $this->assertFileNotExists($file);
TelegramLog::initUpdateLog($file); TelegramLog::initUpdateLog($file);
TelegramLog::update('my update'); TelegramLog::update('my update');
TelegramLog::update('my 50% update');
TelegramLog::update('my %s update', 'placeholder'); TelegramLog::update('my %s update', 'placeholder');
$this->assertFileExists($file); $this->assertFileExists($file);
$debug_log = file_get_contents($file); $debug_log = file_get_contents($file);
$this->assertContains('my update', $debug_log); $this->assertContains('my update', $debug_log);
$this->assertContains('my 50% update', $debug_log);
$this->assertContains('my placeholder update', $debug_log); $this->assertContains('my placeholder update', $debug_log);
} }
...@@ -127,15 +133,19 @@ class TelegramLogTest extends TestCase ...@@ -127,15 +133,19 @@ class TelegramLogTest extends TestCase
TelegramLog::initialize($external_monolog); TelegramLog::initialize($external_monolog);
TelegramLog::error('my error'); TelegramLog::error('my error');
TelegramLog::error('my 50% error');
TelegramLog::error('my %s error', 'placeholder'); TelegramLog::error('my %s error', 'placeholder');
TelegramLog::debug('my debug'); TelegramLog::debug('my debug');
TelegramLog::debug('my 50% debug');
TelegramLog::debug('my %s debug', 'placeholder'); TelegramLog::debug('my %s debug', 'placeholder');
$this->assertFileExists($file); $this->assertFileExists($file);
$file_contents = file_get_contents($file); $file_contents = file_get_contents($file);
$this->assertContains('bot_update_log.ERROR: my error', $file_contents); $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.ERROR: my placeholder error', $file_contents);
$this->assertContains('bot_update_log.DEBUG: my debug', $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); $this->assertContains('bot_update_log.DEBUG: my placeholder debug', $file_contents);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment