Completed
Push — 6.0 ( 669ae2...f0babb )
by liu
17:01
created

Channel::write()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

280
        $this->log($method, /** @scrutinizer ignore-type */ ...$parameters);
Loading history...
281
    }
282
}
283