Make logger more flexible and allow passing of parameters in a sprintf style.

parent b12b2e99
...@@ -40,7 +40,7 @@ class ShortenerCommand extends UserCommand ...@@ -40,7 +40,7 @@ class ShortenerCommand extends UserCommand
$data = []; $data = [];
$data['chat_id'] = $chat_id; $data['chat_id'] = $chat_id;
$text = Botan::shortenUrl("https://github.com/akalongman/php-telegram-bot", $user_id); $text = Botan::shortenUrl('https://github.com/akalongman/php-telegram-bot', $user_id);
$data['text'] = $text; $data['text'] = $text;
......
...@@ -191,7 +191,7 @@ class Botan ...@@ -191,7 +191,7 @@ class Botan
$responseData = json_decode($result, true); $responseData = json_decode($result, true);
if (!$responseData || $responseData['status'] !== 'accepted') { if (!$responseData || $responseData['status'] !== 'accepted') {
TelegramLog::debug('Botan.io stats report failed: ' . ($result ?: 'empty response')); TelegramLog::debug('Botan.io stats report failed: %s', $result ?: 'empty response');
return false; return false;
} }
...@@ -238,7 +238,7 @@ class Botan ...@@ -238,7 +238,7 @@ class Botan
} }
if (filter_var($result, FILTER_VALIDATE_URL) === false) { if (filter_var($result, FILTER_VALIDATE_URL) === false) {
TelegramLog::debug('Botan.io URL shortening failed for "' . $url . '": ' . ($result ?: 'empty response')); TelegramLog::debug('Botan.io URL shortening failed for "%s": %s', $url, $result ?: 'empty response');
return $url; return $url;
} }
......
...@@ -174,13 +174,13 @@ class Message extends Entity ...@@ -174,13 +174,13 @@ class Message extends Entity
return $command; return $command;
} }
$cmd = $this->getFullCommand(); $full_command = $this->getFullCommand();
if (strpos($cmd, '/') === 0) { if (strpos($full_command, '/') === 0) {
$cmd = substr($cmd, 1); $full_command = substr($full_command, 1);
//check if command is follow by botname //check if command is follow by botname
$split_cmd = explode('@', $cmd); $split_cmd = explode('@', $full_command);
if (isset($split_cmd[1])) { if (isset($split_cmd[1])) {
//command is followed by name check if is addressed to me //command is followed by name check if is addressed to me
if (strtolower($split_cmd[1]) === strtolower($this->getBotName())) { if (strtolower($split_cmd[1]) === strtolower($this->getBotName())) {
...@@ -188,7 +188,7 @@ class Message extends Entity ...@@ -188,7 +188,7 @@ class Message extends Entity
} }
} else { } else {
//command is not followed by name //command is not followed by name
return $cmd; return $full_command;
} }
} }
...@@ -208,10 +208,10 @@ class Message extends Entity ...@@ -208,10 +208,10 @@ class Message extends Entity
if ($without_cmd && $command = $this->getFullCommand()) { if ($without_cmd && $command = $this->getFullCommand()) {
if (strlen($command) + 1 < strlen($text)) { if (strlen($command) + 1 < strlen($text)) {
$text = substr($text, strlen($command) + 1); return substr($text, strlen($command) + 1);
} else {
$text = '';
} }
return '';
} }
return $text; return $text;
......
...@@ -499,7 +499,7 @@ class Telegram ...@@ -499,7 +499,7 @@ class Telegram
if (is_int($admin_id) && $admin_id > 0 && !in_array($admin_id, $this->admins_list, true)) { if (is_int($admin_id) && $admin_id > 0 && !in_array($admin_id, $this->admins_list, true)) {
$this->admins_list[] = $admin_id; $this->admins_list[] = $admin_id;
} else { } else {
TelegramLog::error('Invalid value "' . $admin_id . '" for admin.'); TelegramLog::error('Invalid value "%s" for admin.', $admin_id);
} }
return $this; return $this;
...@@ -590,7 +590,7 @@ class Telegram ...@@ -590,7 +590,7 @@ class Telegram
public function addCommandsPath($path, $before = true) public function addCommandsPath($path, $before = true)
{ {
if (!is_dir($path)) { if (!is_dir($path)) {
TelegramLog::error('Commands path "' . $path . '" does not exist.'); TelegramLog::error('Commands path "%s" does not exist.', $path);
} elseif (!in_array($path, $this->commands_paths, true)) { } elseif (!in_array($path, $this->commands_paths, true)) {
if ($before) { if ($before) {
array_unshift($this->commands_paths, $path); array_unshift($this->commands_paths, $path);
......
...@@ -165,12 +165,7 @@ class TelegramLog ...@@ -165,12 +165,7 @@ class TelegramLog
{ {
if (is_resource(self::$debug_log_temp_stream_handle)) { if (is_resource(self::$debug_log_temp_stream_handle)) {
rewind(self::$debug_log_temp_stream_handle); rewind(self::$debug_log_temp_stream_handle);
self::debug( self::debug($message, stream_get_contents(self::$debug_log_temp_stream_handle));
sprintf(
$message,
stream_get_contents(self::$debug_log_temp_stream_handle)
)
);
fclose(self::$debug_log_temp_stream_handle); fclose(self::$debug_log_temp_stream_handle);
self::$debug_log_temp_stream_handle = null; self::$debug_log_temp_stream_handle = null;
} }
...@@ -218,7 +213,7 @@ class TelegramLog ...@@ -218,7 +213,7 @@ class TelegramLog
*/ */
public static function isErrorLogActive() public static function isErrorLogActive()
{ {
return (self::$error_log_path !== null); return self::$error_log_path !== null;
} }
/** /**
...@@ -228,7 +223,7 @@ class TelegramLog ...@@ -228,7 +223,7 @@ class TelegramLog
*/ */
public static function isDebugLogActive() public static function isDebugLogActive()
{ {
return (self::$debug_log_path !== null); return self::$debug_log_path !== null;
} }
/** /**
...@@ -238,7 +233,7 @@ class TelegramLog ...@@ -238,7 +233,7 @@ class TelegramLog
*/ */
public static function isUpdateLogActive() public static function isUpdateLogActive()
{ {
return (self::$update_log_path !== null); return self::$update_log_path !== null;
} }
/** /**
...@@ -249,6 +244,7 @@ class TelegramLog ...@@ -249,6 +244,7 @@ class TelegramLog
public static function error($text) public static function error($text)
{ {
if (self::isErrorLogActive()) { if (self::isErrorLogActive()) {
$text = self::getLogText($text, func_get_args());
self::$monolog->error($text); self::$monolog->error($text);
} }
} }
...@@ -261,6 +257,7 @@ class TelegramLog ...@@ -261,6 +257,7 @@ class TelegramLog
public static function debug($text) public static function debug($text)
{ {
if (self::isDebugLogActive()) { if (self::isDebugLogActive()) {
$text = self::getLogText($text, func_get_args());
self::$monolog->debug($text); self::$monolog->debug($text);
} }
} }
...@@ -273,7 +270,25 @@ class TelegramLog ...@@ -273,7 +270,25 @@ class TelegramLog
public static function update($text) public static function update($text)
{ {
if (self::isUpdateLogActive()) { if (self::isUpdateLogActive()) {
$text = self::getLogText($text, func_get_args());
self::$monolog_update->info($text); self::$monolog_update->info($text);
} }
} }
/**
* Applies vsprintf to the text if placeholder replacements are passed along.
*
* @param string $text
* @param array $args
*
* @return string
*/
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;
}
} }
...@@ -26,7 +26,7 @@ class TelegramLogTest extends TestCase ...@@ -26,7 +26,7 @@ class TelegramLogTest extends TestCase
/** /**
* @var array Dummy logfile paths * @var array Dummy logfile paths
*/ */
private $logfiles = [ private static $logfiles = [
'error' => '/tmp/php-telegram-bot-errorlog.log', 'error' => '/tmp/php-telegram-bot-errorlog.log',
'debug' => '/tmp/php-telegram-bot-debuglog.log', 'debug' => '/tmp/php-telegram-bot-debuglog.log',
'update' => '/tmp/php-telegram-bot-updatelog.log', 'update' => '/tmp/php-telegram-bot-updatelog.log',
...@@ -48,7 +48,7 @@ class TelegramLogTest extends TestCase ...@@ -48,7 +48,7 @@ class TelegramLogTest extends TestCase
protected function tearDown() protected function tearDown()
{ {
// Make sure no logfiles exist. // Make sure no logfiles exist.
foreach ($this->logfiles as $file) { foreach (self::$logfiles as $file) {
file_exists($file) && unlink($file); file_exists($file) && unlink($file);
} }
} }
...@@ -79,37 +79,46 @@ class TelegramLogTest extends TestCase ...@@ -79,37 +79,46 @@ class TelegramLogTest extends TestCase
public function testErrorStream() public function testErrorStream()
{ {
$file = $this->logfiles['error']; $file = self::$logfiles['error'];
$this->assertFileNotExists($file); $this->assertFileNotExists($file);
TelegramLog::initErrorLog($file); TelegramLog::initErrorLog($file);
TelegramLog::error('my error'); TelegramLog::error('my error');
TelegramLog::error('my %s error', 'placeholder');
$this->assertFileExists($file); $this->assertFileExists($file);
$this->assertContains('bot_log.ERROR: my error', 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 placeholder error', $error_log);
} }
public function testDebugStream() public function testDebugStream()
{ {
$file = $this->logfiles['debug']; $file = self::$logfiles['debug'];
$this->assertFileNotExists($file); $this->assertFileNotExists($file);
TelegramLog::initDebugLog($file); TelegramLog::initDebugLog($file);
TelegramLog::debug('my debug'); TelegramLog::debug('my debug');
TelegramLog::debug('my %s debug', 'placeholder');
$this->assertFileExists($file); $this->assertFileExists($file);
$this->assertContains('bot_log.DEBUG: my debug', 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 placeholder debug', $debug_log);
} }
public function testUpdateStream() public function testUpdateStream()
{ {
$file = $this->logfiles['update']; $file = self::$logfiles['update'];
$this->assertFileNotExists($file); $this->assertFileNotExists($file);
TelegramLog::initUpdateLog($file); TelegramLog::initUpdateLog($file);
TelegramLog::update('my update'); TelegramLog::update('my update');
TelegramLog::update('my %s update', 'placeholder');
$this->assertFileExists($file); $this->assertFileExists($file);
$this->assertContains('my update', file_get_contents($file)); $debug_log = file_get_contents($file);
$this->assertContains('my update', $debug_log);
$this->assertContains('my placeholder update', $debug_log);
} }
public function testExternalStream() public function testExternalStream()
{ {
$file = $this->logfiles['external']; $file = self::$logfiles['external'];
$this->assertFileNotExists($file); $this->assertFileNotExists($file);
$external_monolog = new Logger('bot_update_log'); $external_monolog = new Logger('bot_update_log');
...@@ -118,11 +127,15 @@ class TelegramLogTest extends TestCase ...@@ -118,11 +127,15 @@ class TelegramLogTest extends TestCase
TelegramLog::initialize($external_monolog); TelegramLog::initialize($external_monolog);
TelegramLog::error('my error'); TelegramLog::error('my error');
TelegramLog::error('my %s error', 'placeholder');
TelegramLog::debug('my debug'); TelegramLog::debug('my debug');
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 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 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