Skip to content

Commit bb13824

Browse files
authored
Merge pull request #28 from noplanman/psr3_logging
Use own Monolog logging
2 parents 0749d43 + 63c7488 commit bb13824

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
77
### Added
88
### Changed
99
- Bumped Manager to 1.5.
10+
- Logging is now decoupled with custom Monolog logger.
1011
### Deprecated
1112
### Removed
1213
### Fixed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"vlucas/phpdotenv": "^3.4",
2525
"php-http/guzzle6-adapter": "^1.1",
2626
"knplabs/github-api": "^2.11",
27-
"elvanto/litemoji": "^1.4"
27+
"elvanto/litemoji": "^1.4",
28+
"monolog/monolog": "^1.24"
2829
},
2930
"autoload": {
3031
"psr-4": {

composer.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/manager.php

+35-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
use Dotenv\Dotenv;
1414
use Longman\TelegramBot\Exception\TelegramLogException;
1515
use Longman\TelegramBot\TelegramLog;
16+
use Monolog\Formatter\LineFormatter;
17+
use Monolog\Handler\StreamHandler;
18+
use Monolog\Logger;
19+
use Psr\Log\NullLogger;
1620
use TelegramBot\TelegramBotManager\BotManager;
1721

1822
// Composer autoloader.
@@ -41,17 +45,47 @@
4145
}
4246

4347
// Optional extras.
44-
$extras = ['admins', 'commands', 'cron', 'limiter', 'logging', 'paths', 'valid_ips', 'webhook'];
48+
$extras = ['admins', 'commands', 'cron', 'limiter', 'paths', 'valid_ips', 'webhook'];
4549
foreach ($extras as $extra) {
4650
if ($param = getenv('TG_' . strtoupper($extra))) {
4751
$params[$extra] = json_decode($param, true);
4852
}
4953
}
5054

55+
initLogging();
56+
5157
$bot = new BotManager($params);
5258
$bot->run();
5359
} catch (TelegramLogException $e) {
5460
// Silence... beautiful silence =)
5561
} catch (\Throwable $e) {
5662
TelegramLog::error($e->getMessage());
5763
}
64+
65+
/**
66+
* Initialise the logging.
67+
*
68+
*/
69+
function initLogging()
70+
{
71+
// Logging.
72+
$logging_paths = json_decode(getenv('TG_LOGGING'), true) ?? [];
73+
74+
$debug_log = $logging_paths['debug'] ?? null;
75+
$error_log = $logging_paths['error'] ?? null;
76+
$update_log = $logging_paths['update'] ?? null;
77+
78+
// Main logger that handles all 'debug' and 'error' logs.
79+
$logger = ($debug_log || $error_log) ? new Logger('telegram_bot') : new NullLogger();
80+
$debug_log && $logger->pushHandler((new StreamHandler($debug_log, Logger::DEBUG))->setFormatter(new LineFormatter(null, null, true)));
81+
$error_log && $logger->pushHandler((new StreamHandler($error_log, Logger::ERROR))->setFormatter(new LineFormatter(null, null, true)));
82+
83+
// Updates logger for raw updates.
84+
$update_logger = new NullLogger();
85+
if ($update_log) {
86+
$update_logger = new Logger('telegram_bot_updates');
87+
$update_logger->pushHandler((new StreamHandler($update_log, Logger::INFO))->setFormatter(new LineFormatter('%message%' . PHP_EOL)));
88+
}
89+
90+
TelegramLog::initialize($logger, $update_logger);
91+
}

0 commit comments

Comments
 (0)