Completed
Push — 6.0 ( 839503...24e465 )
by liu
09:08
created

Channel::record()   C

Complexity

Conditions 12
Paths 13

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 14.7316

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 12
eloc 14
c 3
b 0
f 0
nc 13
nop 4
dl 0
loc 27
ccs 11
cts 15
cp 0.7332
crap 14.7316
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2021 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: yunwuxin <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\log;
14
15
use Psr\Log\LoggerInterface;
16
use think\contract\LogHandlerInterface;
17
use think\Event;
18
use think\event\LogRecord;
19
use think\event\LogWrite;
20
21
class Channel implements LoggerInterface
22
{
23
    protected $name;
24
    protected $logger;
25
    protected $event;
26
27
    protected $lazy = true;
28
    /**
29
     * 日志信息
30
     * @var array
31
     */
32
    protected $log = [];
33
34
    /**
35
     * 关闭日志
36
     * @var array
37
     */
38
    protected $close = false;
39
40
    /**
41
     * 允许写入类型
42
     * @var array
43
     */
44
    protected $allow = [];
45
46 9
    public function __construct(string $name, LogHandlerInterface $logger, array $allow, bool $lazy = true, Event $event = null)
47
    {
48 9
        $this->name   = $name;
49 9
        $this->logger = $logger;
50 9
        $this->allow  = $allow;
51 9
        $this->lazy   = $lazy;
52 9
        $this->event  = $event;
53 9
    }
54
55
    /**
56
     * 关闭通道
57
     */
58 3
    public function close()
59
    {
60 3
        $this->clear();
61 3
        $this->close = true;
0 ignored issues
show
Documentation Bug introduced by
It seems like true of type true is incompatible with the declared type array of property $close.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
62 3
    }
63
64
    /**
65
     * 清空日志
66
     */
67 6
    public function clear()
68
    {
69 6
        $this->log = [];
70 6
    }
71
72
    /**
73
     * 记录日志信息
74
     * @access public
75
     * @param mixed  $msg     日志信息
76
     * @param string $type    日志级别
77
     * @param array  $context 替换内容
78
     * @param bool   $lazy
79
     * @return $this
80
     */
81 6
    public function record($msg, string $type = 'info', array $context = [], bool $lazy = true)
82
    {
83 6
        if ($this->close || (!empty($this->allow) && !in_array($type, $this->allow))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->close of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
84 3
            return $this;
85
        }
86
87 6
        if (is_string($msg) && !empty($context)) {
88
            $replace = [];
89
            foreach ($context as $key => $val) {
90
                $replace['{' . $key . '}'] = $val;
91
            }
92
93
            $msg = strtr($msg, $replace);
94
        }
95
96 6
        if (!empty($msg) || 0 === $msg) {
97 6
            $this->log[$type][] = $msg;
98 6
            if ($this->event) {
99 6
                $this->event->trigger(new LogRecord($type, $msg));
100
            }
101
        }
102
103 6
        if (!$this->lazy || !$lazy) {
104 3
            $this->save();
105
        }
106
107 6
        return $this;
108
    }
109
110
    /**
111
     * 实时写入日志信息
112
     * @access public
113
     * @param mixed  $msg     调试信息
114
     * @param string $type    日志级别
115
     * @param array  $context 替换内容
116
     * @return $this
117
     */
118
    public function write($msg, string $type = 'info', array $context = [])
119
    {
120
        return $this->record($msg, $type, $context, false);
121
    }
122
123
    /**
124
     * 获取日志信息
125
     * @return array
126
     */
127 3
    public function getLog(): array
128
    {
129 3
        return $this->log;
130
    }
131
132
    /**
133
     * 保存日志
134
     * @return bool
135
     */
136 6
    public function save(): bool
137
    {
138 6
        $log = $this->log;
139 6
        if ($this->event) {
140 6
            $event = new LogWrite($this->name, $log);
141 6
            $this->event->trigger($event);
142 6
            $log = $event->log;
143
        }
144
145 6
        if ($this->logger->save($log)) {
146 6
            $this->clear();
147 6
            return true;
148
        }
149
150
        return false;
151
    }
152
153
    /**
154
     * System is unusable.
155
     *
156
     * @param string $message
157
     * @param array  $context
158
     *
159
     * @return void
160
     */
161
    public function emergency($message, array $context = [])
162
    {
163
        $this->log(__FUNCTION__, $message, $context);
164
    }
165
166
    /**
167
     * Action must be taken immediately.
168
     *
169
     * Example: Entire website down, database unavailable, etc. This should
170
     * trigger the SMS alerts and wake you up.
171
     *
172
     * @param string $message
173
     * @param array  $context
174
     *
175
     * @return void
176
     */
177
    public function alert($message, array $context = [])
178
    {
179
        $this->log(__FUNCTION__, $message, $context);
180
    }
181
182
    /**
183
     * Critical conditions.
184
     *
185
     * Example: Application component unavailable, unexpected exception.
186
     *
187
     * @param string $message
188
     * @param array  $context
189
     *
190
     * @return void
191
     */
192
    public function critical($message, array $context = [])
193
    {
194
        $this->log(__FUNCTION__, $message, $context);
195
    }
196
197
    /**
198
     * Runtime errors that do not require immediate action but should typically
199
     * be logged and monitored.
200
     *
201
     * @param string $message
202
     * @param array  $context
203
     *
204
     * @return void
205
     */
206
    public function error($message, array $context = [])
207
    {
208
        $this->log(__FUNCTION__, $message, $context);
209
    }
210
211
    /**
212
     * Exceptional occurrences that are not errors.
213
     *
214
     * Example: Use of deprecated APIs, poor use of an API, undesirable things
215
     * that are not necessarily wrong.
216
     *
217
     * @param string $message
218
     * @param array  $context
219
     *
220
     * @return void
221
     */
222
    public function warning($message, array $context = [])
223
    {
224
        $this->log(__FUNCTION__, $message, $context);
225
    }
226
227
    /**
228
     * Normal but significant events.
229
     *
230
     * @param string $message
231
     * @param array  $context
232
     *
233
     * @return void
234
     */
235
    public function notice($message, array $context = [])
236
    {
237
        $this->log(__FUNCTION__, $message, $context);
238
    }
239
240
    /**
241
     * Interesting events.
242
     *
243
     * Example: User logs in, SQL logs.
244
     *
245
     * @param string $message
246
     * @param array  $context
247
     *
248
     * @return void
249
     */
250
    public function info($message, array $context = [])
251
    {
252
        $this->log(__FUNCTION__, $message, $context);
253
    }
254
255
    /**
256
     * Detailed debug information.
257
     *
258
     * @param string $message
259
     * @param array  $context
260
     *
261
     * @return void
262
     */
263
    public function debug($message, array $context = [])
264
    {
265
        $this->log(__FUNCTION__, $message, $context);
266
    }
267
268
    /**
269
     * Logs with an arbitrary level.
270
     *
271
     * @param mixed  $level
272
     * @param string $message
273
     * @param array  $context
274
     *
275
     * @return void
276
     */
277
    public function log($level, $message, array $context = [])
278
    {
279
        $this->record($message, $level, $context);
280
    }
281
282
    public function __call($method, $parameters)
283
    {
284
        $this->log($method, ...$parameters);
0 ignored issues
show
Bug introduced by
$parameters is expanded, but the parameter $message of think\log\Channel::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

284
        $this->log($method, /** @scrutinizer ignore-type */ ...$parameters);
Loading history...
285
    }
286
}
287