|
13 | 13 | use Dotenv\Dotenv;
|
14 | 14 | use Longman\TelegramBot\Exception\TelegramLogException;
|
15 | 15 | use Longman\TelegramBot\TelegramLog;
|
| 16 | +use Monolog\Formatter\LineFormatter; |
| 17 | +use Monolog\Handler\StreamHandler; |
| 18 | +use Monolog\Logger; |
| 19 | +use Psr\Log\NullLogger; |
16 | 20 | use TelegramBot\TelegramBotManager\BotManager;
|
17 | 21 |
|
18 | 22 | // Composer autoloader.
|
|
41 | 45 | }
|
42 | 46 |
|
43 | 47 | // Optional extras.
|
44 |
| - $extras = ['admins', 'commands', 'cron', 'limiter', 'logging', 'paths', 'valid_ips', 'webhook']; |
| 48 | + $extras = ['admins', 'commands', 'cron', 'limiter', 'paths', 'valid_ips', 'webhook']; |
45 | 49 | foreach ($extras as $extra) {
|
46 | 50 | if ($param = getenv('TG_' . strtoupper($extra))) {
|
47 | 51 | $params[$extra] = json_decode($param, true);
|
48 | 52 | }
|
49 | 53 | }
|
50 | 54 |
|
| 55 | + initLogging(); |
| 56 | + |
51 | 57 | $bot = new BotManager($params);
|
52 | 58 | $bot->run();
|
53 | 59 | } catch (TelegramLogException $e) {
|
54 | 60 | // Silence... beautiful silence =)
|
55 | 61 | } catch (\Throwable $e) {
|
56 | 62 | TelegramLog::error($e->getMessage());
|
57 | 63 | }
|
| 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