Passed
Push — master ( 9e0b17...76f2be )
by wujunze
02:46
created

Logger::error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the seasx/seas-logger.
5
 *
6
 * (c) Panda <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Seasx\SeasLogger;
13
14
use Psr\Log\LoggerInterface;
15
use SeasLog;
0 ignored issues
show
Bug introduced by
The type SeasLog 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...
16
17
class Logger implements LoggerInterface
18
{
19
    /**
20
     * All level.
21
     */
22
    const ALL = -2147483647;
23
24
    /**
25
     * Detailed debug information.
26
     */
27
    const DEBUG = 100;
28
29
    /**
30
     * Interesting events.
31
     *
32
     * Examples: User logs in, SQL logs.
33
     */
34
    const INFO = 200;
35
36
    /**
37
     * Uncommon events.
38
     */
39
    const NOTICE = 250;
40
41
    /**
42
     * Exceptional occurrences that are not errors.
43
     *
44
     * Examples: Use of deprecated APIs, poor use of an API,
45
     * undesirable things that are not necessarily wrong.
46
     */
47
    const WARNING = 300;
48
49
    /**
50
     * Runtime errors.
51
     */
52
    const ERROR = 400;
53
54
    /**
55
     * Critical conditions.
56
     *
57
     * Example: Application component unavailable, unexpected exception.
58
     */
59
    const CRITICAL = 500;
60
61
    /**
62
     * Action must be taken immediately.
63
     *
64
     * Example: Entire website down, database unavailable, etc.
65
     * This should trigger the SMS alerts and wake you up.
66
     */
67
    const ALERT = 550;
68
69
    /**
70
     * Urgent alert.
71
     */
72
    const EMERGENCY = 600;
73
74
    /**
75
     * request Level limit.
76
     */
77
    public static $RequestLevel = self::ALL;
78
79
80
    /**
81
     * Logging levels from syslog protocol defined in RFC 5424.
82
     *
83
     * This is a static variable and not a constant to serve as an extension point for custom levels
84
     *
85
     * @var string[] Logging levels with the levels as key
86
     */
87
    protected static $levels = [
88
        self::DEBUG => 'DEBUG',
89
        self::INFO => 'INFO',
90
        self::NOTICE => 'NOTICE',
91
        self::WARNING => 'WARNING',
92
        self::ERROR => 'ERROR',
93
        self::CRITICAL => 'CRITICAL',
94
        self::ALERT => 'ALERT',
95
        self::EMERGENCY => 'EMERGENCY',
96
    ];
97
98
    /**
99
     * set request level for seaslog.
100
     *
101
     * @param int $level
102
     */
103 1
    public function setRequestLevel($level = self::ALL)
104
    {
105 1
        self::$RequestLevel = $level;
106 1
    }
107
108
    /**
109
     * @param string $message
110
     * @param array  $context
111
     */
112 1
    public function emergency($message, array $context = [])
113
    {
114 1
        SeasLog::emergency($message, $context);
115 1
    }
116
117
    /**
118
     * @param string $message
119
     * @param array  $context
120
     */
121 1
    public function alert($message, array $context = [])
122
    {
123 1
        SeasLog::alert($message, $context);
124 1
    }
125
126
    /**
127
     * @param string $message
128
     * @param array  $context
129
     */
130 1
    public function critical($message, array $context = [])
131
    {
132 1
        SeasLog::critical($message, $context);
133 1
    }
134
135
    /**
136
     * @param string $message
137
     * @param array  $context
138
     */
139 1
    public function error($message, array $context = [])
140
    {
141 1
        SeasLog::error($message, $context);
142 1
    }
143
144
    /**
145
     * @param string $message
146
     * @param array  $context
147
     */
148 1
    public function warning($message, array $context = [])
149
    {
150 1
        SeasLog::warning($message, $context);
151 1
    }
152
153
    /**
154
     * @param string $message
155
     * @param array  $context
156
     */
157 1
    public function notice($message, array $context = [])
158
    {
159 1
        SeasLog::notice($message, $context);
160 1
    }
161
162
    /**
163
     * @param string $message
164
     * @param array  $context
165
     */
166 2
    public function info($message, array $context = [])
167
    {
168 2
        SeasLog::info($message, $context);
169 2
    }
170
171
    /**
172
     * @param string $message
173
     * @param array  $context
174
     */
175 1
    public function debug($message, array $context = [])
176
    {
177 1
        SeasLog::debug($message, $context);
178 1
    }
179
180
    /**
181
     * @param mixed  $level
182
     * @param string $message
183
     * @param array  $context
184
     */
185 2
    public function log($level, $message, array $context = [])
186
    {
187 2
        if ((int) $level < self::$RequestLevel) {
188 1
            return;
189
        }
190
191
        switch ($level) {
192 2
            case self::EMERGENCY:
193 1
                SeasLog::emergency($message, $context);
194 1
                break;
195 2
            case self::ALERT:
196 1
                SeasLog::alert($message, $context);
197 1
                break;
198 2
            case self::CRITICAL:
199 1
                SeasLog::critical($message, $context);
200 1
                break;
201 2
            case self::ERROR:
202 1
                SeasLog::error($message, $context);
203 1
                break;
204 2
            case self::WARNING:
205 1
                SeasLog::warning($message, $context);
206 1
                break;
207 2
            case self::NOTICE:
208 1
                SeasLog::notice($message, $context);
209 1
                break;
210 2
            case self::INFO:
211 1
                SeasLog::info($message, $context);
212 1
                break;
213 2
            case self::DEBUG:
214 2
                SeasLog::debug($message, $context);
215 2
                break;
216
            default:
217 1
                break;
218
        }
219 2
    }
220
221
    /**
222
     * @param string $basePath
223
     *
224
     * @return bool
225
     */
226 22
    public function setBasePath(string $basePath)
227
    {
228 22
        return SeasLog::setBasePath($basePath);
229
    }
230
231
    /**
232
     * @return string
233
     */
234 1
    public function getBasePath()
235
    {
236 1
        return SeasLog::getBasePath();
237
    }
238
239
    /**
240
     * 设置本次请求标识.
241
     *
242
     * @param string
243
     *
244
     * @return bool
245
     */
246 2
    public static function setRequestID($request_id)
247
    {
248 2
        return SeasLog::setRequestID($request_id);
249
    }
250
251
    /**
252
     * 获取本次请求标识.
253
     *
254
     * @return string
255
     */
256 1
    public static function getRequestID()
257
    {
258 1
        return SeasLog::getRequestID();
259
    }
260
261
    /**
262
     * 设置模块目录.
263
     *
264
     * @param $module
265
     *
266
     * @return bool
267
     */
268 2
    public static function setLogger($module)
269
    {
270 2
        return SeasLog::setLogger($module);
271
    }
272
273
    /**
274
     * 获取最后一次设置的模块目录.
275
     *
276
     * @return string
277
     */
278 1
    public static function getLastLogger()
279
    {
280 1
        return SeasLog::getLastLogger();
281
    }
282
283
    /**
284
     * 设置DatetimeFormat配置.
285
     *
286
     * @param $format
287
     *
288
     * @return bool
289
     */
290 2
    public static function setDatetimeFormat($format)
291
    {
292 2
        return SeasLog::setDatetimeFormat($format);
293
    }
294
295
    /**
296
     * 返回当前DatetimeFormat配置格式.
297
     *
298
     * @return string
299
     */
300 1
    public static function getDatetimeFormat()
301
    {
302 1
        return SeasLog::getDatetimeFormat();
303
    }
304
305
    /**
306
     * 统计所有类型(或单个类型)行数.
307
     *
308
     * @param string $level
309
     * @param string $log_path
310
     * @param null   $key_word
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $key_word is correct as it would always require null to be passed?
Loading history...
311
     *
312
     * @return array
313
     */
314 1
    public static function analyzerCount($level = 'all', $log_path = '*', $key_word = null)
315
    {
316 1
        return SeasLog::analyzerCount($level, $log_path, $key_word);
317
    }
318
319
    /**
320
     * 以数组形式,快速取出某类型log的各行详情.
321
     *
322
     * @param        $level
323
     * @param string $log_path
324
     * @param null   $key_word
325
     * @param int    $start
326
     * @param int    $limit
327
     * @param        $order    默认为正序 SEASLOG_DETAIL_ORDER_ASC,可选倒序 SEASLOG_DETAIL_ORDER_DESC
0 ignored issues
show
Documentation Bug introduced by
The doc comment 默认为正序 at position 0 could not be parsed: Unknown type name '默认为正序' at position 0 in 默认为正序.
Loading history...
328
     *
329
     * @return array
330
     */
331 1
    public static function analyzerDetail(
332
        $level = SEASLOG_INFO,
0 ignored issues
show
Bug introduced by
The constant Seasx\SeasLogger\SEASLOG_INFO was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
333
        $log_path = '*',
334
        $key_word = null,
335
        $start = 1,
336
        $limit = 20,
337
        $order = SEASLOG_DETAIL_ORDER_ASC
0 ignored issues
show
Bug introduced by
The constant Seasx\SeasLogger\SEASLOG_DETAIL_ORDER_ASC was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
338
    ) {
339 1
        return SeasLog::analyzerDetail(
340 1
            $level,
341 1
            $log_path,
342 1
            $key_word,
343 1
            $start,
344 1
            $limit,
345 1
            $order
346
        );
347
    }
348
349
    /**
350
     * 获得当前日志buffer中的内容.
351
     *
352
     * @return array
353
     */
354 1
    public static function getBuffer()
355
    {
356 1
        return SeasLog::getBuffer();
357
    }
358
359
    /**
360
     * 将buffer中的日志立刻刷到硬盘.
361
     *
362
     * @return bool
363
     */
364 1
    public static function flushBuffer()
365
    {
366 1
        return SeasLog::flushBuffer();
367
    }
368
369
    /**
370
     * Create a custom SeasLog instance.
371
     *
372
     * @param array $config
373
     *
374
     * @return Logger
375
     */
376 1
    public function __invoke(array $config)
377
    {
378 1
        $logger = new Logger();
379 1
        if (!empty($config['path'])) {
380 1
            $logger->setBasePath($config['path']);
381
        }
382
383 1
        return $logger;
384
    }
385
}
386