Completed
Push — 6.0 ( e11666...1ea569 )
by yun
06:15
created

Log::__call()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
// +----------------------------------------------------------------------
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 InvalidArgumentException;
16
use Psr\Log\LoggerInterface;
17
use think\event\LogWrite;
18
use think\helper\Arr;
19
use think\log\Channel;
20
use think\log\ChannelSet;
21
22
/**
23
 * 日志管理类
24
 * @package think
0 ignored issues
show
Coding Style introduced by
Package name "think" is not valid; consider "Think" instead
Loading history...
25
 * @mixin Channel
26
 */
27
class Log extends Manager implements LoggerInterface
28
{
29
    const EMERGENCY = 'emergency';
30
    const ALERT     = 'alert';
31
    const CRITICAL  = 'critical';
32
    const ERROR     = 'error';
33
    const WARNING   = 'warning';
34
    const NOTICE    = 'notice';
35
    const INFO      = 'info';
36
    const DEBUG     = 'debug';
37
    const SQL       = 'sql';
38
39
    protected $namespace = '\\think\\log\\driver\\';
40
41
    /**
42
     * 默认驱动
43
     * @return string|null
44
     */
45 2
    public function getDefaultDriver()
46
    {
47 2
        return $this->getConfig('default');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getConfig('default') also could return the type array which is incompatible with the documented return type null|string.
Loading history...
48
    }
49
50
    /**
51
     * 获取日志配置
52
     * @access public
53
     * @param null|string $name    名称
54
     * @param mixed       $default 默认值
55
     * @return mixed
56
     */
57 4
    public function getConfig(string $name = null, $default = null)
58
    {
59 4
        if (!is_null($name)) {
60 4
            return $this->app->config->get('log.' . $name, $default);
61
        }
62
63 1
        return $this->app->config->get('log');
64
    }
65
66
    /**
67
     * 获取渠道配置
68
     * @param string $channel
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
69
     * @param null   $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
70
     * @param null   $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
71
     * @return array
72
     */
73 4
    public function getChannelConfig($channel, $name = null, $default = null)
74
    {
75 4
        if ($config = $this->getConfig("channels.{$channel}")) {
76 3
            return Arr::get($config, $name, $default);
77
        }
78
79 1
        throw new InvalidArgumentException("Channel [$channel] not found.");
80
    }
81
82
    /**
83
     * driver()的别名
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
84
     * @param string|array $name 渠道名
85
     * @return Channel|ChannelSet
86
     */
87 4
    public function channel($name = null)
88
    {
89 4
        if (is_array($name)) {
90 2
            return new ChannelSet($this, $name);
91
        }
92
93 3
        return $this->driver($name);
94
    }
95
96 3
    protected function resolveType(string $name)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function resolveType()
Loading history...
97
    {
98 3
        return $this->getChannelConfig($name, 'type', 'file');
99
    }
100
101 3
    public function createDriver(string $name)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function createDriver()
Loading history...
102
    {
103 3
        $driver = parent::createDriver($name);
104
105 3
        $lazy  = !$this->getChannelConfig($name, "realtime_write", false) && !$this->app->runningInConsole();
106 3
        $allow = array_merge($this->getConfig("level", []), $this->getChannelConfig($name, "level", []));
107
108 3
        return new Channel($name, $driver, $allow, $lazy, $this->app->event);
109
    }
110
111 3
    protected function resolveConfig(string $name)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function resolveConfig()
Loading history...
112
    {
113 3
        return $this->getChannelConfig($name);
114
    }
115
116
    /**
117
     * 清空日志信息
118
     * @access public
119
     * @param string|array $channel 日志通道名
120
     * @return $this
121
     */
122 1
    public function clear($channel = '*')
123
    {
124 1
        if ('*' == $channel) {
125 1
            $channel = array_keys($this->drivers);
126
        }
127
128 1
        $this->channel($channel)->clear();
0 ignored issues
show
Bug introduced by
The method clear() does not exist on think\log\ChannelSet. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
        $this->channel($channel)->/** @scrutinizer ignore-call */ clear();
Loading history...
129
130 1
        return $this;
131
    }
132
133
    /**
134
     * 关闭本次请求日志写入
135
     * @access public
136
     * @param string|array $channel 日志通道名
137
     * @return $this
138
     */
139 1
    public function close($channel = '*')
140
    {
141 1
        if ('*' == $channel) {
142 1
            $channel = array_keys($this->drivers);
143
        }
144
145 1
        $this->channel($channel)->close();
0 ignored issues
show
Bug introduced by
The method close() does not exist on think\log\ChannelSet. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
        $this->channel($channel)->/** @scrutinizer ignore-call */ close();
Loading history...
146
147 1
        return $this;
148
    }
149
150
    /**
151
     * 获取日志信息
152
     * @access public
153
     * @param string $channel 日志通道名
154
     * @return array
155
     */
156 1
    public function getLog(string $channel = null): array
157
    {
158 1
        return $this->channel($channel)->getLog();
0 ignored issues
show
Bug introduced by
The method getLog() does not exist on think\log\ChannelSet. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

158
        return $this->channel($channel)->/** @scrutinizer ignore-call */ getLog();
Loading history...
Bug Best Practice introduced by
The expression return $this->channel($channel)->getLog() could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
159
    }
160
161
    /**
162
     * 保存日志信息
163
     * @access public
164
     * @return bool
165
     */
166 1
    public function save(): bool
167
    {
168
        /** @var Channel $channel */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
169 1
        foreach ($this->drivers as $channel) {
170 1
            $channel->save();
171
        }
172
173 1
        return true;
174
    }
175
176
    /**
177
     * 记录日志信息
178
     * @access public
179
     * @param mixed  $msg     日志信息
180
     * @param string $type    日志级别
181
     * @param array  $context 替换内容
182
     * @param bool   $lazy
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
183
     * @return $this
184
     */
185 2
    public function record($msg, string $type = 'info', array $context = [], bool $lazy = true)
186
    {
187 2
        $channel = $this->getConfig('type_channel.' . $type);
188
189 2
        $this->channel($channel)->record($msg, $type, $context, $lazy);
0 ignored issues
show
Bug introduced by
The method record() does not exist on think\log\ChannelSet. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

189
        $this->channel($channel)->/** @scrutinizer ignore-call */ record($msg, $type, $context, $lazy);
Loading history...
190
191 2
        return $this;
192
    }
193
194
    /**
195
     * 实时写入日志信息
196
     * @access public
197
     * @param mixed  $msg     调试信息
198
     * @param string $type    日志级别
199
     * @param array  $context 替换内容
200
     * @return $this
201
     */
202 1
    public function write($msg, string $type = 'info', array $context = [])
203
    {
204 1
        return $this->record($msg, $type, $context, false);
205
    }
206
207
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $listener should have a doc-comment as per coding-style.
Loading history...
208
     * 注册日志写入事件监听
209
     * @param $listener
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
210
     * @return Event
211
     */
212
    public function listen($listener)
213
    {
214
        return $this->app->event->listen(LogWrite::class, $listener);
215
    }
216
217
    /**
218
     * 记录日志信息
219
     * @access public
220
     * @param string $level   日志级别
221
     * @param mixed  $message 日志信息
222
     * @param array  $context 替换内容
223
     * @return void
224
     */
225 2
    public function log($level, $message, array $context = []): void
226
    {
227 2
        $this->record($message, $level, $context);
228 2
    }
229
230
    /**
231
     * 记录emergency信息
232
     * @access public
233
     * @param mixed $message 日志信息
234
     * @param array $context 替换内容
235
     * @return void
236
     */
237 1
    public function emergency($message, array $context = []): void
238
    {
239 1
        $this->log(__FUNCTION__, $message, $context);
240 1
    }
241
242
    /**
243
     * 记录警报信息
244
     * @access public
245
     * @param mixed $message 日志信息
246
     * @param array $context 替换内容
247
     * @return void
248
     */
249 1
    public function alert($message, array $context = []): void
250
    {
251 1
        $this->log(__FUNCTION__, $message, $context);
252 1
    }
253
254
    /**
255
     * 记录紧急情况
256
     * @access public
257
     * @param mixed $message 日志信息
258
     * @param array $context 替换内容
259
     * @return void
260
     */
261 1
    public function critical($message, array $context = []): void
262
    {
263 1
        $this->log(__FUNCTION__, $message, $context);
264 1
    }
265
266
    /**
267
     * 记录错误信息
268
     * @access public
269
     * @param mixed $message 日志信息
270
     * @param array $context 替换内容
271
     * @return void
272
     */
273 1
    public function error($message, array $context = []): void
274
    {
275 1
        $this->log(__FUNCTION__, $message, $context);
276 1
    }
277
278
    /**
279
     * 记录warning信息
280
     * @access public
281
     * @param mixed $message 日志信息
282
     * @param array $context 替换内容
283
     * @return void
284
     */
285 1
    public function warning($message, array $context = []): void
286
    {
287 1
        $this->log(__FUNCTION__, $message, $context);
288 1
    }
289
290
    /**
291
     * 记录notice信息
292
     * @access public
293
     * @param mixed $message 日志信息
294
     * @param array $context 替换内容
295
     * @return void
296
     */
297 1
    public function notice($message, array $context = []): void
298
    {
299 1
        $this->log(__FUNCTION__, $message, $context);
300 1
    }
301
302
    /**
303
     * 记录一般信息
304
     * @access public
305
     * @param mixed $message 日志信息
306
     * @param array $context 替换内容
307
     * @return void
308
     */
309 2
    public function info($message, array $context = []): void
310
    {
311 2
        $this->log(__FUNCTION__, $message, $context);
312 2
    }
313
314
    /**
315
     * 记录调试信息
316
     * @access public
317
     * @param mixed $message 日志信息
318
     * @param array $context 替换内容
319
     * @return void
320
     */
321 1
    public function debug($message, array $context = []): void
322
    {
323 1
        $this->log(__FUNCTION__, $message, $context);
324 1
    }
325
326
    /**
327
     * 记录sql信息
328
     * @access public
329
     * @param mixed $message 日志信息
330
     * @param array $context 替换内容
331
     * @return void
332
     */
333 1
    public function sql($message, array $context = []): void
334
    {
335 1
        $this->log(__FUNCTION__, $message, $context);
336 1
    }
337
338 1
    public function __call($method, $parameters)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __call()
Loading history...
339
    {
340 1
        $this->log($method, ...$parameters);
0 ignored issues
show
Bug introduced by
$parameters is expanded, but the parameter $message of think\Log::log() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

340
        $this->log($method, /** @scrutinizer ignore-type */ ...$parameters);
Loading history...
341 1
    }
342
}
343