Total Complexity | 48 |
Total Lines | 377 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Log often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Log, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Log implements LoggerInterface |
||
1 ignored issue
–
show
|
|||
20 | { |
||
21 | const EMERGENCY = 'emergency'; |
||
22 | const ALERT = 'alert'; |
||
23 | const CRITICAL = 'critical'; |
||
24 | const ERROR = 'error'; |
||
25 | const WARNING = 'warning'; |
||
26 | const NOTICE = 'notice'; |
||
27 | const INFO = 'info'; |
||
28 | const DEBUG = 'debug'; |
||
29 | const SQL = 'sql'; |
||
30 | |||
31 | /** |
||
32 | * 日志信息 |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $log = []; |
||
36 | |||
37 | /** |
||
38 | * 配置参数 |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $config = []; |
||
42 | |||
43 | /** |
||
44 | * 日志写入驱动 |
||
45 | * @var object |
||
46 | */ |
||
47 | protected $driver; |
||
48 | |||
49 | /** |
||
50 | * 日志授权key |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $key; |
||
54 | |||
55 | /** |
||
56 | * 是否允许日志写入 |
||
57 | * @var bool |
||
58 | */ |
||
59 | protected $allowWrite = true; |
||
60 | |||
61 | public static function __make(Config $config) |
||
1 ignored issue
–
show
|
|||
62 | { |
||
63 | return (new static())->init($config->get('log')); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * 日志初始化 |
||
68 | * @access public |
||
69 | * @param array $config |
||
70 | * @return $this |
||
71 | */ |
||
72 | public function init(array $config = []) |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * 获取日志信息 |
||
90 | * @access public |
||
91 | * @param string $type 信息类型 |
||
92 | * @return array |
||
93 | */ |
||
94 | public function getLog(string $type = ''): array |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * 记录日志信息 |
||
101 | * @access public |
||
102 | * @param mixed $msg 日志信息 |
||
103 | * @param string $type 日志级别 |
||
104 | * @param array $context 替换内容 |
||
105 | * @return $this |
||
106 | */ |
||
107 | public function record($msg, string $type = 'info', array $context = []) |
||
108 | { |
||
109 | if (!$this->allowWrite) { |
||
110 | return; |
||
111 | } |
||
112 | |||
113 | if (is_string($msg) && !empty($context)) { |
||
114 | $replace = []; |
||
115 | foreach ($context as $key => $val) { |
||
116 | $replace['{' . $key . '}'] = $val; |
||
117 | } |
||
118 | |||
119 | $msg = strtr($msg, $replace); |
||
120 | } |
||
121 | |||
122 | if (PHP_SAPI == 'cli') { |
||
123 | if (empty($this->config['level']) || in_array($type, $this->config['level'])) { |
||
124 | // 命令行日志实时写入 |
||
125 | $this->write($msg, $type, true); |
||
126 | } |
||
127 | } else { |
||
128 | $this->log[$type][] = $msg; |
||
129 | } |
||
130 | |||
131 | return $this; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * 记录批量日志信息 |
||
136 | * @access public |
||
137 | * @param array $msg 日志信息 |
||
138 | * @param string $type 日志级别 |
||
139 | * @return $this |
||
140 | */ |
||
141 | public function append(array $log, string $type = 'info') |
||
142 | { |
||
143 | if (!$this->allowWrite || empty($log)) { |
||
144 | return $this; |
||
145 | } |
||
146 | |||
147 | if (isset($this->log[$type])) { |
||
148 | $this->log[$type] += $log; |
||
149 | } else { |
||
150 | $this->log[$type] = $log; |
||
151 | } |
||
152 | |||
153 | return $this; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * 清空日志信息 |
||
158 | * @access public |
||
159 | * @return $this |
||
160 | */ |
||
161 | public function clear() |
||
162 | { |
||
163 | $this->log = []; |
||
164 | |||
165 | return $this; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * 当前日志记录的授权key |
||
170 | * @access public |
||
171 | * @param string $key 授权key |
||
172 | * @return $this |
||
173 | */ |
||
174 | public function key(string $key) |
||
175 | { |
||
176 | $this->key = $key; |
||
177 | |||
178 | return $this; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * 检查日志写入权限 |
||
183 | * @access public |
||
184 | * @param array $config 当前日志配置参数 |
||
185 | * @return bool |
||
186 | */ |
||
187 | public function check(array $config): bool |
||
188 | { |
||
189 | if ($this->key && !empty($config['allow_key']) && !in_array($this->key, $config['allow_key'])) { |
||
190 | return false; |
||
191 | } |
||
192 | |||
193 | return true; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * 关闭本次请求日志写入 |
||
198 | * @access public |
||
199 | * @return $this |
||
200 | */ |
||
201 | public function close() |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * 保存调试信息 |
||
211 | * @access public |
||
212 | * @return bool |
||
213 | */ |
||
214 | public function save(): bool |
||
215 | { |
||
216 | if (empty($this->log) || !$this->allowWrite) { |
||
217 | return true; |
||
218 | } |
||
219 | |||
220 | if (!$this->check($this->config)) { |
||
221 | // 检测日志写入权限 |
||
222 | return false; |
||
223 | } |
||
224 | |||
225 | $log = []; |
||
226 | |||
227 | foreach ($this->log as $level => $info) { |
||
228 | if (!App::isDebug() && 'debug' == $level) { |
||
229 | continue; |
||
230 | } |
||
231 | |||
232 | if (empty($this->config['level']) || in_array($level, $this->config['level'])) { |
||
233 | $log[$level] = $info; |
||
234 | Event::trigger('LogLevel', [$level, $info]); |
||
235 | } |
||
236 | } |
||
237 | |||
238 | $result = $this->driver->save($log); |
||
239 | |||
240 | if ($result) { |
||
241 | $this->log = []; |
||
242 | } |
||
243 | |||
244 | return $result; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * 实时写入日志信息 并支持行为 |
||
249 | * @access public |
||
250 | * @param mixed $msg 调试信息 |
||
251 | * @param string $type 日志级别 |
||
252 | * @param bool $force 是否强制写入 |
||
253 | * @return bool |
||
254 | */ |
||
255 | public function write($msg, string $type = 'info', bool $force = false): bool |
||
256 | { |
||
257 | // 封装日志信息 |
||
258 | if (empty($this->config['level'])) { |
||
259 | $force = true; |
||
260 | } |
||
261 | |||
262 | $log = []; |
||
263 | |||
264 | if (true === $force || in_array($type, $this->config['level'])) { |
||
265 | $log[$type][] = $msg; |
||
266 | } else { |
||
267 | return false; |
||
268 | } |
||
269 | |||
270 | // 监听LogWrite |
||
271 | Event::trigger('LogWrite', $log); |
||
272 | |||
273 | // 写入日志 |
||
274 | return $this->driver->save($log, false); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * 记录日志信息 |
||
279 | * @access public |
||
280 | * @param string $level 日志级别 |
||
281 | * @param mixed $message 日志信息 |
||
282 | * @param array $context 替换内容 |
||
283 | * @return void |
||
284 | */ |
||
285 | public function log($level, $message, array $context = []): void |
||
286 | { |
||
287 | $this->record($message, $level, $context); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * 记录emergency信息 |
||
292 | * @access public |
||
293 | * @param mixed $message 日志信息 |
||
294 | * @param array $context 替换内容 |
||
295 | * @return void |
||
296 | */ |
||
297 | public function emergency($message, array $context = []): void |
||
298 | { |
||
299 | $this->log(__FUNCTION__, $message, $context); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * 记录警报信息 |
||
304 | * @access public |
||
305 | * @param mixed $message 日志信息 |
||
306 | * @param array $context 替换内容 |
||
307 | * @return void |
||
308 | */ |
||
309 | public function alert($message, array $context = []): void |
||
310 | { |
||
311 | $this->log(__FUNCTION__, $message, $context); |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * 记录紧急情况 |
||
316 | * @access public |
||
317 | * @param mixed $message 日志信息 |
||
318 | * @param array $context 替换内容 |
||
319 | * @return void |
||
320 | */ |
||
321 | public function critical($message, array $context = []): void |
||
322 | { |
||
323 | $this->log(__FUNCTION__, $message, $context); |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * 记录错误信息 |
||
328 | * @access public |
||
329 | * @param mixed $message 日志信息 |
||
330 | * @param array $context 替换内容 |
||
331 | * @return void |
||
332 | */ |
||
333 | public function error($message, array $context = []): void |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * 记录warning信息 |
||
340 | * @access public |
||
341 | * @param mixed $message 日志信息 |
||
342 | * @param array $context 替换内容 |
||
343 | * @return void |
||
344 | */ |
||
345 | public function warning($message, array $context = []): void |
||
346 | { |
||
347 | $this->log(__FUNCTION__, $message, $context); |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * 记录notice信息 |
||
352 | * @access public |
||
353 | * @param mixed $message 日志信息 |
||
354 | * @param array $context 替换内容 |
||
355 | * @return void |
||
356 | */ |
||
357 | public function notice($message, array $context = []): void |
||
358 | { |
||
359 | $this->log(__FUNCTION__, $message, $context); |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * 记录一般信息 |
||
364 | * @access public |
||
365 | * @param mixed $message 日志信息 |
||
366 | * @param array $context 替换内容 |
||
367 | * @return void |
||
368 | */ |
||
369 | public function info($message, array $context = []): void |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * 记录调试信息 |
||
376 | * @access public |
||
377 | * @param mixed $message 日志信息 |
||
378 | * @param array $context 替换内容 |
||
379 | * @return void |
||
380 | */ |
||
381 | public function debug($message, array $context = []): void |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * 记录sql信息 |
||
388 | * @access public |
||
389 | * @param mixed $message 日志信息 |
||
390 | * @param array $context 替换内容 |
||
391 | * @return void |
||
392 | */ |
||
393 | public function sql($message, array $context = []): void |
||
398 |