1
|
|
|
<?php |
2
|
|
|
// +---------------------------------------------------------------------- |
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
4
|
|
|
// +---------------------------------------------------------------------- |
5
|
|
|
// | Copyright (c) 2006~2023 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 Psr\Log\LoggerTrait; |
17
|
|
|
use Stringable; |
18
|
|
|
use think\contract\LogHandlerInterface; |
19
|
|
|
use think\Event; |
20
|
|
|
use think\event\LogRecord; |
21
|
|
|
use think\event\LogWrite; |
22
|
|
|
|
23
|
|
|
class Channel implements LoggerInterface |
24
|
|
|
{ |
25
|
|
|
use LoggerTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* 事件 |
29
|
|
|
* @var Event |
30
|
|
|
*/ |
31
|
|
|
protected $event; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* 日志信息 |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $log = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* 关闭日志 |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $close = false; |
44
|
|
|
|
45
|
9 |
|
public function __construct(protected string $name, protected LogHandlerInterface $logger, protected array $allow = [], protected bool $lazy = true, Event $event = null) |
46
|
|
|
{ |
47
|
9 |
|
if($event) { |
48
|
9 |
|
$this->event = $event; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* 关闭通道 |
54
|
|
|
*/ |
55
|
3 |
|
public function close(): void |
56
|
|
|
{ |
57
|
3 |
|
$this->clear(); |
58
|
3 |
|
$this->close = true; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* 清空日志 |
63
|
|
|
*/ |
64
|
6 |
|
public function clear(): void |
65
|
|
|
{ |
66
|
6 |
|
$this->log = []; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* 记录日志信息 |
71
|
|
|
* @access public |
72
|
|
|
* @param mixed $msg 日志信息 |
73
|
|
|
* @param string $type 日志级别 |
74
|
|
|
* @param array $context 替换内容 |
75
|
|
|
* @param bool $lazy |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
6 |
|
public function record($msg, string $type = 'info', array $context = [], bool $lazy = true) |
79
|
|
|
{ |
80
|
6 |
|
if ($this->close || (!empty($this->allow) && !in_array($type, $this->allow))) { |
|
|
|
|
81
|
3 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
6 |
|
if ($msg instanceof Stringable) { |
85
|
|
|
$msg = $msg->__toString(); |
86
|
|
|
} |
87
|
|
|
|
88
|
6 |
|
if (is_string($msg) && !empty($context)) { |
89
|
|
|
$replace = []; |
90
|
|
|
foreach ($context as $key => $val) { |
91
|
|
|
$replace['{' . $key . '}'] = $val; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$msg = strtr($msg, $replace); |
95
|
|
|
} |
96
|
|
|
|
97
|
6 |
|
if (!empty($msg) || 0 === $msg) { |
98
|
6 |
|
$this->log[$type][] = $msg; |
99
|
6 |
|
if ($this->event) { |
100
|
6 |
|
$this->event->trigger(new LogRecord($type, $msg)); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
6 |
|
if (!$this->lazy || !$lazy) { |
105
|
3 |
|
$this->save(); |
106
|
|
|
} |
107
|
|
|
|
108
|
6 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* 实时写入日志信息 |
113
|
|
|
* @access public |
114
|
|
|
* @param mixed $msg 调试信息 |
115
|
|
|
* @param string $type 日志级别 |
116
|
|
|
* @param array $context 替换内容 |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function write($msg, string $type = 'info', array $context = []) |
120
|
|
|
{ |
121
|
|
|
return $this->record($msg, $type, $context, false); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* 获取日志信息 |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
3 |
|
public function getLog(): array |
129
|
|
|
{ |
130
|
3 |
|
return $this->log; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* 保存日志 |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
6 |
|
public function save(): bool |
138
|
|
|
{ |
139
|
6 |
|
$log = $this->log; |
140
|
6 |
|
if ($this->event) { |
141
|
6 |
|
$event = new LogWrite($this->name, $log); |
142
|
6 |
|
$this->event->trigger($event); |
143
|
6 |
|
$log = $event->log; |
144
|
|
|
} |
145
|
|
|
|
146
|
6 |
|
if ($this->logger->save($log)) { |
147
|
6 |
|
$this->clear(); |
148
|
6 |
|
return true; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Logs with an arbitrary level. |
156
|
|
|
* |
157
|
|
|
* @param mixed $level |
158
|
|
|
* @param string|Stringable $message |
159
|
|
|
* @param array $context |
160
|
|
|
* |
161
|
|
|
* @return void |
162
|
|
|
*/ |
163
|
|
|
public function log($level, string|Stringable $message, array $context = []): void |
164
|
|
|
{ |
165
|
|
|
$this->record($message, $level, $context); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function __call($method, $parameters) |
169
|
|
|
{ |
170
|
|
|
$this->log($method, ...$parameters); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
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..