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 Psr\Log\LoggerInterface; |
16
|
|
|
|
17
|
|
|
class Log implements LoggerInterface |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
const EMERGENCY = 'emergency'; |
20
|
|
|
const ALERT = 'alert'; |
21
|
|
|
const CRITICAL = 'critical'; |
22
|
|
|
const ERROR = 'error'; |
23
|
|
|
const WARNING = 'warning'; |
24
|
|
|
const NOTICE = 'notice'; |
25
|
|
|
const INFO = 'info'; |
26
|
|
|
const DEBUG = 'debug'; |
27
|
|
|
const SQL = 'sql'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* 应用对象 |
31
|
|
|
* @var App |
32
|
|
|
*/ |
33
|
|
|
protected $app; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* 日志信息 |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $log = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* 配置参数 |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $config = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* 日志写入驱动 |
49
|
|
|
* @var object |
50
|
|
|
*/ |
51
|
|
|
protected $driver; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* 日志授权key |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $key; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* 是否允许日志写入 |
61
|
|
|
* @var bool |
62
|
|
|
*/ |
63
|
|
|
protected $allowWrite = true; |
64
|
|
|
|
65
|
|
|
/** |
|
|
|
|
66
|
|
|
* 构造方法 |
67
|
|
|
* @access public |
68
|
|
|
*/ |
69
|
|
|
public function __construct(App $app) |
70
|
|
|
{ |
71
|
|
|
$this->app = $app; |
72
|
|
|
$this->init($app->config->get('log')); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* 日志初始化 |
77
|
|
|
* @access public |
78
|
|
|
* @param array $config |
|
|
|
|
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
public function init(array $config = []) |
82
|
|
|
{ |
83
|
|
|
$type = $config['type'] ?? 'File'; |
84
|
|
|
|
85
|
|
|
$this->config = $config; |
86
|
|
|
|
87
|
|
|
unset($config['type']); |
88
|
|
|
if (!empty($config['close'])) { |
89
|
|
|
$this->allowWrite = false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->driver = App::factory($type, '\\think\\log\\driver\\', $config); |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* 获取日志信息 |
99
|
|
|
* @access public |
100
|
|
|
* @param string $type 信息类型 |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
public function getLog(string $type = ''): array |
104
|
|
|
{ |
105
|
|
|
return $type ? $this->log[$type] : $this->log; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* 记录日志信息 |
110
|
|
|
* @access public |
111
|
|
|
* @param mixed $msg 日志信息 |
|
|
|
|
112
|
|
|
* @param string $type 日志级别 |
|
|
|
|
113
|
|
|
* @param array $context 替换内容 |
|
|
|
|
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
|
|
public function record($msg, string $type = 'info', array $context = []) |
117
|
|
|
{ |
118
|
|
|
if (!$this->allowWrite) { |
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (is_string($msg) && !empty($context)) { |
123
|
|
|
$replace = []; |
124
|
|
|
foreach ($context as $key => $val) { |
125
|
|
|
$replace['{' . $key . '}'] = $val; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$msg = strtr($msg, $replace); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (PHP_SAPI == 'cli') { |
132
|
|
|
if (empty($this->config['level']) || in_array($type, $this->config['level'])) { |
133
|
|
|
// 命令行日志实时写入 |
134
|
|
|
$this->write($msg, $type, true); |
135
|
|
|
} |
136
|
|
|
} else { |
137
|
|
|
$this->log[$type][] = $msg; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
|
|
|
|
144
|
|
|
* 记录批量日志信息 |
145
|
|
|
* @access public |
146
|
|
|
* @param array $msg 日志信息 |
|
|
|
|
147
|
|
|
* @param string $type 日志级别 |
|
|
|
|
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
|
|
public function append(array $log, string $type = 'info') |
151
|
|
|
{ |
152
|
|
|
if (!$this->allowWrite || empty($log)) { |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (isset($this->log[$type])) { |
157
|
|
|
$this->log[$type] += $log; |
158
|
|
|
} else { |
159
|
|
|
$this->log[$type] = $log; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* 清空日志信息 |
167
|
|
|
* @access public |
168
|
|
|
* @return $this |
169
|
|
|
*/ |
170
|
|
|
public function clear() |
171
|
|
|
{ |
172
|
|
|
$this->log = []; |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* 当前日志记录的授权key |
179
|
|
|
* @access public |
180
|
|
|
* @param string $key 授权key |
|
|
|
|
181
|
|
|
* @return $this |
182
|
|
|
*/ |
183
|
|
|
public function key(string $key) |
184
|
|
|
{ |
185
|
|
|
$this->key = $key; |
186
|
|
|
|
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* 检查日志写入权限 |
192
|
|
|
* @access public |
193
|
|
|
* @param array $config 当前日志配置参数 |
|
|
|
|
194
|
|
|
* @return bool |
195
|
|
|
*/ |
196
|
|
|
public function check(array $config): bool |
197
|
|
|
{ |
198
|
|
|
if ($this->key && !empty($config['allow_key']) && !in_array($this->key, $config['allow_key'])) { |
199
|
|
|
return false; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return true; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* 关闭本次请求日志写入 |
207
|
|
|
* @access public |
208
|
|
|
* @return $this |
209
|
|
|
*/ |
210
|
|
|
public function close() |
211
|
|
|
{ |
212
|
|
|
$this->allowWrite = false; |
213
|
|
|
$this->log = []; |
214
|
|
|
|
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* 保存调试信息 |
220
|
|
|
* @access public |
221
|
|
|
* @return bool |
222
|
|
|
*/ |
223
|
|
|
public function save(): bool |
224
|
|
|
{ |
225
|
|
|
if (empty($this->log) || !$this->allowWrite) { |
226
|
|
|
return true; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
if (!$this->check($this->config)) { |
230
|
|
|
// 检测日志写入权限 |
231
|
|
|
return false; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$log = []; |
235
|
|
|
|
236
|
|
|
foreach ($this->log as $level => $info) { |
237
|
|
|
if (!App::isDebug() && 'debug' == $level) { |
238
|
|
|
continue; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
if (empty($this->config['level']) || in_array($level, $this->config['level'])) { |
242
|
|
|
$log[$level] = $info; |
243
|
|
|
$this->app->event->trigger('LogLevel', [$level, $info]); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$result = $this->driver->save($log); |
248
|
|
|
|
249
|
|
|
if ($result) { |
250
|
|
|
$this->log = []; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $result; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* 实时写入日志信息 并支持行为 |
258
|
|
|
* @access public |
259
|
|
|
* @param mixed $msg 调试信息 |
260
|
|
|
* @param string $type 日志级别 |
261
|
|
|
* @param bool $force 是否强制写入 |
262
|
|
|
* @return bool |
263
|
|
|
*/ |
264
|
|
|
public function write($msg, string $type = 'info', bool $force = false): bool |
265
|
|
|
{ |
266
|
|
|
// 封装日志信息 |
267
|
|
|
if (empty($this->config['level'])) { |
268
|
|
|
$force = true; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$log = []; |
272
|
|
|
|
273
|
|
|
if (true === $force || in_array($type, $this->config['level'])) { |
274
|
|
|
$log[$type][] = $msg; |
275
|
|
|
} else { |
276
|
|
|
return false; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
// 监听LogWrite |
280
|
|
|
$this->app->event->trigger('LogWrite', $log); |
281
|
|
|
|
282
|
|
|
// 写入日志 |
283
|
|
|
return $this->driver->save($log, false); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* 记录日志信息 |
288
|
|
|
* @access public |
289
|
|
|
* @param string $level 日志级别 |
|
|
|
|
290
|
|
|
* @param mixed $message 日志信息 |
|
|
|
|
291
|
|
|
* @param array $context 替换内容 |
|
|
|
|
292
|
|
|
* @return void |
293
|
|
|
*/ |
294
|
|
|
public function log($level, $message, array $context = []): void |
295
|
|
|
{ |
296
|
|
|
$this->record($message, $level, $context); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* 记录emergency信息 |
301
|
|
|
* @access public |
302
|
|
|
* @param mixed $message 日志信息 |
|
|
|
|
303
|
|
|
* @param array $context 替换内容 |
|
|
|
|
304
|
|
|
* @return void |
305
|
|
|
*/ |
306
|
|
|
public function emergency($message, array $context = []): void |
307
|
|
|
{ |
308
|
|
|
$this->log(__FUNCTION__, $message, $context); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* 记录警报信息 |
313
|
|
|
* @access public |
314
|
|
|
* @param mixed $message 日志信息 |
|
|
|
|
315
|
|
|
* @param array $context 替换内容 |
|
|
|
|
316
|
|
|
* @return void |
317
|
|
|
*/ |
318
|
|
|
public function alert($message, array $context = []): void |
319
|
|
|
{ |
320
|
|
|
$this->log(__FUNCTION__, $message, $context); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* 记录紧急情况 |
325
|
|
|
* @access public |
326
|
|
|
* @param mixed $message 日志信息 |
|
|
|
|
327
|
|
|
* @param array $context 替换内容 |
|
|
|
|
328
|
|
|
* @return void |
329
|
|
|
*/ |
330
|
|
|
public function critical($message, array $context = []): void |
331
|
|
|
{ |
332
|
|
|
$this->log(__FUNCTION__, $message, $context); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* 记录错误信息 |
337
|
|
|
* @access public |
338
|
|
|
* @param mixed $message 日志信息 |
|
|
|
|
339
|
|
|
* @param array $context 替换内容 |
|
|
|
|
340
|
|
|
* @return void |
341
|
|
|
*/ |
342
|
|
|
public function error($message, array $context = []): void |
343
|
|
|
{ |
344
|
|
|
$this->log(__FUNCTION__, $message, $context); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* 记录warning信息 |
349
|
|
|
* @access public |
350
|
|
|
* @param mixed $message 日志信息 |
|
|
|
|
351
|
|
|
* @param array $context 替换内容 |
|
|
|
|
352
|
|
|
* @return void |
353
|
|
|
*/ |
354
|
|
|
public function warning($message, array $context = []): void |
355
|
|
|
{ |
356
|
|
|
$this->log(__FUNCTION__, $message, $context); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* 记录notice信息 |
361
|
|
|
* @access public |
362
|
|
|
* @param mixed $message 日志信息 |
|
|
|
|
363
|
|
|
* @param array $context 替换内容 |
|
|
|
|
364
|
|
|
* @return void |
365
|
|
|
*/ |
366
|
|
|
public function notice($message, array $context = []): void |
367
|
|
|
{ |
368
|
|
|
$this->log(__FUNCTION__, $message, $context); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* 记录一般信息 |
373
|
|
|
* @access public |
374
|
|
|
* @param mixed $message 日志信息 |
|
|
|
|
375
|
|
|
* @param array $context 替换内容 |
|
|
|
|
376
|
|
|
* @return void |
377
|
|
|
*/ |
378
|
|
|
public function info($message, array $context = []): void |
379
|
|
|
{ |
380
|
|
|
$this->log(__FUNCTION__, $message, $context); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* 记录调试信息 |
385
|
|
|
* @access public |
386
|
|
|
* @param mixed $message 日志信息 |
|
|
|
|
387
|
|
|
* @param array $context 替换内容 |
|
|
|
|
388
|
|
|
* @return void |
389
|
|
|
*/ |
390
|
|
|
public function debug($message, array $context = []): void |
391
|
|
|
{ |
392
|
|
|
$this->log(__FUNCTION__, $message, $context); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* 记录sql信息 |
397
|
|
|
* @access public |
398
|
|
|
* @param mixed $message 日志信息 |
|
|
|
|
399
|
|
|
* @param array $context 替换内容 |
|
|
|
|
400
|
|
|
* @return void |
401
|
|
|
*/ |
402
|
|
|
public function sql($message, array $context = []): void |
403
|
|
|
{ |
404
|
|
|
$this->log(__FUNCTION__, $message, $context); |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
|