Passed
Push — 5.2 ( b20e27...8edef9 )
by liu
03:45 queued 47s
created

Log::notice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think;
14
15
use Psr\Log\LoggerInterface;
16
use think\facade\App;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, think\App. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use think\facade\Event;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, think\Event. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
19
class Log implements LoggerInterface
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
20
{
21
    const EMERGENCY = 'emergency';
22
    const ALERT     = 'alert';
23
    const CRITICAL  = 'critical';
24
    const ERROR     = 'error';
25
    const WARNING   = 'warning';
26
    const NOTICE    = 'notice';
27
    const INFO      = 'info';
28
    const DEBUG     = 'debug';
29
    const SQL       = 'sql';
30
31
    /**
32
     * 日志信息
33
     * @var array
34
     */
35
    protected $log = [];
36
37
    /**
38
     * 配置参数
39
     * @var array
40
     */
41
    protected $config = [];
42
43
    /**
44
     * 日志写入驱动
45
     * @var object
46
     */
47
    protected $driver;
48
49
    /**
50
     * 日志授权key
51
     * @var string
52
     */
53
    protected $key;
54
55
    /**
56
     * 是否允许日志写入
57
     * @var bool
58
     */
59
    protected $allowWrite = true;
60
61
    public static function __make(Config $config)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Method name "Log::__make" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
62
    {
63
        return (new static())->init($config->get('log'));
0 ignored issues
show
Bug introduced by
It seems like $config->get('log') can also be of type null; however, parameter $config of think\Log::init() does only seem to accept array, 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

63
        return (new static())->init(/** @scrutinizer ignore-type */ $config->get('log'));
Loading history...
64
    }
65
66
    /**
67
     * 日志初始化
68
     * @access public
69
     * @param  array $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
70
     * @return $this
71
     */
72
    public function init(array $config = [])
73
    {
74
        $type = $config['type'] ?? 'File';
75
76
        $this->config = $config;
77
78
        unset($config['type']);
79
        if (!empty($config['close'])) {
80
            $this->allowWrite = false;
81
        }
82
83
        $this->driver = App::factory($type, '\\think\\log\\driver\\', $config);
84
85
        return $this;
86
    }
87
88
    /**
89
     * 获取日志信息
90
     * @access public
91
     * @param  string $type 信息类型
92
     * @return array
93
     */
94
    public function getLog(string $type = ''): array
95
    {
96
        return $type ? $this->log[$type] : $this->log;
97
    }
98
99
    /**
100
     * 记录日志信息
101
     * @access public
102
     * @param  mixed  $msg       日志信息
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 7 found
Loading history...
103
     * @param  string $type      日志级别
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 6 found
Loading history...
104
     * @param  array  $context   替换内容
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
105
     * @return $this
106
     */
107
    public function record($msg, string $type = 'info', array $context = [])
108
    {
109
        if (!$this->allowWrite) {
110
            return;
111
        }
112
113
        if (is_string($msg) && !empty($context)) {
114
            $replace = [];
115
            foreach ($context as $key => $val) {
116
                $replace['{' . $key . '}'] = $val;
117
            }
118
119
            $msg = strtr($msg, $replace);
120
        }
121
122
        if (PHP_SAPI == 'cli') {
123
            if (empty($this->config['level']) || in_array($type, $this->config['level'])) {
124
                // 命令行日志实时写入
125
                $this->write($msg, $type, true);
126
            }
127
        } else {
128
            $this->log[$type][] = $msg;
129
        }
130
131
        return $this;
132
    }
133
134
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $log should have a doc-comment as per coding-style.
Loading history...
135
     * 记录批量日志信息
136
     * @access public
137
     * @param  array  $msg       日志信息
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $msg does not match actual variable name $log
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 7 found
Loading history...
138
     * @param  string $type      日志级别
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 6 found
Loading history...
139
     * @return $this
140
     */
141
    public function append(array $log, string $type = 'info')
142
    {
143
        if (!$this->allowWrite || empty($log)) {
144
            return $this;
145
        }
146
147
        if (isset($this->log[$type])) {
148
            $this->log[$type] += $log;
149
        } else {
150
            $this->log[$type] = $log;
151
        }
152
153
        return $this;
154
    }
155
156
    /**
157
     * 清空日志信息
158
     * @access public
159
     * @return $this
160
     */
161
    public function clear()
162
    {
163
        $this->log = [];
164
165
        return $this;
166
    }
167
168
    /**
169
     * 当前日志记录的授权key
170
     * @access public
171
     * @param  string  $key  授权key
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
172
     * @return $this
173
     */
174
    public function key(string $key)
175
    {
176
        $this->key = $key;
177
178
        return $this;
179
    }
180
181
    /**
182
     * 检查日志写入权限
183
     * @access public
184
     * @param  array  $config  当前日志配置参数
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
185
     * @return bool
186
     */
187
    public function check(array $config): bool
188
    {
189
        if ($this->key && !empty($config['allow_key']) && !in_array($this->key, $config['allow_key'])) {
190
            return false;
191
        }
192
193
        return true;
194
    }
195
196
    /**
197
     * 关闭本次请求日志写入
198
     * @access public
199
     * @return $this
200
     */
201
    public function close()
202
    {
203
        $this->allowWrite = false;
204
        $this->log        = [];
205
206
        return $this;
207
    }
208
209
    /**
210
     * 保存调试信息
211
     * @access public
212
     * @return bool
213
     */
214
    public function save(): bool
215
    {
216
        if (empty($this->log) || !$this->allowWrite) {
217
            return true;
218
        }
219
220
        if (!$this->check($this->config)) {
221
            // 检测日志写入权限
222
            return false;
223
        }
224
225
        $log = [];
226
227
        foreach ($this->log as $level => $info) {
228
            if (!App::isDebug() && 'debug' == $level) {
229
                continue;
230
            }
231
232
            if (empty($this->config['level']) || in_array($level, $this->config['level'])) {
233
                $log[$level] = $info;
234
                Event::trigger('LogLevel', [$level, $info]);
235
            }
236
        }
237
238
        $result = $this->driver->save($log);
239
240
        if ($result) {
241
            $this->log = [];
242
        }
243
244
        return $result;
245
    }
246
247
    /**
248
     * 实时写入日志信息 并支持行为
249
     * @access public
250
     * @param  mixed  $msg   调试信息
251
     * @param  string $type  日志级别
252
     * @param  bool   $force 是否强制写入
253
     * @return bool
254
     */
255
    public function write($msg, string $type = 'info', bool $force = false): bool
256
    {
257
        // 封装日志信息
258
        if (empty($this->config['level'])) {
259
            $force = true;
260
        }
261
262
        $log = [];
263
264
        if (true === $force || in_array($type, $this->config['level'])) {
265
            $log[$type][] = $msg;
266
        } else {
267
            return false;
268
        }
269
270
        // 监听LogWrite
271
        Event::trigger('LogWrite', $log);
272
273
        // 写入日志
274
        return $this->driver->save($log, false);
275
    }
276
277
    /**
278
     * 记录日志信息
279
     * @access public
280
     * @param  string $level     日志级别
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 5 found
Loading history...
281
     * @param  mixed  $message   日志信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
282
     * @param  array  $context   替换内容
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
283
     * @return void
284
     */
285
    public function log($level, $message, array $context = []): void
286
    {
287
        $this->record($message, $level, $context);
288
    }
289
290
    /**
291
     * 记录emergency信息
292
     * @access public
293
     * @param  mixed  $message   日志信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
294
     * @param  array  $context   替换内容
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
295
     * @return void
296
     */
297
    public function emergency($message, array $context = []): void
298
    {
299
        $this->log(__FUNCTION__, $message, $context);
300
    }
301
302
    /**
303
     * 记录警报信息
304
     * @access public
305
     * @param  mixed  $message   日志信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
306
     * @param  array  $context   替换内容
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
307
     * @return void
308
     */
309
    public function alert($message, array $context = []): void
310
    {
311
        $this->log(__FUNCTION__, $message, $context);
312
    }
313
314
    /**
315
     * 记录紧急情况
316
     * @access public
317
     * @param  mixed  $message   日志信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
318
     * @param  array  $context   替换内容
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
319
     * @return void
320
     */
321
    public function critical($message, array $context = []): void
322
    {
323
        $this->log(__FUNCTION__, $message, $context);
324
    }
325
326
    /**
327
     * 记录错误信息
328
     * @access public
329
     * @param  mixed  $message   日志信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
330
     * @param  array  $context   替换内容
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
331
     * @return void
332
     */
333
    public function error($message, array $context = []): void
334
    {
335
        $this->log(__FUNCTION__, $message, $context);
336
    }
337
338
    /**
339
     * 记录warning信息
340
     * @access public
341
     * @param  mixed  $message   日志信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
342
     * @param  array  $context   替换内容
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
343
     * @return void
344
     */
345
    public function warning($message, array $context = []): void
346
    {
347
        $this->log(__FUNCTION__, $message, $context);
348
    }
349
350
    /**
351
     * 记录notice信息
352
     * @access public
353
     * @param  mixed  $message   日志信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
354
     * @param  array  $context   替换内容
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
355
     * @return void
356
     */
357
    public function notice($message, array $context = []): void
358
    {
359
        $this->log(__FUNCTION__, $message, $context);
360
    }
361
362
    /**
363
     * 记录一般信息
364
     * @access public
365
     * @param  mixed  $message   日志信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
366
     * @param  array  $context   替换内容
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
367
     * @return void
368
     */
369
    public function info($message, array $context = []): void
370
    {
371
        $this->log(__FUNCTION__, $message, $context);
372
    }
373
374
    /**
375
     * 记录调试信息
376
     * @access public
377
     * @param  mixed  $message   日志信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
378
     * @param  array  $context   替换内容
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
379
     * @return void
380
     */
381
    public function debug($message, array $context = []): void
382
    {
383
        $this->log(__FUNCTION__, $message, $context);
384
    }
385
386
    /**
387
     * 记录sql信息
388
     * @access public
389
     * @param  mixed  $message   日志信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
390
     * @param  array  $context   替换内容
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
391
     * @return void
392
     */
393
    public function sql($message, array $context = []): void
394
    {
395
        $this->log(__FUNCTION__, $message, $context);
396
    }
397
}
398