Completed
Push — 6.0 ( cb8a0b...bb9f45 )
by yun
06:00
created

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

127
        $this->channel($channel)->/** @scrutinizer ignore-call */ clear();
Loading history...
128
129 1
        return $this;
130
    }
131
132
    /**
133
     * 关闭本次请求日志写入
134
     * @access public
135
     * @param string|array $channel 日志通道名
136
     * @return $this
137
     */
138 1
    public function close($channel = '*')
139
    {
140 1
        if ('*' == $channel) {
141 1
            $channel = array_keys($this->drivers);
142
        }
143
144 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

144
        $this->channel($channel)->/** @scrutinizer ignore-call */ close();
Loading history...
145
146 1
        return $this;
147
    }
148
149
    /**
150
     * 获取日志信息
151
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
152 1
    public function getLog()
153
    {
154 1
        return $this->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

154
        return $this->channel()->/** @scrutinizer ignore-call */ getLog();
Loading history...
155
    }
156
157
    /**
158
     * 保存日志信息
159
     * @access public
160
     * @return bool
161
     */
162 1
    public function save(): bool
163
    {
164
        /** @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...
165 1
        foreach ($this->drivers as $channel) {
166 1
            $channel->save();
167
        }
168
169 1
        return true;
170
    }
171
172
    /**
173
     * 记录日志信息
174
     * @access public
175
     * @param mixed  $msg     日志信息
176
     * @param string $type    日志级别
177
     * @param array  $context 替换内容
178
     * @param bool   $lazy
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
179
     * @return $this
180
     */
181 2
    public function record($msg, string $type = 'info', array $context = [], $lazy = true)
182
    {
183 2
        $channel = $this->getConfig('type_channel.' . $type);
184
185 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

185
        $this->channel($channel)->/** @scrutinizer ignore-call */ record($msg, $type, $context, $lazy);
Loading history...
186
187 2
        return $this;
188
    }
189
190
    /**
191
     * 实时写入日志信息
192
     * @access public
193
     * @param mixed  $msg     调试信息
194
     * @param string $type    日志级别
195
     * @param array  $context 替换内容
196
     * @return $this
197
     */
198 1
    public function write($msg, string $type = 'info', array $context = [])
199
    {
200 1
        return $this->record($msg, $type, $context, false);
201
    }
202
203
    /**
204
     * 记录日志信息
205
     * @access public
206
     * @param string $level   日志级别
207
     * @param mixed  $message 日志信息
208
     * @param array  $context 替换内容
209
     * @return void
210
     */
211 2
    public function log($level, $message, array $context = []): void
212
    {
213 2
        $this->record($message, $level, $context);
214 2
    }
215
216
    /**
217
     * 记录emergency信息
218
     * @access public
219
     * @param mixed $message 日志信息
220
     * @param array $context 替换内容
221
     * @return void
222
     */
223 1
    public function emergency($message, array $context = []): void
224
    {
225 1
        $this->log(__FUNCTION__, $message, $context);
226 1
    }
227
228
    /**
229
     * 记录警报信息
230
     * @access public
231
     * @param mixed $message 日志信息
232
     * @param array $context 替换内容
233
     * @return void
234
     */
235 1
    public function alert($message, array $context = []): void
236
    {
237 1
        $this->log(__FUNCTION__, $message, $context);
238 1
    }
239
240
    /**
241
     * 记录紧急情况
242
     * @access public
243
     * @param mixed $message 日志信息
244
     * @param array $context 替换内容
245
     * @return void
246
     */
247 1
    public function critical($message, array $context = []): void
248
    {
249 1
        $this->log(__FUNCTION__, $message, $context);
250 1
    }
251
252
    /**
253
     * 记录错误信息
254
     * @access public
255
     * @param mixed $message 日志信息
256
     * @param array $context 替换内容
257
     * @return void
258
     */
259 1
    public function error($message, array $context = []): void
260
    {
261 1
        $this->log(__FUNCTION__, $message, $context);
262 1
    }
263
264
    /**
265
     * 记录warning信息
266
     * @access public
267
     * @param mixed $message 日志信息
268
     * @param array $context 替换内容
269
     * @return void
270
     */
271 1
    public function warning($message, array $context = []): void
272
    {
273 1
        $this->log(__FUNCTION__, $message, $context);
274 1
    }
275
276
    /**
277
     * 记录notice信息
278
     * @access public
279
     * @param mixed $message 日志信息
280
     * @param array $context 替换内容
281
     * @return void
282
     */
283 1
    public function notice($message, array $context = []): void
284
    {
285 1
        $this->log(__FUNCTION__, $message, $context);
286 1
    }
287
288
    /**
289
     * 记录一般信息
290
     * @access public
291
     * @param mixed $message 日志信息
292
     * @param array $context 替换内容
293
     * @return void
294
     */
295 2
    public function info($message, array $context = []): void
296
    {
297 2
        $this->log(__FUNCTION__, $message, $context);
298 2
    }
299
300
    /**
301
     * 记录调试信息
302
     * @access public
303
     * @param mixed $message 日志信息
304
     * @param array $context 替换内容
305
     * @return void
306
     */
307 1
    public function debug($message, array $context = []): void
308
    {
309 1
        $this->log(__FUNCTION__, $message, $context);
310 1
    }
311
312
    /**
313
     * 记录sql信息
314
     * @access public
315
     * @param mixed $message 日志信息
316
     * @param array $context 替换内容
317
     * @return void
318
     */
319 1
    public function sql($message, array $context = []): void
320
    {
321 1
        $this->log(__FUNCTION__, $message, $context);
322 1
    }
323
324 1
    public function __call($method, $parameters)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __call()
Loading history...
325
    {
326 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

326
        $this->log($method, /** @scrutinizer ignore-type */ ...$parameters);
Loading history...
327 1
    }
328
}
329