| Total Complexity | 42 |
| Total Lines | 299 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like FileLogger 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 FileLogger, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class FileLogger extends AbstractLogger implements ConfigInterface |
||
| 20 | { |
||
| 21 | use ConfigTrait; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * 文件 |
||
| 25 | * |
||
| 26 | * @var resource |
||
| 27 | */ |
||
| 28 | protected $temp; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * 临时文件名 |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $tempName; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * 移除文件 |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $removeFiles = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * 最后的日志 |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $latest; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * 构建文件日志 |
||
| 53 | * |
||
| 54 | * @param array $config |
||
| 55 | */ |
||
| 56 | public function __construct(array $config = []) |
||
| 57 | { |
||
| 58 | $this->set($config); |
||
| 59 | register_shutdown_function([$this, 'shutdown']); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * 设置配置 |
||
| 64 | * @param array $config |
||
| 65 | */ |
||
| 66 | public function set(array $config) |
||
| 67 | { |
||
| 68 | $this->applyConfig($config); |
||
| 69 | FileSystem::make($this->getConfig('save-path')); |
||
| 70 | FileSystem::make($this->getConfig('save-dump-path')); |
||
| 71 | FileSystem::make($this->getConfig('save-zip-path')); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return resource |
||
| 76 | * @throws FileLoggerException |
||
| 77 | */ |
||
| 78 | public function getAvailableWrite() |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @throws FileLoggerException |
||
| 89 | */ |
||
| 90 | private function prepareWrite() |
||
| 91 | { |
||
| 92 | $unique = substr(md5(uniqid()), 0, 8); |
||
| 93 | $save = $this->getConfig('save-path'); |
||
| 94 | $this->tempName = $save . '/' . date('YmdHis') . '.' . $unique . '.log'; |
||
| 95 | $temp = fopen($this->tempName, 'w+'); |
||
| 96 | if ($temp !== false) { |
||
| 97 | $this->temp = $temp; |
||
| 98 | } else { |
||
| 99 | throw new FileLoggerException(__METHOD__ . ':' . sprintf('cannot create log file')); |
||
| 100 | } |
||
| 101 | $this->latest = $save . '/' . $this->getConfig('file-name'); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | public function getDefaultConfig(): array |
||
| 117 | ]; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * 打包文件 |
||
| 122 | */ |
||
| 123 | private function packLogFile() |
||
| 124 | { |
||
| 125 | $logFile = $this->latest; |
||
| 126 | $path = preg_replace( |
||
| 127 | '/[\\\\]+/', |
||
| 128 | '/', |
||
| 129 | $this->getConfig('save-zip-path') . '/' . date('Y-m-d') . '.zip' |
||
| 130 | ); |
||
| 131 | $zip = $this->getZipArchive($path); |
||
| 132 | if ($zip !== null) { |
||
| 133 | $add = $zip->addFile($logFile, date('Y-m-d') . '-' . $zip->numFiles . '.log'); |
||
| 134 | if (is_dir($this->getConfig('save-dump-path'))) { |
||
| 135 | $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator( |
||
| 136 | $this->getConfig('save-dump-path'), |
||
| 137 | RecursiveDirectoryIterator::SKIP_DOTS |
||
| 138 | )); |
||
| 139 | foreach ($it as $dumpLog) { |
||
| 140 | if ($zip->addFile($dumpLog, 'dump/' . basename($dumpLog))) { |
||
| 141 | array_push($this->removeFiles, $dumpLog); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | $zip->close(); |
||
| 146 | if ($add) { |
||
| 147 | $this->clearContent($logFile); |
||
| 148 | } |
||
| 149 | } else { |
||
| 150 | if (is_file($logFile) && file_exists($logFile)) { |
||
| 151 | $this->safeMoveKeep( |
||
| 152 | $logFile, |
||
| 153 | $this->getConfig('save-path') |
||
| 154 | . '/' . date('Y-m-d') |
||
| 155 | . '-' . substr(md5(uniqid()), 0, 8). '.log' |
||
| 156 | ); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param string $from |
||
| 163 | * @param string $to |
||
| 164 | */ |
||
| 165 | private function safeMoveKeep(string $from, string $to) |
||
| 166 | { |
||
| 167 | $fromR = fopen($from, 'r'); |
||
| 168 | $toR = fopen($to, 'w+'); |
||
| 169 | flock($toR, LOCK_EX); |
||
|
|
|||
| 170 | // 复制内容 |
||
| 171 | stream_copy_to_stream($fromR, $toR); |
||
| 172 | flock($toR, LOCK_UN); |
||
| 173 | fclose($toR); |
||
| 174 | fclose($fromR); |
||
| 175 | // 清空内容 |
||
| 176 | $this->clearContent($from); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * 清空内容 |
||
| 181 | * @param string $path |
||
| 182 | */ |
||
| 183 | private function clearContent(string $path) |
||
| 184 | { |
||
| 185 | fclose(fopen($path, 'w')); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * 获取压缩 |
||
| 190 | * |
||
| 191 | * @param string $path |
||
| 192 | * @return ZipArchive|null |
||
| 193 | */ |
||
| 194 | private function getZipArchive(string $path) |
||
| 195 | { |
||
| 196 | if (class_exists('ZipArchive')) { |
||
| 197 | $zip = new ZipArchive; |
||
| 198 | $res = $zip->open($path, ZipArchive::CREATE); |
||
| 199 | if ($res === true) { |
||
| 200 | return $zip; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | return null; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * 检查日志文件大小 |
||
| 208 | * |
||
| 209 | * @return boolean |
||
| 210 | */ |
||
| 211 | private function checkSize(): bool |
||
| 212 | { |
||
| 213 | $logFile = $this->latest; |
||
| 214 | if (file_exists($logFile)) { |
||
| 215 | if (filesize($logFile) > $this->getConfig('max-file-size')) { |
||
| 216 | return true; |
||
| 217 | } |
||
| 218 | } |
||
| 219 | return false; |
||
| 220 | } |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * @param string $level |
||
| 225 | * @param string $message |
||
| 226 | * @param array $context |
||
| 227 | * @return mixed|void |
||
| 228 | * @throws FileLoggerException |
||
| 229 | */ |
||
| 230 | public function log($level, string $message, array $context = []) |
||
| 231 | { |
||
| 232 | if (LogLevel::compare($level, $this->getConfig('log-level')) >= 0) { |
||
| 233 | $replace = []; |
||
| 234 | $message = $this->interpolate($message, $context); |
||
| 235 | $replace['%level%'] = $level; |
||
| 236 | $replace['%message%'] = $message; |
||
| 237 | $write = strtr($this->getConfig('log-format'), $replace); |
||
| 238 | fwrite($this->getAvailableWrite(), $write . PHP_EOL); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * 将临时文件写入最后日志 |
||
| 245 | */ |
||
| 246 | private function rollLatest() |
||
| 247 | { |
||
| 248 | if (isset($this->latest)) { |
||
| 249 | $latest = fopen($this->latest, 'a+'); |
||
| 250 | if (flock($latest, LOCK_EX)) { |
||
| 251 | rewind($this->temp); |
||
| 252 | stream_copy_to_stream($this->temp, $latest); |
||
| 253 | flock($latest, LOCK_UN); |
||
| 254 | if (file_exists($this->tempName)) { |
||
| 255 | unlink($this->tempName); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | fclose($latest); |
||
| 259 | fclose($this->temp); |
||
| 260 | $this->temp = null; |
||
| 261 | $this->tempName = null; |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * 删除已经压缩的文件 |
||
| 267 | */ |
||
| 268 | private function removePackFiles() |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * 即时写入日志 |
||
| 280 | */ |
||
| 281 | public function write() |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * 程序关闭时调用 |
||
| 292 | */ |
||
| 293 | public function shutdown() |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param string $message |
||
| 303 | * @param array $context |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public function interpolate(string $message, array $context) |
||
| 318 | } |
||
| 319 | } |
||
| 320 |