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: 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 | 6 | public function getDefaultDriver() |
|||
46 | { |
||||
47 | 6 | 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 | 12 | public function getConfig(string $name = null, $default = null) |
|||
58 | { |
||||
59 | 12 | if (!is_null($name)) { |
|||
60 | 12 | return $this->app->config->get('log.' . $name, $default); |
|||
61 | } |
||||
62 | |||||
63 | 3 | return $this->app->config->get('log'); |
|||
64 | } |
||||
65 | |||||
66 | /** |
||||
67 | * 获取渠道配置 |
||||
68 | * @param string $channel |
||||
69 | * @param null $name |
||||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||||
70 | * @param null $default |
||||
0 ignored issues
–
show
|
|||||
71 | * @return array |
||||
72 | */ |
||||
73 | 12 | public function getChannelConfig($channel, $name = null, $default = null) |
|||
74 | { |
||||
75 | 12 | if ($config = $this->getConfig("channels.{$channel}")) { |
|||
76 | 9 | return Arr::get($config, $name, $default); |
|||
77 | } |
||||
78 | |||||
79 | 3 | throw new InvalidArgumentException("Channel [$channel] not found."); |
|||
80 | } |
||||
81 | |||||
82 | /** |
||||
83 | * driver()的别名 |
||||
84 | * @param string|array $name 渠道名 |
||||
85 | * @return Channel|ChannelSet |
||||
86 | */ |
||||
87 | 12 | public function channel($name = null) |
|||
88 | { |
||||
89 | 12 | if (is_array($name)) { |
|||
90 | 6 | return new ChannelSet($this, $name); |
|||
91 | } |
||||
92 | |||||
93 | 9 | return $this->driver($name); |
|||
94 | } |
||||
95 | |||||
96 | 9 | protected function resolveType(string $name) |
|||
97 | { |
||||
98 | 9 | return $this->getChannelConfig($name, 'type', 'file'); |
|||
99 | } |
||||
100 | |||||
101 | 9 | public function createDriver(string $name) |
|||
102 | { |
||||
103 | 9 | $driver = parent::createDriver($name); |
|||
104 | |||||
105 | 9 | $lazy = !$this->getChannelConfig($name, "realtime_write", false) && !$this->app->runningInConsole(); |
|||
106 | 9 | $allow = array_merge($this->getConfig("level", []), $this->getChannelConfig($name, "level", [])); |
|||
107 | |||||
108 | 9 | return new Channel($name, $driver, $allow, $lazy, $this->app->event); |
|||
109 | } |
||||
110 | |||||
111 | 9 | protected function resolveConfig(string $name) |
|||
112 | { |
||||
113 | 9 | return $this->getChannelConfig($name); |
|||
114 | } |
||||
115 | |||||
116 | /** |
||||
117 | * 清空日志信息 |
||||
118 | * @access public |
||||
119 | * @param string|array $channel 日志通道名 |
||||
120 | * @return $this |
||||
121 | */ |
||||
122 | 3 | public function clear($channel = '*') |
|||
123 | { |
||||
124 | 3 | if ('*' == $channel) { |
|||
125 | 3 | $channel = array_keys($this->drivers); |
|||
126 | } |
||||
127 | |||||
128 | 3 | $this->channel($channel)->clear(); |
|||
0 ignored issues
–
show
The method
clear() does not exist on think\log\ChannelSet . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
129 | |||||
130 | 3 | return $this; |
|||
131 | } |
||||
132 | |||||
133 | /** |
||||
134 | * 关闭本次请求日志写入 |
||||
135 | * @access public |
||||
136 | * @param string|array $channel 日志通道名 |
||||
137 | * @return $this |
||||
138 | */ |
||||
139 | 3 | public function close($channel = '*') |
|||
140 | { |
||||
141 | 3 | if ('*' == $channel) { |
|||
142 | 3 | $channel = array_keys($this->drivers); |
|||
143 | } |
||||
144 | |||||
145 | 3 | $this->channel($channel)->close(); |
|||
0 ignored issues
–
show
The method
close() does not exist on think\log\ChannelSet . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
146 | |||||
147 | 3 | return $this; |
|||
148 | } |
||||
149 | |||||
150 | /** |
||||
151 | * 获取日志信息 |
||||
152 | * @access public |
||||
153 | * @param string $channel 日志通道名 |
||||
154 | * @return array |
||||
155 | */ |
||||
156 | 3 | public function getLog(string $channel = null): array |
|||
157 | { |
||||
158 | 3 | return $this->channel($channel)->getLog(); |
|||
0 ignored issues
–
show
The method
getLog() does not exist on think\log\ChannelSet . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
159 | } |
||||
160 | |||||
161 | /** |
||||
162 | * 保存日志信息 |
||||
163 | * @access public |
||||
164 | * @return bool |
||||
165 | */ |
||||
166 | 3 | public function save(): bool |
|||
167 | { |
||||
168 | /** @var Channel $channel */ |
||||
169 | 3 | foreach ($this->drivers as $channel) { |
|||
170 | 3 | $channel->save(); |
|||
171 | } |
||||
172 | |||||
173 | 3 | 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 | 6 | public function record($msg, string $type = 'info', array $context = [], bool $lazy = true) |
|||
186 | { |
||||
187 | 6 | $channel = $this->getConfig('type_channel.' . $type); |
|||
188 | |||||
189 | 6 | $this->channel($channel)->record($msg, $type, $context, $lazy); |
|||
0 ignored issues
–
show
The method
record() does not exist on think\log\ChannelSet . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
190 | |||||
191 | 6 | 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 | 3 | public function write($msg, string $type = 'info', array $context = []) |
|||
203 | { |
||||
204 | 3 | 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 | 6 | public function log($level, $message, array $context = []): void |
|||
226 | { |
||||
227 | 6 | $this->record($message, $level, $context); |
|||
228 | 6 | } |
|||
229 | |||||
230 | /** |
||||
231 | * 记录emergency信息 |
||||
232 | * @access public |
||||
233 | * @param mixed $message 日志信息 |
||||
234 | * @param array $context 替换内容 |
||||
235 | * @return void |
||||
236 | */ |
||||
237 | 3 | public function emergency($message, array $context = []): void |
|||
238 | { |
||||
239 | 3 | $this->log(__FUNCTION__, $message, $context); |
|||
240 | 3 | } |
|||
241 | |||||
242 | /** |
||||
243 | * 记录警报信息 |
||||
244 | * @access public |
||||
245 | * @param mixed $message 日志信息 |
||||
246 | * @param array $context 替换内容 |
||||
247 | * @return void |
||||
248 | */ |
||||
249 | 3 | public function alert($message, array $context = []): void |
|||
250 | { |
||||
251 | 3 | $this->log(__FUNCTION__, $message, $context); |
|||
252 | 3 | } |
|||
253 | |||||
254 | /** |
||||
255 | * 记录紧急情况 |
||||
256 | * @access public |
||||
257 | * @param mixed $message 日志信息 |
||||
258 | * @param array $context 替换内容 |
||||
259 | * @return void |
||||
260 | */ |
||||
261 | 3 | public function critical($message, array $context = []): void |
|||
262 | { |
||||
263 | 3 | $this->log(__FUNCTION__, $message, $context); |
|||
264 | 3 | } |
|||
265 | |||||
266 | /** |
||||
267 | * 记录错误信息 |
||||
268 | * @access public |
||||
269 | * @param mixed $message 日志信息 |
||||
270 | * @param array $context 替换内容 |
||||
271 | * @return void |
||||
272 | */ |
||||
273 | 3 | public function error($message, array $context = []): void |
|||
274 | { |
||||
275 | 3 | $this->log(__FUNCTION__, $message, $context); |
|||
276 | 3 | } |
|||
277 | |||||
278 | /** |
||||
279 | * 记录warning信息 |
||||
280 | * @access public |
||||
281 | * @param mixed $message 日志信息 |
||||
282 | * @param array $context 替换内容 |
||||
283 | * @return void |
||||
284 | */ |
||||
285 | 3 | public function warning($message, array $context = []): void |
|||
286 | { |
||||
287 | 3 | $this->log(__FUNCTION__, $message, $context); |
|||
288 | 3 | } |
|||
289 | |||||
290 | /** |
||||
291 | * 记录notice信息 |
||||
292 | * @access public |
||||
293 | * @param mixed $message 日志信息 |
||||
294 | * @param array $context 替换内容 |
||||
295 | * @return void |
||||
296 | */ |
||||
297 | 3 | public function notice($message, array $context = []): void |
|||
298 | { |
||||
299 | 3 | $this->log(__FUNCTION__, $message, $context); |
|||
300 | 3 | } |
|||
301 | |||||
302 | /** |
||||
303 | * 记录一般信息 |
||||
304 | * @access public |
||||
305 | * @param mixed $message 日志信息 |
||||
306 | * @param array $context 替换内容 |
||||
307 | * @return void |
||||
308 | */ |
||||
309 | 6 | public function info($message, array $context = []): void |
|||
310 | { |
||||
311 | 6 | $this->log(__FUNCTION__, $message, $context); |
|||
312 | 6 | } |
|||
313 | |||||
314 | /** |
||||
315 | * 记录调试信息 |
||||
316 | * @access public |
||||
317 | * @param mixed $message 日志信息 |
||||
318 | * @param array $context 替换内容 |
||||
319 | * @return void |
||||
320 | */ |
||||
321 | 3 | public function debug($message, array $context = []): void |
|||
322 | { |
||||
323 | 3 | $this->log(__FUNCTION__, $message, $context); |
|||
324 | 3 | } |
|||
325 | |||||
326 | /** |
||||
327 | * 记录sql信息 |
||||
328 | * @access public |
||||
329 | * @param mixed $message 日志信息 |
||||
330 | * @param array $context 替换内容 |
||||
331 | * @return void |
||||
332 | */ |
||||
333 | 3 | public function sql($message, array $context = []): void |
|||
334 | { |
||||
335 | 3 | $this->log(__FUNCTION__, $message, $context); |
|||
336 | 3 | } |
|||
337 | |||||
338 | 3 | public function __call($method, $parameters) |
|||
339 | { |
||||
340 | 3 | $this->log($method, ...$parameters); |
|||
0 ignored issues
–
show
$parameters is expanded, but the parameter $message of think\Log::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
![]() |
|||||
341 | 3 | } |
|||
342 | } |
||||
343 |