Skip to content

Commit 89f22c2

Browse files
authored
Merge pull request #983 from noplanman/982-no_logging_error
Fix logging deprecation output
2 parents 80f626f + ae1d1b9 commit 89f22c2

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
88
- [:ledger: View file changes][Unreleased]
99
### Added
1010
### Changed
11+
- Logging only updates or only debug/errors is now possible.
1112
### Deprecated
1213
### Removed
1314
### Fixed
15+
- Don't output deprecation notices if no logging is enabled.
1416
### Security
1517

1618
## [0.59.0] - 2019-07-07

src/TelegramLog.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ class TelegramLog
8888
*/
8989
public static function initialize(LoggerInterface $logger = null, LoggerInterface $update_logger = null)
9090
{
91-
// Clearly deprecated code still being executed.
92-
if ($logger === null) {
91+
if ($logger === null && $update_logger === null) {
92+
// Clearly deprecated code still being executed.
9393
(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);
9494
$logger = new Logger('bot_log');
9595
} elseif ($logger instanceof Logger) {
@@ -286,11 +286,12 @@ public static function __callStatic($name, array $arguments)
286286
} elseif ($name === 'update') {
287287
$logger = self::$update_logger;
288288
$name = 'info';
289-
} else {
290-
return;
291289
}
292290

293-
self::initialize(self::$logger, self::$update_logger);
291+
// Clearly we have no logging enabled.
292+
if ($logger === null) {
293+
return;
294+
}
294295

295296
// Replace any placeholders from the passed context.
296297
if (count($arguments) >= 2) {

tests/unit/TelegramLogTest.php

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Longman\TelegramBot\Tests\Unit;
1212

13+
use Longman\TelegramBot\Exception\TelegramLogException;
1314
use Longman\TelegramBot\TelegramLog;
1415
use Monolog\Handler\StreamHandler;
1516
use Monolog\Logger;
@@ -27,17 +28,18 @@ class TelegramLogTest extends TestCase
2728
* @var array Dummy logfile paths
2829
*/
2930
private static $logfiles = [
30-
'error' => '/tmp/php-telegram-bot-errorlog.log',
31-
'debug' => '/tmp/php-telegram-bot-debuglog.log',
32-
'update' => '/tmp/php-telegram-bot-updatelog.log',
33-
'external' => '/tmp/php-telegram-bot-externallog.log',
31+
'error' => '/tmp/php-telegram-bot-error.log',
32+
'debug' => '/tmp/php-telegram-bot-debug.log',
33+
'update' => '/tmp/php-telegram-bot-update.log',
34+
'external' => '/tmp/php-telegram-bot-external.log',
35+
'external_update' => '/tmp/php-telegram-bot-external_update.log',
3436
];
3537

3638
protected function setUp()
3739
{
3840
// Make sure no logger instance is set before each test.
39-
TestHelpers::setStaticProperty('Longman\TelegramBot\TelegramLog', 'logger', null);
40-
TestHelpers::setStaticProperty('Longman\TelegramBot\TelegramLog', 'update_logger', null);
41+
TestHelpers::setStaticProperty(TelegramLog::class, 'logger', null);
42+
TestHelpers::setStaticProperty(TelegramLog::class, 'update_logger', null);
4143
}
4244

4345
protected function tearDown()
@@ -48,39 +50,35 @@ protected function tearDown()
4850
}
4951
}
5052

51-
/**
52-
* @expectedException \Longman\TelegramBot\Exception\TelegramLogException
53-
*/
5453
public function testNewInstanceWithoutErrorPath()
5554
{
55+
$this->setExpectedException(TelegramLogException::class);
5656
TelegramLog::initErrorLog('');
5757
}
5858

59-
/**
60-
* @expectedException \Longman\TelegramBot\Exception\TelegramLogException
61-
*/
6259
public function testNewInstanceWithoutDebugPath()
6360
{
61+
$this->setExpectedException(TelegramLogException::class);
6462
TelegramLog::initDebugLog('');
6563
}
6664

67-
/**
68-
* @expectedException \Longman\TelegramBot\Exception\TelegramLogException
69-
*/
7065
public function testNewInstanceWithoutUpdatePath()
7166
{
67+
$this->setExpectedException(TelegramLogException::class);
7268
TelegramLog::initUpdateLog('');
7369
}
7470

7571
public function testErrorStream()
7672
{
7773
$file = self::$logfiles['error'];
74+
7875
$this->assertFileNotExists($file);
7976
TelegramLog::initErrorLog($file);
8077
TelegramLog::error('my error');
8178
TelegramLog::error('my 50% error');
8279
TelegramLog::error('my old %s %s error', 'custom', 'placeholder');
8380
TelegramLog::error('my new {place} {holder} error', ['place' => 'custom', 'holder' => 'placeholder']);
81+
8482
$this->assertFileExists($file);
8583
$error_log = file_get_contents($file);
8684
$this->assertContains('bot_log.ERROR: my error', $error_log);
@@ -92,12 +90,14 @@ public function testErrorStream()
9290
public function testDebugStream()
9391
{
9492
$file = self::$logfiles['debug'];
93+
9594
$this->assertFileNotExists($file);
9695
TelegramLog::initDebugLog($file);
9796
TelegramLog::debug('my debug');
9897
TelegramLog::debug('my 50% debug');
9998
TelegramLog::debug('my old %s %s debug', 'custom', 'placeholder');
10099
TelegramLog::debug('my new {place} {holder} debug', ['place' => 'custom', 'holder' => 'placeholder']);
100+
101101
$this->assertFileExists($file);
102102
$debug_log = file_get_contents($file);
103103
$this->assertContains('bot_log.DEBUG: my debug', $debug_log);
@@ -109,12 +109,14 @@ public function testDebugStream()
109109
public function testUpdateStream()
110110
{
111111
$file = self::$logfiles['update'];
112+
112113
$this->assertFileNotExists($file);
113114
TelegramLog::initUpdateLog($file);
114115
TelegramLog::update('my update');
115116
TelegramLog::update('my 50% update');
116117
TelegramLog::update('my old %s %s update', 'custom', 'placeholder');
117118
TelegramLog::update('my new {place} {holder} update', ['place' => 'custom', 'holder' => 'placeholder']);
119+
118120
$this->assertFileExists($file);
119121
$update_log = file_get_contents($file);
120122
$this->assertContains('my update', $update_log);
@@ -125,14 +127,18 @@ public function testUpdateStream()
125127

126128
public function testExternalStream()
127129
{
128-
$file = self::$logfiles['external'];
130+
$file = self::$logfiles['external'];
131+
$file_update = self::$logfiles['external_update'];
129132
$this->assertFileNotExists($file);
133+
$this->assertFileNotExists($file_update);
130134

131-
$external_monolog = new Logger('bot_external_log');
132-
$external_monolog->pushHandler(new StreamHandler($file, Logger::ERROR));
133-
$external_monolog->pushHandler(new StreamHandler($file, Logger::DEBUG));
135+
$external_logger = new Logger('bot_external_log');
136+
$external_logger->pushHandler(new StreamHandler($file, Logger::ERROR));
137+
$external_logger->pushHandler(new StreamHandler($file, Logger::DEBUG));
138+
$external_update_logger = new Logger('bot_external_update_log');
139+
$external_update_logger->pushHandler(new StreamHandler($file_update, Logger::INFO));
134140

135-
TelegramLog::initialize($external_monolog);
141+
TelegramLog::initialize($external_logger, $external_update_logger);
136142
TelegramLog::error('my error');
137143
TelegramLog::error('my 50% error');
138144
TelegramLog::error('my old %s %s error', 'custom', 'placeholder');
@@ -141,9 +147,15 @@ public function testExternalStream()
141147
TelegramLog::debug('my 50% debug');
142148
TelegramLog::debug('my old %s %s debug', 'custom', 'placeholder');
143149
TelegramLog::debug('my new {place} {holder} debug', ['place' => 'custom', 'holder' => 'placeholder']);
150+
TelegramLog::update('my update');
151+
TelegramLog::update('my 50% update');
152+
TelegramLog::update('my old %s %s update', 'custom', 'placeholder');
153+
TelegramLog::update('my new {place} {holder} update', ['place' => 'custom', 'holder' => 'placeholder']);
144154

145155
$this->assertFileExists($file);
146-
$file_contents = file_get_contents($file);
156+
$this->assertFileExists($file_update);
157+
$file_contents = file_get_contents($file);
158+
$file_update_contents = file_get_contents($file_update);
147159
$this->assertContains('bot_external_log.ERROR: my error', $file_contents);
148160
$this->assertContains('bot_external_log.ERROR: my 50% error', $file_contents);
149161
$this->assertContains('bot_external_log.ERROR: my old custom placeholder error', $file_contents);
@@ -152,5 +164,9 @@ public function testExternalStream()
152164
$this->assertContains('bot_external_log.DEBUG: my 50% debug', $file_contents);
153165
$this->assertContains('bot_external_log.DEBUG: my old custom placeholder debug', $file_contents);
154166
$this->assertContains('bot_external_log.DEBUG: my new custom placeholder debug', $file_contents);
167+
$this->assertContains('bot_external_update_log.INFO: my update', $file_update_contents);
168+
$this->assertContains('bot_external_update_log.INFO: my 50% update', $file_update_contents);
169+
$this->assertContains('bot_external_update_log.INFO: my old custom placeholder update', $file_update_contents);
170+
$this->assertContains('bot_external_update_log.INFO: my new custom placeholder update', $file_update_contents);
155171
}
156172
}

0 commit comments

Comments
 (0)