Issues (10)

src/Monolog/LoggerFactory.php (3 issues)

1
<?php
2
namespace App\Monolog;
3
4
use Monolog\Formatter\LineFormatter;
5
use Monolog\Handler\StreamHandler;
6
use Monolog\Handler\TelegramBotHandler;
7
use Monolog\Logger;
8
use Monolog\Registry;
9
10
class LoggerFactory {
11
12
    const TELEGRAM_CHANNEL = 'telegram-messenger';
13
14
    public static function getInstance($channel, $context) {
15
16
        $key = md5(json_encode([$channel, $context]));
17
        if(Registry::hasLogger($key)) {
18
            return Registry::getInstance($key);
19
        }
20
21
        if($channel == self::TELEGRAM_CHANNEL) {
22
23
            if(isset($_ENV['APP_LOG_TELEGRAM'])) {
24
                $APP_LOG_TELEGRAM = trim(strtolower($_ENV['APP_LOG_TELEGRAM']));
25
                if($APP_LOG_TELEGRAM !== 'on' && $APP_LOG_TELEGRAM !== 'true' && $APP_LOG_TELEGRAM !== '1') {
26
                    return null;
27
                }
28
            } else {
29
                return null;
30
            }
31
32
            if(!isset($_ENV['APP_LOG_TELEGRAM_KEY']) || empty($_ENV['APP_LOG_TELEGRAM_KEY'])) {
33
                return null;
34
            }
35
            if(!isset($_ENV['APP_LOG_TELEGRAM_CHATID']) || empty($_ENV['APP_LOG_TELEGRAM_CHATID'])) {
36
                return null;
37
            }
38
39
            if(!class_exists('\Monolog\Handler\TelegramBotHandler')) {
40
                return null;
41
            }
42
43
            $sender = new TelegramBotHandler($_ENV['APP_LOG_TELEGRAM_KEY'], $_ENV['APP_LOG_TELEGRAM_CHATID']);
44
45
            if(isset($context['parse_mode']) && !empty($context['parse_mode'])) {
46
                $sender->setParseMode($context['parse_mode']);
47
                unset($context['parse_mode']);
48
            } else {
49
                $sender->setParseMode('HTML');
50
            }
51
            if(isset($context['disable_notification']) && $context['disable_notification'] === true) {
52
                $sender->disableNotification(true);
53
                unset($context['disable_notification']);
54
            }
55
            if(isset($context['disable_preview']) && $context['disable_preview'] === false) {
56
                $sender->disableWebPagePreview(false);
57
                unset($context['disable_preview']);
58
            } else {
59
                $sender->disableWebPagePreview(true);
60
            }
61
62
            $sender->setFormatter(new LineFormatter("%level_name%! %message%"));
63
64
            $telegramLogger = new Logger(self::TELEGRAM_CHANNEL);
65
            $telegramLogger->pushHandler($sender);
66
            Registry::addLogger($telegramLogger, $key, true);
67
68
            return $telegramLogger;
69
70
        } else {
71
72
            $logPath = $_SERVER['DOCUMENT_ROOT'] . ($_ENV['APP_LOG_FOLDER'] ?: '/log/');
73
            $logPath .= $channel . '/' . date('Y-m-d') . '.log';
74
            $logDir = pathinfo($logPath, PATHINFO_DIRNAME);
75
            if(!file_exists($logDir)) {
0 ignored issues
show
It seems like $logDir can also be of type array; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
            if(!file_exists(/** @scrutinizer ignore-type */ $logDir)) {
Loading history...
76
                $mode = 0775;
77
                if(defined('BX_DIR_PERMISSIONS') && BX_DIR_PERMISSIONS) {
0 ignored issues
show
The constant App\Monolog\BX_DIR_PERMISSIONS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
78
                    $mode = BX_DIR_PERMISSIONS;
79
                }
80
                mkdir($logDir, $mode, true);
0 ignored issues
show
It seems like $logDir can also be of type array; however, parameter $directory of mkdir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
                mkdir(/** @scrutinizer ignore-type */ $logDir, $mode, true);
Loading history...
81
            }
82
83
            $handler = new StreamHandler($logPath);
84
            $handler->setFormatter(new ArrayFormatter());
85
86
            $logger = new Logger($channel);
87
            $logger->pushHandler($handler);
88
            Registry::addLogger($logger, $key, true);
89
90
            return $logger;
91
        }
92
    }
93
}
94