Skip to content

Commit f997e35

Browse files
authored
Merge pull request #20 from noplanman/pre_init_logging
Initialise loggers before anything else, to allow logging of all errors.
2 parents baec8e3 + d61bc60 commit f997e35

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
1010
### Deprecated
1111
### Removed
1212
### Fixed
13+
- Initialise loggers before anything else, to allow logging of all errors.
1314
### Security
1415

1516
## [0.43.0] - 2017-04-17

src/BotManager.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class BotManager
5656
*/
5757
public function __construct(array $params)
5858
{
59+
// Initialise logging before anything else, to allow errors to be logged.
60+
$this->initLogging($params['logging'] ?? []);
61+
5962
$this->params = new Params($params);
6063
$this->action = new Action($this->params->getScriptParam('a'));
6164

@@ -117,9 +120,6 @@ public function getAction(): Action
117120
*/
118121
public function run(): self
119122
{
120-
// Initialise logging.
121-
$this->initLogging();
122-
123123
// Make sure this is a valid call.
124124
$this->validateSecret();
125125

@@ -143,19 +143,17 @@ public function run(): self
143143
/**
144144
* Initialise all loggers.
145145
*
146+
* @param array $log_paths
147+
*
146148
* @return \NPM\TelegramBotManager\BotManager
147149
* @throws \Exception
148150
*/
149-
public function initLogging(): self
151+
public function initLogging(array $log_paths): self
150152
{
151-
$logging = $this->params->getBotParam('logging');
152-
if (is_array($logging)) {
153-
/** @var array $logging */
154-
foreach ($logging as $logger => $logfile) {
155-
('debug' === $logger) && TelegramLog::initDebugLog($logfile);
156-
('error' === $logger) && TelegramLog::initErrorLog($logfile);
157-
('update' === $logger) && TelegramLog::initUpdateLog($logfile);
158-
}
153+
foreach ($log_paths as $logger => $logfile) {
154+
('debug' === $logger) && TelegramLog::initDebugLog($logfile);
155+
('error' === $logger) && TelegramLog::initErrorLog($logfile);
156+
('update' === $logger) && TelegramLog::initUpdateLog($logfile);
159157
}
160158

161159
return $this;

tests/TelegramBotManager/Tests/BotManagerTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,18 @@ public function testValidTelegramObject()
108108

109109
public function testInitLogging()
110110
{
111-
$botManager = new BotManager(array_merge(ParamsTest::$demo_vital_params, [
111+
self::assertFalse(TelegramLog::isDebugLogActive());
112+
self::assertFalse(TelegramLog::isErrorLogActive());
113+
self::assertFalse(TelegramLog::isUpdateLogActive());
114+
115+
new BotManager(array_merge(ParamsTest::$demo_vital_params, [
112116
'logging' => [
113117
'debug' => '/tmp/php-telegram-bot-debuglog.log',
114118
'error' => '/tmp/php-telegram-bot-errorlog.log',
115119
'update' => '/tmp/php-telegram-bot-updatelog.log',
116120
],
117121
]));
118122

119-
self::assertFalse(TelegramLog::isDebugLogActive());
120-
self::assertFalse(TelegramLog::isErrorLogActive());
121-
self::assertFalse(TelegramLog::isUpdateLogActive());
122-
123-
$botManager->initLogging();
124-
125123
self::assertTrue(TelegramLog::isDebugLogActive());
126124
self::assertTrue(TelegramLog::isErrorLogActive());
127125
self::assertTrue(TelegramLog::isUpdateLogActive());

0 commit comments

Comments
 (0)