| Conditions | 17 |
| Paths | 1024 |
| Total Lines | 118 |
| Code Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 64 | public function getHandler(array $channelValues) |
||
| 65 | { |
||
| 66 | $app = $this->app; |
||
| 67 | |||
| 68 | $levels = Logger::getLevels(); |
||
| 69 | |||
| 70 | // ファイル名などの設定を行い、設定がなければデフォルト値を設定 |
||
| 71 | $logFileName = isset($channelValues['filename']) ? $channelValues['filename'] : $app['config']['log']['filename']; |
||
| 72 | $delimiter = isset($channelValues['delimiter']) ? $channelValues['delimiter'] : $app['config']['log']['delimiter']; |
||
| 73 | $dateFormat = isset($channelValues['dateformat']) ? $channelValues['dateformat'] : $app['config']['log']['dateformat']; |
||
| 74 | $logLevel = isset($channelValues['log_level']) ? $channelValues['log_level'] : $app['config']['log']['log_level']; |
||
| 75 | $actionLevel = isset($channelValues['action_level']) ? $channelValues['action_level'] : $app['config']['log']['action_level']; |
||
| 76 | $passthruLevel = isset($channelValues['passthru_level']) ? $channelValues['passthru_level'] : $app['config']['log']['passthru_level']; |
||
| 77 | $maxFiles = isset($channelValues['max_files']) ? $channelValues['max_files'] : $app['config']['log']['max_files']; |
||
| 78 | $logDateFormat = isset($channelValues['log_dateformat']) ? $channelValues['log_dateformat'] : $app['config']['log']['log_dateformat']; |
||
| 79 | $logFormat = isset($channelValues['log_format']) ? $channelValues['log_format'] : $app['config']['log']['log_format']; |
||
| 80 | |||
| 81 | if ($app['debug']) { |
||
| 82 | $level = Logger::DEBUG; |
||
| 83 | } else { |
||
| 84 | $level = $logLevel; |
||
| 85 | } |
||
| 86 | |||
| 87 | |||
| 88 | // RotateHandlerの設定 |
||
| 89 | $filename = $app['config']['root_dir'].'/app/log/'.$logFileName.'.log'; |
||
| 90 | $RotateHandler = new RotatingFileHandler($filename, $maxFiles, $level); |
||
| 91 | $RotateHandler->setFilenameFormat( |
||
| 92 | $logFileName.$delimiter.'{date}'.$app['config']['log']['suffix'], |
||
| 93 | $dateFormat |
||
| 94 | ); |
||
| 95 | |||
| 96 | // ログフォーマットの設定(設定ファイルで定義) |
||
| 97 | $RotateHandler->setFormatter(new LineFormatter($logFormat.PHP_EOL, $logDateFormat, true, true)); |
||
| 98 | |||
| 99 | // FingerCossedHandlerの設定 |
||
| 100 | $FingerCrossedHandler = new FingersCrossedHandler( |
||
| 101 | $RotateHandler, |
||
| 102 | new ErrorLevelActivationStrategy($levels[$actionLevel]), |
||
| 103 | 0, |
||
| 104 | true, |
||
| 105 | true, |
||
| 106 | $levels[$passthruLevel] |
||
| 107 | ); |
||
| 108 | |||
| 109 | |||
| 110 | // Processorの内容をログ出力 |
||
| 111 | $webProcessor = new WebProcessor(); |
||
| 112 | $uidProcessor = new UidProcessor(8); |
||
| 113 | |||
| 114 | $FingerCrossedHandler->pushProcessor(function ($record) use ($app, $uidProcessor, $webProcessor) { |
||
| 115 | // ログフォーマットに出力する値を独自に設定 |
||
| 116 | |||
| 117 | $record['level_name'] = sprintf("%-5s", $record['level_name']); |
||
| 118 | |||
| 119 | // セッションIDと会員IDを設定 |
||
| 120 | $record['session_id'] = null; |
||
| 121 | $record['user_id'] = null; |
||
| 122 | if ($app->isBooted()) { |
||
| 123 | if (isset($app['session'])) { |
||
| 124 | $sessionId = $app['session']->getId(); |
||
| 125 | if ($sessionId) { |
||
| 126 | $record['session_id'] = substr(sha1($sessionId), 0, 8); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | if (isset($app['user'])) { |
||
| 130 | $user = $app->user(); |
||
| 131 | if ($user instanceof Customer || $user instanceof Member) { |
||
| 132 | $record['user_id'] = $user->getId(); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | $record['uid'] = $uidProcessor->getUid(); |
||
| 138 | |||
| 139 | $record['url'] = $webProcessor->getRequestUri(); |
||
| 140 | $record['ip'] = $webProcessor->getClientIp(); |
||
| 141 | $record['referrer'] = $webProcessor->getReferer(); |
||
| 142 | $record['method'] = $webProcessor->getMethod(); |
||
| 143 | $record['user_agent'] = $webProcessor->getUserAgent(); |
||
| 144 | |||
| 145 | // クラス名などを一旦保持し、不要な情報は削除 |
||
| 146 | $line = $record['extra']['line']; |
||
| 147 | $functionName = $record['extra']['function']; |
||
| 148 | // php5.3だとclass名が取得できないため、ファイル名を元に出力 |
||
| 149 | // $className = $record['extra']['class']; |
||
| 150 | $className = $record['extra']['file']; |
||
| 151 | |||
| 152 | // 不要な情報を削除 |
||
| 153 | unset($record['extra']['file']); |
||
| 154 | unset($record['extra']['line']); |
||
| 155 | unset($record['extra']['class']); |
||
| 156 | unset($record['extra']['function']); |
||
| 157 | |||
| 158 | $record['class'] = pathinfo($className, PATHINFO_FILENAME); |
||
| 159 | $record['function'] = $functionName; |
||
| 160 | $record['line'] = $line; |
||
| 161 | |||
| 162 | return $record; |
||
| 163 | }); |
||
| 164 | |||
| 165 | // クラス名等を取得するProcessor、ログ出力時にクラス名/関数名を無視するための設定を行っている |
||
| 166 | $skipClasses = array('Psr\\Log\\', 'Eccube\\Log\\'); |
||
| 167 | $skipFunctions = array( |
||
| 168 | 'log_info', |
||
| 169 | 'log_notice', |
||
| 170 | 'log_warning', |
||
| 171 | 'log_error', |
||
| 172 | 'log_critical', |
||
| 173 | 'log_alert', |
||
| 174 | 'log_emergency' |
||
| 175 | ); |
||
| 176 | $intro = new IntrospectionProcessor(Logger::DEBUG, $skipClasses, $skipFunctions); |
||
| 177 | $FingerCrossedHandler->pushProcessor($intro); |
||
| 178 | |||
| 179 | return $FingerCrossedHandler; |
||
| 180 | |||
| 181 | } |
||
| 182 | |||
| 184 |