Complex classes like Logger 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Logger, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Logger |
||
| 18 | { |
||
| 19 | use LoggerUtils; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var Logger ロガー |
||
| 23 | */ |
||
| 24 | private static $logger; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var LoggerFormatter ロガーフォーマッタ |
||
| 28 | */ |
||
| 29 | private static $formatter; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var Container ログ設定コンテナ |
||
| 33 | */ |
||
| 34 | private static $config; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Container ログ設定コンテナ |
||
| 38 | */ |
||
| 39 | private $logConfig; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array<IOutputter> Outputterリスト |
||
| 43 | */ |
||
| 44 | private $outputters; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var Container IOコンテナ |
||
| 48 | */ |
||
| 49 | private $ioContainer; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var File ログファイル |
||
| 53 | */ |
||
| 54 | private $logFile; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var File ステータスファイル |
||
| 58 | */ |
||
| 59 | private $statusFile; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * コンストラクタ |
||
| 63 | * @param Container ログ設定コンテナ |
||
| 64 | */ |
||
| 65 | 141 | private function __construct(Container $logConfig) |
|
| 66 | { |
||
| 67 | 141 | $this->logConfig = $logConfig; |
|
| 68 | 141 | $this->outputters = []; |
|
| 69 | |||
| 70 | 141 | $logFile = new File($logConfig->logPath); |
|
| 71 | 141 | $statusFile = new File($logConfig->statusPath); |
|
| 72 | |||
| 73 | 141 | $this->ioContainer = new Container(); |
|
| 74 | |||
| 75 | 14 | $this->ioContainer->statusReader = function () use ($statusFile) { |
|
| 76 | 14 | return new FileReader($statusFile); |
|
| 77 | }; |
||
| 78 | $this->ioContainer->statusWriter = function () use ($statusFile) { |
||
| 79 | return new SimpleFileWriter($statusFile->getFilePath()); |
||
| 80 | }; |
||
| 81 | $this->ioContainer->logWriter = function () use ($logFile) { |
||
| 82 | return new SimpleFileWriter($logFile->getFilePath()); |
||
| 83 | }; |
||
| 84 | |||
| 85 | 141 | $this->logFile = $logFile; |
|
| 86 | 141 | $this->statusFile = $statusFile; |
|
| 87 | 141 | } |
|
| 88 | |||
| 89 | /** |
||
| 90 | * デストラクタ |
||
| 91 | */ |
||
| 92 | public function __destruct() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * ログ設定を返却する |
||
| 99 | * @return Container ログ設定 |
||
| 100 | */ |
||
| 101 | public function getConfig() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * 遅延書き出しを有効にする |
||
| 108 | */ |
||
| 109 | public static function enableLazyWrite() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * 即時書き出しを有効にする |
||
| 116 | */ |
||
| 117 | public static function enableDirectWrite() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * インスタンスを返却する |
||
| 124 | * @return WebStream\Module\Logger ロガーインスタンス |
||
| 125 | */ |
||
| 126 | 141 | public static function getInstance() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * ファイナライザ |
||
| 133 | */ |
||
| 134 | public static function finalize() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Loggerを初期化する |
||
| 143 | * @param Container ログ設定コンテナ |
||
| 144 | */ |
||
| 145 | 141 | public static function init(Container $config) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Loggerが初期化済みかどうかチェックする |
||
| 154 | * @param bool 初期化済みならtrue |
||
| 155 | */ |
||
| 156 | public static function isInitialized() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Loggerメソッドの呼び出しを受ける |
||
| 163 | * @param string メソッド名(ログレベル文字列) |
||
| 164 | * @param array 引数 |
||
| 165 | */ |
||
| 166 | public static function __callStatic($level, $arguments) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Outputterを設定する |
||
| 181 | * @param array<IOutputter> $outputters Outputterリスト |
||
| 182 | */ |
||
| 183 | 141 | public function setOutputter(array $outputters) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * タイムスタンプを取得する |
||
| 195 | * @return string タイムスタンプ |
||
| 196 | */ |
||
| 197 | private function getTimeStamp() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * ログを書き出す |
||
| 207 | * @param string ログレベル文字列 |
||
| 208 | * @param string 出力文字列 |
||
| 209 | * @param array<mixed> 埋め込み値リスト |
||
| 210 | */ |
||
| 211 | 141 | public function write($level, $msg, $context = null) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * ログファイルをアーカイブする |
||
| 249 | * stream.log -> stream.(作成日時)-(現在日時).log |
||
| 250 | * @param string ログファイルパス |
||
| 251 | */ |
||
| 252 | 97 | private function rotate() |
|
| 266 | |||
| 267 | /** |
||
| 268 | * ログステータスファイルに書きこむ |
||
| 269 | * @throws IOException |
||
| 270 | */ |
||
| 271 | private function writeStatus() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * ログステータスファイルを読み込む |
||
| 278 | * @return int UnixTime |
||
| 279 | * @throws LoggerException |
||
| 280 | */ |
||
| 281 | 14 | private function readStatus() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * ステータスファイルを作成する |
||
| 293 | */ |
||
| 294 | 14 | private function createStatusFile() |
|
| 301 | |||
| 302 | /** |
||
| 303 | * ローテートを実行する |
||
| 304 | * @param integer 作成日時のUnixTime |
||
| 305 | * @param integer 現在日時のUnixTime |
||
| 306 | */ |
||
| 307 | 8 | private function runRotate($from, $to) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * 時間単位でローテートする |
||
| 323 | * stream.log -> stream.(作成日時)-(現在日時).log |
||
| 324 | */ |
||
| 325 | private function rotateByCycle() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * サイズ単位でローテートする |
||
| 339 | * stream.log -> stream.(作成日時)-(現在日時).log |
||
| 340 | */ |
||
| 341 | private function rotateBySize() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * ログ出力パスを返却する |
||
| 356 | * @return string ログ出力パス |
||
| 357 | */ |
||
| 358 | public function getLogPath() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * ログローテートサイクルを返却する |
||
| 365 | * @return string ログ出力パス |
||
| 366 | */ |
||
| 367 | public function getLogRotateCycle() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * ログローテートサイズを返却する |
||
| 374 | * @return string ログ出力パス |
||
| 375 | */ |
||
| 376 | public function getLogRotateSize() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * 遅延書き出しを有効にする |
||
| 383 | */ |
||
| 384 | public function lazyWrite() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * 即時書き出しを有効にする |
||
| 395 | */ |
||
| 396 | public function directWrite() |
||
| 404 | } |
||
| 405 |