Passed
Push — 5.2 ( 48fd10...14f2ed )
by liu
02:32
created

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