Passed
Push — master ( bf96b7...274159 )
by Ioannes
01:29
created

Log::notice()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 5
nop 2
1
<?php
2
namespace App;
3
4
use App\Monolog\FormatHelper;
5
use App\Monolog\LoggerFactory;
6
use Bitrix\Main\Diag\Debug;
0 ignored issues
show
Bug introduced by
The type Bitrix\Main\Diag\Debug was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Monolog\Logger;
8
use Psr\Log\LoggerInterface;
9
use Psr\Log\LogLevel;
10
11
class Log implements LoggerInterface {
12
13
    private $channel;
14
15
    /**
16
     * @param string $channel
17
     */
18
    public function __construct($channel = '') {
19
20
        if(!empty($channel)) {
21
            $this->channel = $channel;
22
        } else {
23
            $this->channel = ($_ENV['APP_LOG_BITRIX_CHANNEL'] ?: 'bitrix');
24
        }
25
    }
26
27
    /**
28
     * @param mixed $message
29
     * @param array $context
30
     */
31
    public function alert($message, $context = [])
32
    {
33
        try {
34
            $logger = LoggerFactory::getInstance($this->channel, $context);
35
            $message = FormatHelper::stringfyMessage($message);
36
            $logger->alert($message, (array) $context);
37
        } catch (\Exception $e) {
38
            $this->logInnerException($e);
39
        }
40
    }
41
42
    /**
43
     * @param mixed $message
44
     * @param array $context
45
     */
46
    public function critical($message, $context = [])
47
    {
48
        try {
49
            $logger = LoggerFactory::getInstance($this->channel, $context);
50
            $message = FormatHelper::stringfyMessage($message);
51
            $logger->critical($message, (array) $context);
52
        } catch (\Exception $e) {
53
            $this->logInnerException($e);
54
        }
55
    }
56
57
    /**
58
     * @param mixed $message
59
     * @param array $context
60
     */
61
    public function error($message, $context = [])
62
    {
63
        if ($this->isDebugEnabled(Logger::ERROR)) {
64
            try {
65
                $logger = LoggerFactory::getInstance($this->channel, $context);
66
                $message = FormatHelper::stringfyMessage($message);
67
                $logger->error($message, (array) $context);
68
            } catch (\Exception $e) {
69
                $this->logInnerException($e);
70
            }
71
        }
72
    }
73
74
    /**
75
     * @param mixed $message
76
     * @param array $context
77
     */
78
    public function warning($message, $context = [])
79
    {
80
        if ($this->isDebugEnabled(Logger::WARNING)) {
81
            try {
82
                $logger = LoggerFactory::getInstance($this->channel, $context);
83
                $message = FormatHelper::stringfyMessage($message);
84
                $logger->warning($message, (array) $context);
85
            } catch (\Exception $e) {
86
                $this->logInnerException($e);
87
            }
88
        }
89
    }
90
91
    /**
92
     * @param mixed $message
93
     * @param array $context
94
     */
95
    public function notice($message, $context = [])
96
    {
97
        if ($this->isDebugEnabled(Logger::NOTICE)) {
98
            try {
99
                $logger = LoggerFactory::getInstance($this->channel, $context);
100
                $message = FormatHelper::stringfyMessage($message);
101
                $logger->notice($message, (array) $context);
102
            } catch (\Exception $e) {
103
                $this->logInnerException($e);
104
            }
105
        }
106
    }
107
108
    /**
109
     * @param mixed $message
110
     * @param array $context
111
     */
112
    public function info($message, $context = [])
113
    {
114
        if ($this->isDebugEnabled(Logger::INFO)) {
115
            try {
116
                $logger = LoggerFactory::getInstance($this->channel, $context);
117
                $message = FormatHelper::stringfyMessage($message);
118
                $logger->info($message, (array) $context);
119
            } catch (\Exception $e) {
120
                $this->logInnerException($e);
121
            }
122
        }
123
    }
124
125
    /**
126
     * @param mixed $message
127
     * @param array $context
128
     */
129
    public function debug($message, $context = [])
130
    {
131
        if ($this->isDebugEnabled(Logger::DEBUG)) {
132
            try {
133
                $logger = LoggerFactory::getInstance($this->channel, $context);
134
                $message = FormatHelper::stringfyMessage($message);
135
                $logger->debug($message, (array) $context);
136
            } catch (\Exception $e) {
137
                $this->logInnerException($e);
138
            }
139
        }
140
    }
141
142
    /**
143
     * @param mixed $message
144
     * @param array $context
145
     */
146
    public function emergency($message, $context = [])
147
    {
148
        try {
149
            $logger = LoggerFactory::getInstance($this->channel, $context);
150
            $message = FormatHelper::stringfyMessage($message);
151
            $logger->emergency($message, (array) $context);
152
        } catch (\Exception $e) {
153
            $this->logInnerException($e);
154
        }
155
    }
156
157
    /**
158
     * @param mixed $level
159
     * @param mixed $message
160
     * @param array $context
161
     */
162
    public function log($level, $message, $context = [])
163
    {
164
        if ($this->isDebugEnabled($level)) {
165
            try {
166
                $logger = LoggerFactory::getInstance($this->channel, $context);
167
                $message = FormatHelper::stringfyMessage($message);
168
                $logger->log($level, $message, (array) $context);
169
            } catch (\Exception $e) {
170
                $this->logInnerException($e);
171
            }
172
        }
173
    }
174
175
    /**
176
     * @param mixed $level
177
     * @param mixed $message
178
     * @param array $context
179
     */
180
    public function telegram($level, $message, $context = []) {
181
182
        if (!$this->isDebugEnabled($level)) {
183
            return;
184
        }
185
        $this->log($level, $message, $context);
186
187
        $logger = LoggerFactory::getInstance('telegram-messenger', $context);
188
189
        if($logger) {
190
            $message = FormatHelper::stringfyTelegramMessage($message, (array) $context);
191
            $logger->log($level, $message, $context);
192
        }
193
    }
194
195
    /**
196
     * @param mixed $level
197
     * @return bool
198
     */
199
    private function isDebugEnabled($level)
200
    {
201
        if (defined('FORCE_DEBUG') && FORCE_DEBUG) {
0 ignored issues
show
Bug introduced by
The constant App\FORCE_DEBUG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
202
            return true;
203
        }
204
205
        $level = Logger::toMonologLevel($level);
206
207
        $minDebugLevel = ($_ENV['APP_DEBUG_LEVEL'] ?: LogLevel::DEBUG);
208
        $minDebugLevel = Logger::toMonologLevel($minDebugLevel);
209
210
        if($level >= $minDebugLevel) {
211
            return true;
212
        }
213
        return false;
214
    }
215
216
    /**
217
     * @param \Exception $exception
218
     */
219
    protected function logInnerException(\Exception $exception)
220
    {
221
        Debug::writeToFile((string) $exception, "", "inner_error.log");
222
    }
223
224
    public static function cleanLogs($daysAgo = 15) {
225
226
        $logPath = $_SERVER['DOCUMENT_ROOT'] . ($_ENV['APP_LOG_FOLDER'] ?: '/log/');
227
        $command = sprintf("find %s -mindepth 1 -type f -mtime +%d | xargs rm", $logPath, $daysAgo);
228
        exec($command);
229
        return sprintf('\App\Log::cleanLogs(%d);', $daysAgo);
230
    }
231
}
232