| Conditions | 14 |
| Paths | 3072 |
| Total Lines | 72 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 75 | public static function write($app, $message, $level) { |
||
| 76 | $config = \OC::$server->getSystemConfig(); |
||
| 77 | |||
| 78 | // default to ISO8601 |
||
| 79 | $format = $config->getValue('logdateformat', \DateTime::ATOM); |
||
| 80 | $logTimeZone = $config->getValue('logtimezone', 'UTC'); |
||
| 81 | try { |
||
| 82 | $timezone = new \DateTimeZone($logTimeZone); |
||
| 83 | } catch (\Exception $e) { |
||
| 84 | $timezone = new \DateTimeZone('UTC'); |
||
| 85 | } |
||
| 86 | $time = \DateTime::createFromFormat("U.u", number_format(microtime(true), 4, ".", "")); |
||
| 87 | if ($time === false) { |
||
| 88 | $time = new \DateTime(null, $timezone); |
||
| 89 | } else { |
||
| 90 | // apply timezone if $time is created from UNIX timestamp |
||
| 91 | $time->setTimezone($timezone); |
||
| 92 | } |
||
| 93 | $request = \OC::$server->getRequest(); |
||
| 94 | $reqId = $request->getId(); |
||
| 95 | $remoteAddr = $request->getRemoteAddress(); |
||
| 96 | // remove username/passwords from URLs before writing the to the log file |
||
| 97 | $time = $time->format($format); |
||
| 98 | $url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--'; |
||
| 99 | $method = is_string($request->getMethod()) ? $request->getMethod() : '--'; |
||
| 100 | if($config->getValue('installed', false)) { |
||
| 101 | $user = (\OC_User::getUser()) ? \OC_User::getUser() : '--'; |
||
| 102 | } else { |
||
| 103 | $user = '--'; |
||
| 104 | } |
||
| 105 | $userAgent = $request->getHeader('User-Agent') ?: '--'; |
||
| 106 | $version = $config->getValue('version', ''); |
||
| 107 | $entry = compact( |
||
| 108 | 'reqId', |
||
| 109 | 'level', |
||
| 110 | 'time', |
||
| 111 | 'remoteAddr', |
||
| 112 | 'user', |
||
| 113 | 'app', |
||
| 114 | 'method', |
||
| 115 | 'url', |
||
| 116 | 'message', |
||
| 117 | 'userAgent', |
||
| 118 | 'version' |
||
| 119 | ); |
||
| 120 | // PHP's json_encode only accept proper UTF-8 strings, loop over all |
||
| 121 | // elements to ensure that they are properly UTF-8 compliant or convert |
||
| 122 | // them manually. |
||
| 123 | foreach($entry as $key => $value) { |
||
| 124 | if(is_string($value)) { |
||
| 125 | $testEncode = json_encode($value); |
||
| 126 | if($testEncode === false) { |
||
| 127 | $entry[$key] = utf8_encode($value); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | $entry = json_encode($entry, JSON_PARTIAL_OUTPUT_ON_ERROR); |
||
| 132 | $handle = @fopen(self::$logFile, 'a'); |
||
| 133 | if ((fileperms(self::$logFile) & 0777) != 0640) { |
||
| 134 | @chmod(self::$logFile, 0640); |
||
|
|
|||
| 135 | } |
||
| 136 | if ($handle) { |
||
| 137 | fwrite($handle, $entry."\n"); |
||
| 138 | fclose($handle); |
||
| 139 | } else { |
||
| 140 | // Fall back to error_log |
||
| 141 | error_log($entry); |
||
| 142 | } |
||
| 143 | if (php_sapi_name() === 'cli-server') { |
||
| 144 | error_log($message, 4); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 204 |
If you suppress an error, we recommend checking for the error condition explicitly: