| Conditions | 16 |
| Paths | 533 |
| Total Lines | 78 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 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 |
||
| 118 | public function writeLog($message, $level = self::INFO){ |
||
| 119 | $configLogLevel = get_config('log_level'); |
||
| 120 | if(! $configLogLevel){ |
||
| 121 | //so means no need log just stop here |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | //check config log level |
||
| 125 | if(! self::isValidConfigLevel($configLogLevel)){ |
||
| 126 | //NOTE: here need put the show_error() "logging" to false to prevent loop |
||
| 127 | show_error('Invalid config log level [' . $configLogLevel . '], the value must be one of the following: ' . implode(', ', array_map('strtoupper', self::$validConfigLevel)), $title = 'Log Config Error', $logging = false); |
||
| 128 | } |
||
| 129 | |||
| 130 | //check if config log_logger_name is set |
||
| 131 | if($this->logger){ |
||
| 132 | $configLoggerName = get_config('log_logger_name', ''); |
||
| 133 | if($configLoggerName){ |
||
| 134 | if (is_array($configLoggerName)){ |
||
| 135 | //for best comparaison put all string to lowercase |
||
| 136 | $configLoggerName = array_map('strtolower', $configLoggerName); |
||
| 137 | if(! in_array(strtolower($this->logger), $configLoggerName)){ |
||
| 138 | return; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | else if(strtolower($this->logger) !== strtolower($configLoggerName)){ |
||
| 142 | return; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | //if $level is not an integer |
||
| 148 | if(! is_numeric($level)){ |
||
| 149 | $level = self::getLevelValue($level); |
||
| 150 | } |
||
| 151 | |||
| 152 | //check if can logging regarding the log level config |
||
| 153 | $configLevel = self::getLevelValue($configLogLevel); |
||
| 154 | if($configLevel > $level){ |
||
| 155 | //can't log |
||
| 156 | return; |
||
| 157 | } |
||
| 158 | |||
| 159 | $logSavePath = get_config('log_save_path'); |
||
| 160 | if(! $logSavePath){ |
||
| 161 | $logSavePath = LOGS_PATH; |
||
| 162 | } |
||
| 163 | |||
| 164 | if(! is_dir($logSavePath) || !is_writable($logSavePath)){ |
||
| 165 | //NOTE: here need put the show_error() "logging" to false to prevent loop |
||
| 166 | show_error('Error : the log dir does not exists or is not writable', $title = 'Log directory error', $logging = false); |
||
| 167 | } |
||
| 168 | |||
| 169 | $path = $logSavePath . 'logs-' . date('Y-m-d') . '.log'; |
||
| 170 | if(! file_exists($path)){ |
||
| 171 | touch($path); |
||
| 172 | } |
||
| 173 | //may be at this time helper user_agent not yet included |
||
| 174 | require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
||
| 175 | |||
| 176 | ///////////////////// date ////////////// |
||
| 177 | $timestampWithMicro = microtime(true); |
||
| 178 | $microtime = sprintf('%06d', ($timestampWithMicro - floor($timestampWithMicro)) * 1000000); |
||
| 179 | $dateTime = new DateTime(date('Y-m-d H:i:s.' . $microtime, $timestampWithMicro)); |
||
| 180 | $logDate = $dateTime->format('Y-m-d H:i:s.u'); |
||
| 181 | //ip |
||
| 182 | $ip = get_ip(); |
||
| 183 | //level name |
||
| 184 | $levelName = self::getLevelName($level); |
||
|
|
|||
| 185 | |||
| 186 | //debug info |
||
| 187 | $dtrace = debug_backtrace(); |
||
| 188 | $fileInfo = $dtrace[0]['file'] == __FILE__ ? $dtrace[1] : $dtrace[0]; |
||
| 189 | |||
| 190 | $str = $logDate . ' [' . str_pad($levelName, 7 /*warning len*/) . '] ' . ' [' . str_pad($ip, 15) . '] ' . $this->logger . ' : ' . $message . ' ' . '[' . $fileInfo['file'] . '::' . $fileInfo['line'] . ']' . "\n"; |
||
| 191 | $fp = fopen($path, 'a+'); |
||
| 192 | if(is_resource($fp)){ |
||
| 193 | flock($fp, LOCK_EX); // exclusive lock, will get released when the file is closed |
||
| 194 | fwrite($fp, $str); |
||
| 195 | fclose($fp); |
||
| 196 | } |
||
| 271 |