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
|
9 |
|
|
46
|
|
|
public function __construct(string $name, LogHandlerInterface $logger, array $allow, bool $lazy = true, Event $event = null) |
47
|
9 |
|
{ |
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
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* 关闭通道 |
57
|
3 |
|
*/ |
58
|
|
|
public function close() |
59
|
3 |
|
{ |
60
|
3 |
|
$this->clear(); |
61
|
3 |
|
$this->close = true; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* 清空日志 |
66
|
6 |
|
*/ |
67
|
|
|
public function clear() |
68
|
6 |
|
{ |
69
|
6 |
|
$this->log = []; |
70
|
|
|
} |
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
|
6 |
|
*/ |
81
|
|
|
public function record($msg, string $type = 'info', array $context = [], bool $lazy = true) |
82
|
6 |
|
{ |
83
|
3 |
|
if ($this->close || (!empty($this->allow) && !in_array($type, $this->allow))) { |
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
6 |
|
|
87
|
|
|
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
|
6 |
|
|
96
|
6 |
|
if (!empty($msg) || 0 === $msg) { |
97
|
|
|
$this->log[$type][] = $msg; |
98
|
|
|
if ($this->event) { |
99
|
6 |
|
$this->event->trigger(new LogRecord($type, $msg)); |
100
|
3 |
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
6 |
|
if (!$this->lazy || !$lazy) { |
104
|
|
|
$this->save(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
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
|
3 |
|
/** |
124
|
|
|
* 获取日志信息 |
125
|
3 |
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public function getLog(): array |
128
|
|
|
{ |
129
|
|
|
return $this->log; |
130
|
|
|
} |
131
|
|
|
|
132
|
6 |
|
/** |
133
|
|
|
* 保存日志 |
134
|
6 |
|
* @return bool |
135
|
6 |
|
*/ |
136
|
6 |
|
public function save(): bool |
137
|
6 |
|
{ |
138
|
6 |
|
$log = $this->log; |
139
|
|
|
if ($this->event) { |
140
|
|
|
$event = new LogWrite($this->name, $log); |
141
|
6 |
|
$this->event->trigger($event); |
142
|
6 |
|
$log = $event->log; |
143
|
6 |
|
} |
144
|
|
|
|
145
|
|
|
if ($this->logger->save($log)) { |
146
|
|
|
$this->clear(); |
147
|
|
|
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); |
|
|
|
|
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
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..