From 5002857e6ab401d0100dfc33486f558e8669ca68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Tue, 16 Jul 2019 17:59:25 +0200 Subject: [PATCH 1/2] Don't output deprecation error if no logging is enabled, allow more logging flexibility. --- CHANGELOG.md | 2 ++ src/TelegramLog.php | 11 ++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e1ddb39f..354152b51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,9 +8,11 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c - [:ledger: View file changes][Unreleased] ### Added ### Changed +- Logging only updates or only debug/errors is now possible. ### Deprecated ### Removed ### Fixed +- Don't output deprecation notices if no logging is enabled. ### Security ## [0.59.0] - 2019-07-07 diff --git a/src/TelegramLog.php b/src/TelegramLog.php index 77f7e6bab..643f37ebd 100644 --- a/src/TelegramLog.php +++ b/src/TelegramLog.php @@ -88,8 +88,8 @@ class TelegramLog */ public static function initialize(LoggerInterface $logger = null, LoggerInterface $update_logger = null) { - // Clearly deprecated code still being executed. - if ($logger === null) { + if ($logger === null && $update_logger === null) { + // Clearly deprecated code still being executed. (defined('PHPUNIT_TESTSUITE') && PHPUNIT_TESTSUITE) || trigger_error('A PSR-3 compatible LoggerInterface object must be provided. Initialise with a preconfigured logger instance.', E_USER_DEPRECATED); $logger = new Logger('bot_log'); } elseif ($logger instanceof Logger) { @@ -286,11 +286,12 @@ public static function __callStatic($name, array $arguments) } elseif ($name === 'update') { $logger = self::$update_logger; $name = 'info'; - } else { - return; } - self::initialize(self::$logger, self::$update_logger); + // Clearly we have no logging enabled. + if ($logger === null) { + return; + } // Replace any placeholders from the passed context. if (count($arguments) >= 2) { From ae1d1b9d1278d9baf87d9aae554358985ce85816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Thu, 18 Jul 2019 01:44:06 +0200 Subject: [PATCH 2/2] Add new tests for external updates logger. --- tests/unit/TelegramLogTest.php | 58 ++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/tests/unit/TelegramLogTest.php b/tests/unit/TelegramLogTest.php index e29c5a04d..0a1d60486 100644 --- a/tests/unit/TelegramLogTest.php +++ b/tests/unit/TelegramLogTest.php @@ -10,6 +10,7 @@ namespace Longman\TelegramBot\Tests\Unit; +use Longman\TelegramBot\Exception\TelegramLogException; use Longman\TelegramBot\TelegramLog; use Monolog\Handler\StreamHandler; use Monolog\Logger; @@ -27,17 +28,18 @@ class TelegramLogTest extends TestCase * @var array Dummy logfile paths */ private static $logfiles = [ - 'error' => '/tmp/php-telegram-bot-errorlog.log', - 'debug' => '/tmp/php-telegram-bot-debuglog.log', - 'update' => '/tmp/php-telegram-bot-updatelog.log', - 'external' => '/tmp/php-telegram-bot-externallog.log', + 'error' => '/tmp/php-telegram-bot-error.log', + 'debug' => '/tmp/php-telegram-bot-debug.log', + 'update' => '/tmp/php-telegram-bot-update.log', + 'external' => '/tmp/php-telegram-bot-external.log', + 'external_update' => '/tmp/php-telegram-bot-external_update.log', ]; protected function setUp() { // Make sure no logger instance is set before each test. - TestHelpers::setStaticProperty('Longman\TelegramBot\TelegramLog', 'logger', null); - TestHelpers::setStaticProperty('Longman\TelegramBot\TelegramLog', 'update_logger', null); + TestHelpers::setStaticProperty(TelegramLog::class, 'logger', null); + TestHelpers::setStaticProperty(TelegramLog::class, 'update_logger', null); } protected function tearDown() @@ -48,39 +50,35 @@ protected function tearDown() } } - /** - * @expectedException \Longman\TelegramBot\Exception\TelegramLogException - */ public function testNewInstanceWithoutErrorPath() { + $this->setExpectedException(TelegramLogException::class); TelegramLog::initErrorLog(''); } - /** - * @expectedException \Longman\TelegramBot\Exception\TelegramLogException - */ public function testNewInstanceWithoutDebugPath() { + $this->setExpectedException(TelegramLogException::class); TelegramLog::initDebugLog(''); } - /** - * @expectedException \Longman\TelegramBot\Exception\TelegramLogException - */ public function testNewInstanceWithoutUpdatePath() { + $this->setExpectedException(TelegramLogException::class); TelegramLog::initUpdateLog(''); } public function testErrorStream() { $file = self::$logfiles['error']; + $this->assertFileNotExists($file); TelegramLog::initErrorLog($file); TelegramLog::error('my error'); TelegramLog::error('my 50% error'); TelegramLog::error('my old %s %s error', 'custom', 'placeholder'); TelegramLog::error('my new {place} {holder} error', ['place' => 'custom', 'holder' => 'placeholder']); + $this->assertFileExists($file); $error_log = file_get_contents($file); $this->assertContains('bot_log.ERROR: my error', $error_log); @@ -92,12 +90,14 @@ public function testErrorStream() public function testDebugStream() { $file = self::$logfiles['debug']; + $this->assertFileNotExists($file); TelegramLog::initDebugLog($file); TelegramLog::debug('my debug'); TelegramLog::debug('my 50% debug'); TelegramLog::debug('my old %s %s debug', 'custom', 'placeholder'); TelegramLog::debug('my new {place} {holder} debug', ['place' => 'custom', 'holder' => 'placeholder']); + $this->assertFileExists($file); $debug_log = file_get_contents($file); $this->assertContains('bot_log.DEBUG: my debug', $debug_log); @@ -109,12 +109,14 @@ public function testDebugStream() public function testUpdateStream() { $file = self::$logfiles['update']; + $this->assertFileNotExists($file); TelegramLog::initUpdateLog($file); TelegramLog::update('my update'); TelegramLog::update('my 50% update'); TelegramLog::update('my old %s %s update', 'custom', 'placeholder'); TelegramLog::update('my new {place} {holder} update', ['place' => 'custom', 'holder' => 'placeholder']); + $this->assertFileExists($file); $update_log = file_get_contents($file); $this->assertContains('my update', $update_log); @@ -125,14 +127,18 @@ public function testUpdateStream() public function testExternalStream() { - $file = self::$logfiles['external']; + $file = self::$logfiles['external']; + $file_update = self::$logfiles['external_update']; $this->assertFileNotExists($file); + $this->assertFileNotExists($file_update); - $external_monolog = new Logger('bot_external_log'); - $external_monolog->pushHandler(new StreamHandler($file, Logger::ERROR)); - $external_monolog->pushHandler(new StreamHandler($file, Logger::DEBUG)); + $external_logger = new Logger('bot_external_log'); + $external_logger->pushHandler(new StreamHandler($file, Logger::ERROR)); + $external_logger->pushHandler(new StreamHandler($file, Logger::DEBUG)); + $external_update_logger = new Logger('bot_external_update_log'); + $external_update_logger->pushHandler(new StreamHandler($file_update, Logger::INFO)); - TelegramLog::initialize($external_monolog); + TelegramLog::initialize($external_logger, $external_update_logger); TelegramLog::error('my error'); TelegramLog::error('my 50% error'); TelegramLog::error('my old %s %s error', 'custom', 'placeholder'); @@ -141,9 +147,15 @@ public function testExternalStream() TelegramLog::debug('my 50% debug'); TelegramLog::debug('my old %s %s debug', 'custom', 'placeholder'); TelegramLog::debug('my new {place} {holder} debug', ['place' => 'custom', 'holder' => 'placeholder']); + TelegramLog::update('my update'); + TelegramLog::update('my 50% update'); + TelegramLog::update('my old %s %s update', 'custom', 'placeholder'); + TelegramLog::update('my new {place} {holder} update', ['place' => 'custom', 'holder' => 'placeholder']); $this->assertFileExists($file); - $file_contents = file_get_contents($file); + $this->assertFileExists($file_update); + $file_contents = file_get_contents($file); + $file_update_contents = file_get_contents($file_update); $this->assertContains('bot_external_log.ERROR: my error', $file_contents); $this->assertContains('bot_external_log.ERROR: my 50% error', $file_contents); $this->assertContains('bot_external_log.ERROR: my old custom placeholder error', $file_contents); @@ -152,5 +164,9 @@ public function testExternalStream() $this->assertContains('bot_external_log.DEBUG: my 50% debug', $file_contents); $this->assertContains('bot_external_log.DEBUG: my old custom placeholder debug', $file_contents); $this->assertContains('bot_external_log.DEBUG: my new custom placeholder debug', $file_contents); + $this->assertContains('bot_external_update_log.INFO: my update', $file_update_contents); + $this->assertContains('bot_external_update_log.INFO: my 50% update', $file_update_contents); + $this->assertContains('bot_external_update_log.INFO: my old custom placeholder update', $file_update_contents); + $this->assertContains('bot_external_update_log.INFO: my new custom placeholder update', $file_update_contents); } }