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

Channel::clear()   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 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace think\log;
4
5
use Psr\Log\LoggerInterface;
6
use think\contract\LogHandlerInterface;
7
use think\Event;
8
use think\event\LogWrite;
9
10
class Channel implements LoggerInterface
11
{
12
    protected $name;
13
    protected $logger;
14
    protected $event;
15
16
    protected $lazy = true;
17
    /**
18
     * 日志信息
19
     * @var array
20
     */
21
    protected $log = [];
22
23
    /**
24
     * 关闭日志
25
     * @var array
26
     */
27
    protected $close = false;
28
29
    /**
30
     * 允许写入类型
31
     * @var array
32
     */
33
    protected $allow = [];
34
35 3
    public function __construct($name, LogHandlerInterface $logger, $allow, $lazy = true, Event $event = null)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
36
    {
37 3
        $this->name   = $name;
38 3
        $this->logger = $logger;
39 3
        $this->allow  = $allow;
40 3
        $this->lazy   = $lazy;
41 3
        $this->event  = $event;
42 3
    }
43
44
    /**
45
     * 关闭通道
46
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
47 1
    public function close()
48
    {
49 1
        $this->clear();
50 1
        $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...
51 1
    }
52
53
    /**
54
     * 清空日志
55
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
56 2
    public function clear()
57
    {
58 2
        $this->log = [];
59 2
    }
60
61
    /**
62
     * 记录日志信息
63
     * @access public
64
     * @param mixed  $msg     日志信息
65
     * @param string $type    日志级别
66
     * @param array  $context 替换内容
67
     * @param bool   $lazy
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
68
     * @return $this
69
     */
70 2
    public function record($msg, string $type = 'info', array $context = [], $lazy = true)
71
    {
72 2
        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...
73 1
            return $this;
74
        }
75
76 2
        if (is_string($msg) && !empty($context)) {
77
            $replace = [];
78
            foreach ($context as $key => $val) {
79
                $replace['{' . $key . '}'] = $val;
80
            }
81
82
            $msg = strtr($msg, $replace);
83
        }
84
85 2
        $this->log[$type][] = $msg;
86
87 2
        if (!$this->lazy || !$lazy) {
88 1
            $this->save();
89
        }
90
91 2
        return $this;
92
    }
93
94
    /**
95
     * 实时写入日志信息
96
     * @access public
97
     * @param mixed  $msg     调试信息
98
     * @param string $type    日志级别
99
     * @param array  $context 替换内容
100
     * @return $this
101
     */
102
    public function write($msg, string $type = 'info', array $context = [])
103
    {
104
        return $this->record($msg, $type, $context, false);
105
    }
106
107
    /**
108
     * 获取日志信息
109
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
110 1
    public function getLog()
111
    {
112 1
        return $this->log;
113
    }
114
115
    /**
116
     * 保存日志
117
     * @return bool
118
     */
119 2
    public function save()
120
    {
121 2
        $log = $this->log;
122 2
        if ($this->event) {
123 2
            $event = new LogWrite($this->name, $log);
124 2
            $this->event->trigger($event);
125 2
            $log = $event->log;
126
        }
127
128 2
        if ($this->logger->save($log)) {
129 2
            $this->clear();
130 2
            return true;
131
        }
132
133
        return false;
134
    }
135
136
    /**
137
     * System is unusable.
138
     *
139
     * @param string $message
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
140
     * @param array  $context
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
141
     *
142
     * @return void
143
     */
144
    public function emergency($message, array $context = [])
145
    {
146
        $this->log(__FUNCTION__, $message, $context);
147
    }
148
149
    /**
150
     * Action must be taken immediately.
151
     *
152
     * Example: Entire website down, database unavailable, etc. This should
153
     * trigger the SMS alerts and wake you up.
154
     *
155
     * @param string $message
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
156
     * @param array  $context
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
157
     *
158
     * @return void
159
     */
160
    public function alert($message, array $context = [])
161
    {
162
        $this->log(__FUNCTION__, $message, $context);
163
    }
164
165
    /**
166
     * Critical conditions.
167
     *
168
     * Example: Application component unavailable, unexpected exception.
169
     *
170
     * @param string $message
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
171
     * @param array  $context
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
172
     *
173
     * @return void
174
     */
175
    public function critical($message, array $context = [])
176
    {
177
        $this->log(__FUNCTION__, $message, $context);
178
    }
179
180
    /**
181
     * Runtime errors that do not require immediate action but should typically
182
     * be logged and monitored.
183
     *
184
     * @param string $message
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
185
     * @param array  $context
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
186
     *
187
     * @return void
188
     */
189
    public function error($message, array $context = [])
190
    {
191
        $this->log(__FUNCTION__, $message, $context);
192
    }
193
194
    /**
195
     * Exceptional occurrences that are not errors.
196
     *
197
     * Example: Use of deprecated APIs, poor use of an API, undesirable things
198
     * that are not necessarily wrong.
199
     *
200
     * @param string $message
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
201
     * @param array  $context
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
202
     *
203
     * @return void
204
     */
205
    public function warning($message, array $context = [])
206
    {
207
        $this->log(__FUNCTION__, $message, $context);
208
    }
209
210
    /**
211
     * Normal but significant events.
212
     *
213
     * @param string $message
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
214
     * @param array  $context
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
215
     *
216
     * @return void
217
     */
218
    public function notice($message, array $context = [])
219
    {
220
        $this->log(__FUNCTION__, $message, $context);
221
    }
222
223
    /**
224
     * Interesting events.
225
     *
226
     * Example: User logs in, SQL logs.
227
     *
228
     * @param string $message
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
229
     * @param array  $context
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
230
     *
231
     * @return void
232
     */
233
    public function info($message, array $context = [])
234
    {
235
        $this->log(__FUNCTION__, $message, $context);
236
    }
237
238
    /**
239
     * Detailed debug information.
240
     *
241
     * @param string $message
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
242
     * @param array  $context
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
243
     *
244
     * @return void
245
     */
246
    public function debug($message, array $context = [])
247
    {
248
        $this->log(__FUNCTION__, $message, $context);
249
    }
250
251
    /**
252
     * Logs with an arbitrary level.
253
     *
254
     * @param mixed  $level
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
255
     * @param string $message
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
256
     * @param array  $context
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
257
     *
258
     * @return void
259
     */
260
    public function log($level, $message, array $context = [])
261
    {
262
        $this->record($message, $level, $context);
263
    }
264
265
    public function __call($method, $parameters)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __call()
Loading history...
266
    {
267
        $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

267
        $this->log($method, /** @scrutinizer ignore-type */ ...$parameters);
Loading history...
268
    }
269
}
270