Logger::flushBuffer()   A
last analyzed

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 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 2
        if (!array_key_exists($level, self::$levels)) {
192 1
            return;
193
        }
194
195 2
        $levelFunction = strtolower(self::$levels[$level]);
196
197 2
        SeasLog::$levelFunction($message, $context);
198 2
    }
199
200
    /**
201
     * @param string $basePath
202
     *
203
     * @return bool
204
     */
205 22
    public function setBasePath(string $basePath)
206
    {
207 22
        return SeasLog::setBasePath($basePath);
208
    }
209
210
    /**
211
     * @return string
212
     */
213 1
    public function getBasePath()
214
    {
215 1
        return SeasLog::getBasePath();
216
    }
217
218
    /**
219
     * 设置本次请求标识.
220
     *
221
     * @param string
222
     *
223
     * @return bool
224
     */
225 2
    public static function setRequestID($request_id)
226
    {
227 2
        return SeasLog::setRequestID($request_id);
228
    }
229
230
    /**
231
     * 获取本次请求标识.
232
     *
233
     * @return string
234
     */
235 1
    public static function getRequestID()
236
    {
237 1
        return SeasLog::getRequestID();
238
    }
239
240
    /**
241
     * 设置模块目录.
242
     *
243
     * @param $module
244
     *
245
     * @return bool
246
     */
247 2
    public static function setLogger($module)
248
    {
249 2
        return SeasLog::setLogger($module);
250
    }
251
252
    /**
253
     * 获取最后一次设置的模块目录.
254
     *
255
     * @return string
256
     */
257 1
    public static function getLastLogger()
258
    {
259 1
        return SeasLog::getLastLogger();
260
    }
261
262
    /**
263
     * 设置DatetimeFormat配置.
264
     *
265
     * @param $format
266
     *
267
     * @return bool
268
     */
269 2
    public static function setDatetimeFormat($format)
270
    {
271 2
        return SeasLog::setDatetimeFormat($format);
272
    }
273
274
    /**
275
     * 返回当前DatetimeFormat配置格式.
276
     *
277
     * @return string
278
     */
279 1
    public static function getDatetimeFormat()
280
    {
281 1
        return SeasLog::getDatetimeFormat();
282
    }
283
284
    /**
285
     * 统计所有类型(或单个类型)行数.
286
     *
287
     * @param string $level
288
     * @param string $log_path
289
     * @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...
290
     *
291
     * @return array
292
     */
293 1
    public static function analyzerCount($level = 'all', $log_path = '*', $key_word = null)
294
    {
295 1
        return SeasLog::analyzerCount($level, $log_path, $key_word);
296
    }
297
298
    /**
299
     * 以数组形式,快速取出某类型log的各行详情.
300
     *
301
     * @param        $level
302
     * @param string $log_path
303
     * @param null $key_word
304
     * @param int $start
305
     * @param int $limit
306
     * @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...
307
     *
308
     * @return array
309
     */
310 1
    public static function analyzerDetail(
311
        $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...
312
        $log_path = '*',
313
        $key_word = null,
314
        $start = 1,
315
        $limit = 20,
316
        $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...
317
    ) {
318 1
        return SeasLog::analyzerDetail(
319 1
            $level,
320 1
            $log_path,
321 1
            $key_word,
322 1
            $start,
323 1
            $limit,
324 1
            $order
325
        );
326
    }
327
328
    /**
329
     * 获得当前日志buffer中的内容.
330
     *
331
     * @return array
332
     */
333 1
    public static function getBuffer()
334
    {
335 1
        return SeasLog::getBuffer();
336
    }
337
338
    /**
339
     * 将buffer中的日志立刻刷到硬盘.
340
     *
341
     * @return bool
342
     */
343 1
    public static function flushBuffer()
344
    {
345 1
        return SeasLog::flushBuffer();
346
    }
347
348
    /**
349
     * Create a custom SeasLog instance.
350
     *
351
     * @param array $config
352
     *
353
     * @return Logger
354
     */
355 1
    public function __invoke(array $config)
356
    {
357 1
        $logger = new Logger();
358 1
        if (!empty($config['path'])) {
359 1
            $logger->setBasePath($config['path']);
360
        }
361
362 1
        return $logger;
363
    }
364
365
    /**
366
     * Manually release stream flow from logger
367
     *
368
     * @param $type
369
     * @param string $name
370
     * @return bool
371
     */
372
    public static function closeLoggerStream($type = SEASLOG_CLOSE_LOGGER_STREAM_MOD_ALL, $name = '')
0 ignored issues
show
Bug introduced by
The constant Seasx\SeasLogger\SEASLOG...E_LOGGER_STREAM_MOD_ALL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
373
    {
374
        if (empty($name)) {
375
            return SeasLog::closeLoggerStream($type);
376
        }
377
378
        return SeasLog::closeLoggerStream($type, $name);
379
380
    }
381
}
382