|
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\event\LogWrite; |
|
18
|
|
|
use think\helper\Arr; |
|
19
|
|
|
use think\log\Channel; |
|
20
|
|
|
use think\log\ChannelSet; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* 日志管理类 |
|
24
|
|
|
* @package think |
|
|
|
|
|
|
25
|
|
|
* @mixin Channel |
|
26
|
|
|
*/ |
|
27
|
|
|
class Log extends Manager implements LoggerInterface |
|
28
|
|
|
{ |
|
29
|
|
|
const EMERGENCY = 'emergency'; |
|
30
|
|
|
const ALERT = 'alert'; |
|
31
|
|
|
const CRITICAL = 'critical'; |
|
32
|
|
|
const ERROR = 'error'; |
|
33
|
|
|
const WARNING = 'warning'; |
|
34
|
|
|
const NOTICE = 'notice'; |
|
35
|
|
|
const INFO = 'info'; |
|
36
|
|
|
const DEBUG = 'debug'; |
|
37
|
|
|
const SQL = 'sql'; |
|
38
|
|
|
|
|
39
|
|
|
protected $namespace = '\\think\\log\\driver\\'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* 默认驱动 |
|
43
|
|
|
* @return string|null |
|
44
|
|
|
*/ |
|
45
|
2 |
|
public function getDefaultDriver() |
|
46
|
|
|
{ |
|
47
|
2 |
|
return $this->getConfig('default'); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* 获取日志配置 |
|
52
|
|
|
* @access public |
|
53
|
|
|
* @param null|string $name 名称 |
|
54
|
|
|
* @param mixed $default 默认值 |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
*/ |
|
57
|
4 |
|
public function getConfig(string $name = null, $default = null) |
|
58
|
|
|
{ |
|
59
|
4 |
|
if (!is_null($name)) { |
|
60
|
4 |
|
return $this->app->config->get('log.' . $name, $default); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
return $this->app->config->get('log'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* 获取渠道配置 |
|
68
|
|
|
* @param string $channel |
|
|
|
|
|
|
69
|
|
|
* @param null $name |
|
|
|
|
|
|
70
|
|
|
* @param null $default |
|
|
|
|
|
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
4 |
|
public function getChannelConfig($channel, $name = null, $default = null) |
|
74
|
|
|
{ |
|
75
|
4 |
|
if ($config = $this->getConfig("channels.{$channel}")) { |
|
76
|
3 |
|
return Arr::get($config, $name, $default); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
throw new InvalidArgumentException("Channel [$channel] not found."); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* driver()的别名 |
|
|
|
|
|
|
84
|
|
|
* @param string|array $name 渠道名 |
|
85
|
|
|
* @return Channel|ChannelSet |
|
86
|
|
|
*/ |
|
87
|
4 |
|
public function channel($name = null) |
|
88
|
|
|
{ |
|
89
|
4 |
|
if (is_array($name)) { |
|
90
|
2 |
|
return new ChannelSet($this, $name); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
3 |
|
return $this->driver($name); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
3 |
|
protected function resolveType(string $name) |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
3 |
|
return $this->getChannelConfig($name, 'type', 'file'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
3 |
|
public function createDriver(string $name) |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
3 |
|
$driver = parent::createDriver($name); |
|
104
|
|
|
|
|
105
|
3 |
|
$lazy = !$this->getChannelConfig($name, "realtime_write", false) && !$this->app->runningInConsole(); |
|
106
|
3 |
|
$allow = array_merge($this->getConfig("level", []), $this->getChannelConfig($name, "level", [])); |
|
107
|
|
|
|
|
108
|
3 |
|
return new Channel($name, $driver, $allow, $lazy, $this->app->event); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
3 |
|
protected function resolveConfig(string $name) |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
3 |
|
return $this->getChannelConfig($name); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* 清空日志信息 |
|
118
|
|
|
* @access public |
|
119
|
|
|
* @param string|array $channel 日志通道名 |
|
120
|
|
|
* @return $this |
|
121
|
|
|
*/ |
|
122
|
1 |
|
public function clear($channel = '*') |
|
123
|
|
|
{ |
|
124
|
1 |
|
if ('*' == $channel) { |
|
125
|
1 |
|
$channel = array_keys($this->drivers); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
1 |
|
$this->channel($channel)->clear(); |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
1 |
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* 关闭本次请求日志写入 |
|
135
|
|
|
* @access public |
|
136
|
|
|
* @param string|array $channel 日志通道名 |
|
137
|
|
|
* @return $this |
|
138
|
|
|
*/ |
|
139
|
1 |
|
public function close($channel = '*') |
|
140
|
|
|
{ |
|
141
|
1 |
|
if ('*' == $channel) { |
|
142
|
1 |
|
$channel = array_keys($this->drivers); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
$this->channel($channel)->close(); |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
1 |
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* 获取日志信息 |
|
152
|
|
|
* @access public |
|
153
|
|
|
* @param string $channel 日志通道名 |
|
154
|
|
|
* @return array |
|
155
|
|
|
*/ |
|
156
|
1 |
|
public function getLog(string $channel = null): array |
|
157
|
|
|
{ |
|
158
|
1 |
|
return $this->channel($channel)->getLog(); |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* 保存日志信息 |
|
163
|
|
|
* @access public |
|
164
|
|
|
* @return bool |
|
165
|
|
|
*/ |
|
166
|
1 |
|
public function save(): bool |
|
167
|
|
|
{ |
|
168
|
|
|
/** @var Channel $channel */ |
|
|
|
|
|
|
169
|
1 |
|
foreach ($this->drivers as $channel) { |
|
170
|
1 |
|
$channel->save(); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
1 |
|
return true; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* 记录日志信息 |
|
178
|
|
|
* @access public |
|
179
|
|
|
* @param mixed $msg 日志信息 |
|
180
|
|
|
* @param string $type 日志级别 |
|
181
|
|
|
* @param array $context 替换内容 |
|
182
|
|
|
* @param bool $lazy |
|
|
|
|
|
|
183
|
|
|
* @return $this |
|
184
|
|
|
*/ |
|
185
|
2 |
|
public function record($msg, string $type = 'info', array $context = [], bool $lazy = true) |
|
186
|
|
|
{ |
|
187
|
2 |
|
$channel = $this->getConfig('type_channel.' . $type); |
|
188
|
|
|
|
|
189
|
2 |
|
$this->channel($channel)->record($msg, $type, $context, $lazy); |
|
|
|
|
|
|
190
|
|
|
|
|
191
|
2 |
|
return $this; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* 实时写入日志信息 |
|
196
|
|
|
* @access public |
|
197
|
|
|
* @param mixed $msg 调试信息 |
|
198
|
|
|
* @param string $type 日志级别 |
|
199
|
|
|
* @param array $context 替换内容 |
|
200
|
|
|
* @return $this |
|
201
|
|
|
*/ |
|
202
|
1 |
|
public function write($msg, string $type = 'info', array $context = []) |
|
203
|
|
|
{ |
|
204
|
1 |
|
return $this->record($msg, $type, $context, false); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
|
|
|
|
|
208
|
|
|
* 注册日志写入事件监听 |
|
209
|
|
|
* @param $listener |
|
|
|
|
|
|
210
|
|
|
* @return Event |
|
211
|
|
|
*/ |
|
212
|
|
|
public function listen($listener) |
|
213
|
|
|
{ |
|
214
|
|
|
return $this->app->event->listen(LogWrite::class, $listener); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* 记录日志信息 |
|
219
|
|
|
* @access public |
|
220
|
|
|
* @param string $level 日志级别 |
|
221
|
|
|
* @param mixed $message 日志信息 |
|
222
|
|
|
* @param array $context 替换内容 |
|
223
|
|
|
* @return void |
|
224
|
|
|
*/ |
|
225
|
2 |
|
public function log($level, $message, array $context = []): void |
|
226
|
|
|
{ |
|
227
|
2 |
|
$this->record($message, $level, $context); |
|
228
|
2 |
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* 记录emergency信息 |
|
232
|
|
|
* @access public |
|
233
|
|
|
* @param mixed $message 日志信息 |
|
234
|
|
|
* @param array $context 替换内容 |
|
235
|
|
|
* @return void |
|
236
|
|
|
*/ |
|
237
|
1 |
|
public function emergency($message, array $context = []): void |
|
238
|
|
|
{ |
|
239
|
1 |
|
$this->log(__FUNCTION__, $message, $context); |
|
240
|
1 |
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* 记录警报信息 |
|
244
|
|
|
* @access public |
|
245
|
|
|
* @param mixed $message 日志信息 |
|
246
|
|
|
* @param array $context 替换内容 |
|
247
|
|
|
* @return void |
|
248
|
|
|
*/ |
|
249
|
1 |
|
public function alert($message, array $context = []): void |
|
250
|
|
|
{ |
|
251
|
1 |
|
$this->log(__FUNCTION__, $message, $context); |
|
252
|
1 |
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* 记录紧急情况 |
|
256
|
|
|
* @access public |
|
257
|
|
|
* @param mixed $message 日志信息 |
|
258
|
|
|
* @param array $context 替换内容 |
|
259
|
|
|
* @return void |
|
260
|
|
|
*/ |
|
261
|
1 |
|
public function critical($message, array $context = []): void |
|
262
|
|
|
{ |
|
263
|
1 |
|
$this->log(__FUNCTION__, $message, $context); |
|
264
|
1 |
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* 记录错误信息 |
|
268
|
|
|
* @access public |
|
269
|
|
|
* @param mixed $message 日志信息 |
|
270
|
|
|
* @param array $context 替换内容 |
|
271
|
|
|
* @return void |
|
272
|
|
|
*/ |
|
273
|
1 |
|
public function error($message, array $context = []): void |
|
274
|
|
|
{ |
|
275
|
1 |
|
$this->log(__FUNCTION__, $message, $context); |
|
276
|
1 |
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* 记录warning信息 |
|
280
|
|
|
* @access public |
|
281
|
|
|
* @param mixed $message 日志信息 |
|
282
|
|
|
* @param array $context 替换内容 |
|
283
|
|
|
* @return void |
|
284
|
|
|
*/ |
|
285
|
1 |
|
public function warning($message, array $context = []): void |
|
286
|
|
|
{ |
|
287
|
1 |
|
$this->log(__FUNCTION__, $message, $context); |
|
288
|
1 |
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* 记录notice信息 |
|
292
|
|
|
* @access public |
|
293
|
|
|
* @param mixed $message 日志信息 |
|
294
|
|
|
* @param array $context 替换内容 |
|
295
|
|
|
* @return void |
|
296
|
|
|
*/ |
|
297
|
1 |
|
public function notice($message, array $context = []): void |
|
298
|
|
|
{ |
|
299
|
1 |
|
$this->log(__FUNCTION__, $message, $context); |
|
300
|
1 |
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* 记录一般信息 |
|
304
|
|
|
* @access public |
|
305
|
|
|
* @param mixed $message 日志信息 |
|
306
|
|
|
* @param array $context 替换内容 |
|
307
|
|
|
* @return void |
|
308
|
|
|
*/ |
|
309
|
2 |
|
public function info($message, array $context = []): void |
|
310
|
|
|
{ |
|
311
|
2 |
|
$this->log(__FUNCTION__, $message, $context); |
|
312
|
2 |
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* 记录调试信息 |
|
316
|
|
|
* @access public |
|
317
|
|
|
* @param mixed $message 日志信息 |
|
318
|
|
|
* @param array $context 替换内容 |
|
319
|
|
|
* @return void |
|
320
|
|
|
*/ |
|
321
|
1 |
|
public function debug($message, array $context = []): void |
|
322
|
|
|
{ |
|
323
|
1 |
|
$this->log(__FUNCTION__, $message, $context); |
|
324
|
1 |
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* 记录sql信息 |
|
328
|
|
|
* @access public |
|
329
|
|
|
* @param mixed $message 日志信息 |
|
330
|
|
|
* @param array $context 替换内容 |
|
331
|
|
|
* @return void |
|
332
|
|
|
*/ |
|
333
|
1 |
|
public function sql($message, array $context = []): void |
|
334
|
|
|
{ |
|
335
|
1 |
|
$this->log(__FUNCTION__, $message, $context); |
|
336
|
1 |
|
} |
|
337
|
|
|
|
|
338
|
1 |
|
public function __call($method, $parameters) |
|
|
|
|
|
|
339
|
|
|
{ |
|
340
|
1 |
|
$this->log($method, ...$parameters); |
|
|
|
|
|
|
341
|
1 |
|
} |
|
342
|
|
|
} |
|
343
|
|
|
|