Completed
Push — master ( 28d5b3...cf7798 )
by wujunze
03:07
created

Logger::log()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 33
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 13.3199

Importance

Changes 0
Metric Value
cc 10
eloc 29
nc 10
nop 3
dl 0
loc 33
ccs 19
cts 28
cp 0.6786
crap 13.3199
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
     * Monolog API version.
81
     *
82
     * This is only bumped when API breaks are done and should
83
     * follow the major version of the library
84
     *
85
     * @var int
86
     */
87
    const API = 2;
88
89
    /**
90
     * Logging levels from syslog protocol defined in RFC 5424.
91
     *
92
     * This is a static variable and not a constant to serve as an extension point for custom levels
93
     *
94
     * @var string[] Logging levels with the levels as key
95
     */
96
    protected static $levels = [
97
        self::DEBUG => 'DEBUG',
98
        self::INFO => 'INFO',
99
        self::NOTICE => 'NOTICE',
100
        self::WARNING => 'WARNING',
101
        self::ERROR => 'ERROR',
102
        self::CRITICAL => 'CRITICAL',
103
        self::ALERT => 'ALERT',
104
        self::EMERGENCY => 'EMERGENCY',
105
    ];
106
107
    /**
108
     * set request level for seaslog.
109
     *
110
     * @param int $level
111
     */
112 1
    public function setRequestLevel($level = self::ALL)
113
    {
114 1
        self::$RequestLevel = $level;
115 1
    }
116
117
    /**
118
     * @param string $message
119
     * @param array  $context
120
     */
121 1
    public function emergency($message, array $context = [])
122
    {
123 1
        SeasLog::emergency($message, $context);
124 1
    }
125
126
    /**
127
     * @param string $message
128
     * @param array  $context
129
     */
130 1
    public function alert($message, array $context = [])
131
    {
132 1
        SeasLog::alert($message, $context);
133 1
    }
134
135
    /**
136
     * @param string $message
137
     * @param array  $context
138
     */
139 2
    public function critical($message, array $context = [])
140
    {
141 2
        SeasLog::critical($message, $context);
142 2
    }
143
144
    /**
145
     * @param string $message
146
     * @param array  $context
147
     */
148
    public function error($message, array $context = [])
149
    {
150
        SeasLog::error($message, $context);
151
    }
152
153
    /**
154
     * @param string $message
155
     * @param array  $context
156
     */
157 1
    public function warning($message, array $context = [])
158
    {
159 1
        SeasLog::warning($message, $context);
160 1
    }
161
162
    /**
163
     * @param string $message
164
     * @param array  $context
165
     */
166 1
    public function notice($message, array $context = [])
167
    {
168 1
        SeasLog::notice($message, $context);
169 1
    }
170
171
    /**
172
     * @param string $message
173
     * @param array  $context
174
     */
175 1
    public function info($message, array $context = [])
176
    {
177 1
        SeasLog::info($message, $context);
178 1
    }
179
180
    /**
181
     * @param string $message
182
     * @param array  $context
183
     */
184 1
    public function debug($message, array $context = [])
185
    {
186 1
        SeasLog::debug($message, $context);
187 1
    }
188
189
    /**
190
     * @param mixed  $level
191
     * @param string $message
192
     * @param array  $context
193
     */
194 2
    public function log($level, $message, array $context = [])
195
    {
196 2
        if ((int) $level < self::$RequestLevel) {
197 1
            return;
198
        }
199
200
        switch ($level) {
201 2
            case self::EMERGENCY:
202 1
                SeasLog::emergency($message, $context);
203 1
                break;
204 2
            case self::ALERT:
205
                SeasLog::alert($message, $context);
206
                break;
207 2
            case self::CRITICAL:
208 1
                SeasLog::critical($message, $context);
209 1
                break;
210 2
            case self::ERROR:
211 1
                SeasLog::error($message, $context);
212 1
                break;
213 1
            case self::WARNING:
214
                SeasLog::warning($message, $context);
215
                break;
216 1
            case self::NOTICE:
217
                SeasLog::notice($message, $context);
218
                break;
219 1
            case self::INFO:
220
                SeasLog::info($message, $context);
221
                break;
222 1
            case self::DEBUG:
223 1
                SeasLog::debug($message, $context);
224 1
                break;
225
            default:
226
                break;
227
        }
228 2
    }
229
230
    /**
231
     * @param string $basePath
232
     *
233
     * @return bool
234
     */
235 10
    public function setBasePath(string $basePath)
236
    {
237 10
        return SeasLog::setBasePath($basePath);
238
    }
239
240
    /**
241
     * @return string
242
     */
243
    public function getBasePath()
244
    {
245
        return SeasLog::getBasePath();
246
    }
247
248
    /**
249
     * 设置本次请求标识.
250
     *
251
     * @param string
252
     *
253
     * @return bool
254
     */
255
    public static function setRequestID($request_id)
256
    {
257
        return SeasLog::setRequestID($request_id);
258
    }
259
260
    /**
261
     * 获取本次请求标识.
262
     *
263
     * @return string
264
     */
265
    public static function getRequestID()
266
    {
267
        return SeasLog::getRequestID();
268
    }
269
270
    /**
271
     * 设置模块目录.
272
     *
273
     * @param $module
274
     *
275
     * @return bool
276
     */
277
    public static function setLogger($module)
278
    {
279
        return SeasLog::setLogger($module);
280
    }
281
282
    /**
283
     * 获取最后一次设置的模块目录.
284
     *
285
     * @return string
286
     */
287
    public static function getLastLogger()
288
    {
289
        return SeasLog::getLastLogger();
290
    }
291
292
    /**
293
     * 设置DatetimeFormat配置.
294
     *
295
     * @param $format
296
     *
297
     * @return bool
298
     */
299
    public static function setDatetimeFormat($format)
300
    {
301
        return SeasLog::setDatetimeFormat($format);
302
    }
303
304
    /**
305
     * 返回当前DatetimeFormat配置格式.
306
     *
307
     * @return string
308
     */
309
    public static function getDatetimeFormat()
310
    {
311
        return SeasLog::getDatetimeFormat();
312
    }
313
314
    /**
315
     * 统计所有类型(或单个类型)行数.
316
     *
317
     * @param string $level
318
     * @param string $log_path
319
     * @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...
320
     *
321
     * @return array
322
     */
323
    public static function analyzerCount($level = 'all', $log_path = '*', $key_word = null)
324
    {
325
        return SeasLog::analyzerCount($level, $log_path, $key_word);
326
    }
327
328
    /**
329
     * 以数组形式,快速取出某类型log的各行详情.
330
     *
331
     * @param        $level
332
     * @param string $log_path
333
     * @param null   $key_word
334
     * @param int    $start
335
     * @param int    $limit
336
     * @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...
337
     *
338
     * @return array
339
     */
340
    public static function analyzerDetail(
341
        $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...
342
        $log_path = '*',
343
        $key_word = null,
344
        $start = 1,
345
        $limit = 20,
346
        $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...
347
    ) {
348
        return SeasLog::analyzerDetail(
349
            $level,
350
            $log_path,
351
            $key_word,
352
            $start,
353
            $limit,
354
            $order
355
        );
356
    }
357
358
    /**
359
     * 获得当前日志buffer中的内容.
360
     *
361
     * @return array
362
     */
363
    public static function getBuffer()
364
    {
365
        return SeasLog::getBuffer();
366
    }
367
368
    /**
369
     * 将buffer中的日志立刻刷到硬盘.
370
     *
371
     * @return bool
372
     */
373
    public static function flushBuffer()
374
    {
375
        return SeasLog::flushBuffer();
376
    }
377
378
    /**
379
     * Create a custom SeasLog instance.
380
     *
381
     * @param array $config
382
     *
383
     * @return \SeasLog\Logger
0 ignored issues
show
Bug introduced by
The type SeasLog\Logger 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...
384
     */
385
    public function __invoke(array $config)
386
    {
387
        $logger = new Logger();
388
        if (!empty($config['path'])) {
389
            $logger->setBasePath($config['path']);
390
        }
391
392
        return $logger;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $logger returns the type Seasx\SeasLogger\Logger which is incompatible with the documented return type SeasLog\Logger.
Loading history...
393
    }
394
}
395