| Total Complexity | 46 |
| Total Lines | 375 |
| 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 |
||
| 95 | { |
||
| 96 | return $type ? $this->log[$type] : $this->log; |
||
| 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 = []) |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * 记录批量日志信息 |
||
| 134 | * @access public |
||
| 135 | * @param array $msg 日志信息 |
||
| 136 | * @param string $type 日志级别 |
||
| 137 | * @return $this |
||
| 138 | */ |
||
| 139 | public function append(array $log, string $type = 'info') |
||
| 140 | { |
||
| 141 | if (!$this->allowWrite || empty($log)) { |
||
| 142 | return $this; |
||
| 143 | } |
||
| 144 | |||
| 145 | if (isset($this->log[$type])) { |
||
| 146 | $this->log[$type] += $log; |
||
| 147 | } else { |
||
| 148 | $this->log[$type] = $log; |
||
| 149 | } |
||
| 150 | |||
| 151 | return $this; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * 清空日志信息 |
||
| 156 | * @access public |
||
| 157 | * @return $this |
||
| 158 | */ |
||
| 159 | public function clear() |
||
| 160 | { |
||
| 161 | $this->log = []; |
||
| 162 | |||
| 163 | return $this; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * 当前日志记录的授权key |
||
| 168 | * @access public |
||
| 169 | * @param string $key 授权key |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | public function key(string $key) |
||
| 173 | { |
||
| 174 | $this->key = $key; |
||
| 175 | |||
| 176 | return $this; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * 检查日志写入权限 |
||
| 181 | * @access public |
||
| 182 | * @param array $config 当前日志配置参数 |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | public function check(array $config): bool |
||
| 186 | { |
||
| 187 | if ($this->key && !empty($config['allow_key']) && !in_array($this->key, $config['allow_key'])) { |
||
| 188 | return false; |
||
| 189 | } |
||
| 190 | |||
| 191 | return true; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * 关闭本次请求日志写入 |
||
| 196 | * @access public |
||
| 197 | * @return $this |
||
| 198 | */ |
||
| 199 | public function close() |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * 保存调试信息 |
||
| 209 | * @access public |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | public function save(): bool |
||
| 213 | { |
||
| 214 | if (empty($this->log) || !$this->allowWrite) { |
||
| 215 | return true; |
||
| 216 | } |
||
| 217 | |||
| 218 | if (!$this->check($this->config)) { |
||
| 219 | // 检测日志写入权限 |
||
| 220 | return false; |
||
| 221 | } |
||
| 222 | |||
| 223 | $log = []; |
||
| 224 | |||
| 225 | foreach ($this->log as $level => $info) { |
||
| 226 | if (!App::isDebug() && 'debug' == $level) { |
||
| 227 | continue; |
||
| 228 | } |
||
| 229 | |||
| 230 | if (empty($this->config['level']) || in_array($level, $this->config['level'])) { |
||
| 231 | $log[$level] = $info; |
||
| 232 | Event::trigger('LogLevel', [$level, $info]); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | $result = $this->driver->save($log); |
||
| 237 | |||
| 238 | if ($result) { |
||
| 239 | $this->log = []; |
||
| 240 | } |
||
| 241 | |||
| 242 | return $result; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * 实时写入日志信息 并支持行为 |
||
| 247 | * @access public |
||
| 248 | * @param mixed $msg 调试信息 |
||
| 249 | * @param string $type 日志级别 |
||
| 250 | * @param bool $force 是否强制写入 |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | public function write($msg, string $type = 'info', bool $force = false): bool |
||
| 254 | { |
||
| 255 | // 封装日志信息 |
||
| 256 | if (empty($this->config['level'])) { |
||
| 257 | $force = true; |
||
| 258 | } |
||
| 259 | |||
| 260 | $log = []; |
||
| 261 | |||
| 262 | if (true === $force || in_array($type, $this->config['level'])) { |
||
| 263 | $log[$type][] = $msg; |
||
| 264 | } else { |
||
| 265 | return false; |
||
| 266 | } |
||
| 267 | |||
| 268 | // 监听LogWrite |
||
| 269 | Event::trigger('LogWrite', $log); |
||
| 270 | |||
| 271 | // 写入日志 |
||
| 272 | return $this->driver->save($log, false); |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * 记录日志信息 |
||
| 277 | * @access public |
||
| 278 | * @param string $level 日志级别 |
||
| 279 | * @param mixed $message 日志信息 |
||
| 280 | * @param array $context 替换内容 |
||
| 281 | * @return void |
||
| 282 | */ |
||
| 283 | public function log($level, $message, array $context = []): void |
||
| 284 | { |
||
| 285 | $this->record($message, $level, $context); |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * 记录emergency信息 |
||
| 290 | * @access public |
||
| 291 | * @param mixed $message 日志信息 |
||
| 292 | * @param array $context 替换内容 |
||
| 293 | * @return void |
||
| 294 | */ |
||
| 295 | public function emergency($message, array $context = []): void |
||
| 296 | { |
||
| 297 | $this->log(__FUNCTION__, $message, $context); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * 记录警报信息 |
||
| 302 | * @access public |
||
| 303 | * @param mixed $message 日志信息 |
||
| 304 | * @param array $context 替换内容 |
||
| 305 | * @return void |
||
| 306 | */ |
||
| 307 | public function alert($message, array $context = []): void |
||
| 308 | { |
||
| 309 | $this->log(__FUNCTION__, $message, $context); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * 记录紧急情况 |
||
| 314 | * @access public |
||
| 315 | * @param mixed $message 日志信息 |
||
| 316 | * @param array $context 替换内容 |
||
| 317 | * @return void |
||
| 318 | */ |
||
| 319 | public function critical($message, array $context = []): void |
||
| 320 | { |
||
| 321 | $this->log(__FUNCTION__, $message, $context); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * 记录错误信息 |
||
| 326 | * @access public |
||
| 327 | * @param mixed $message 日志信息 |
||
| 328 | * @param array $context 替换内容 |
||
| 329 | * @return void |
||
| 330 | */ |
||
| 331 | public function error($message, array $context = []): void |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * 记录warning信息 |
||
| 338 | * @access public |
||
| 339 | * @param mixed $message 日志信息 |
||
| 340 | * @param array $context 替换内容 |
||
| 341 | * @return void |
||
| 342 | */ |
||
| 343 | public function warning($message, array $context = []): void |
||
| 344 | { |
||
| 345 | $this->log(__FUNCTION__, $message, $context); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * 记录notice信息 |
||
| 350 | * @access public |
||
| 351 | * @param mixed $message 日志信息 |
||
| 352 | * @param array $context 替换内容 |
||
| 353 | * @return void |
||
| 354 | */ |
||
| 355 | public function notice($message, array $context = []): void |
||
| 356 | { |
||
| 357 | $this->log(__FUNCTION__, $message, $context); |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * 记录一般信息 |
||
| 362 | * @access public |
||
| 363 | * @param mixed $message 日志信息 |
||
| 364 | * @param array $context 替换内容 |
||
| 365 | * @return void |
||
| 366 | */ |
||
| 367 | public function info($message, array $context = []): void |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * 记录调试信息 |
||
| 374 | * @access public |
||
| 375 | * @param mixed $message 日志信息 |
||
| 376 | * @param array $context 替换内容 |
||
| 377 | * @return void |
||
| 378 | */ |
||
| 379 | public function debug($message, array $context = []): void |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * 记录sql信息 |
||
| 386 | * @access public |
||
| 387 | * @param mixed $message 日志信息 |
||
| 388 | * @param array $context 替换内容 |
||
| 389 | * @return void |
||
| 390 | */ |
||
| 391 | public function sql($message, array $context = []): void |
||
| 396 |