@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | |
| 17 | 17 | class Debug implements LoggerInterface, LoggerAwareInterface, DumpInterface, AttachInterface, ConfigInterface |
| 18 | 18 | { |
| 19 | - use LoggerTrait,LoggerAwareTrait,DumpTrait,AttachTrait,ConfigTrait; |
|
| 19 | + use LoggerTrait, LoggerAwareTrait, DumpTrait, AttachTrait, ConfigTrait; |
|
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | 22 | * 时间记录 |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | { |
| 52 | 52 | $replace = []; |
| 53 | 53 | foreach ($context as $key => $val) { |
| 54 | - $replace['{' . $key . '}'] = $val; |
|
| 54 | + $replace['{'.$key.'}'] = $val; |
|
| 55 | 55 | } |
| 56 | 56 | return strtr($message, $replace); |
| 57 | 57 | } |
@@ -67,26 +67,26 @@ discard block |
||
| 67 | 67 | return $attribute; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - protected static function formatBytes(int $bytes, int $precision=0) |
|
| 70 | + protected static function formatBytes(int $bytes, int $precision = 0) |
|
| 71 | 71 | { |
| 72 | - $human= ['B', 'KB', 'MB', 'GB', 'TB']; |
|
| 72 | + $human = ['B', 'KB', 'MB', 'GB', 'TB']; |
|
| 73 | 73 | $bytes = max($bytes, 0); |
| 74 | - $pow = floor(($bytes?log($bytes):0)/log(1024)); |
|
| 75 | - $pos = min($pow, count($human)-1); |
|
| 76 | - $bytes /= (1 << (10* $pos)); |
|
| 74 | + $pow = floor(($bytes ?log($bytes) : 0) / log(1024)); |
|
| 75 | + $pos = min($pow, count($human) - 1); |
|
| 76 | + $bytes /= (1 << (10 * $pos)); |
|
| 77 | 77 | return round($bytes, $precision).' '.$human[$pos]; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - public function time(string $name, string $type= LogLevel::INFO) |
|
| 80 | + public function time(string $name, string $type = LogLevel::INFO) |
|
| 81 | 81 | { |
| 82 | - $this->timeRecord[$name]=['time'=>microtime(true),'level'=>$type]; |
|
| 82 | + $this->timeRecord[$name] = ['time'=>microtime(true), 'level'=>$type]; |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | public function timeEnd(string $name) |
| 86 | 86 | { |
| 87 | 87 | if (\array_key_exists($name, $this->timeRecord)) { |
| 88 | - $pass=microtime(true)-$this->timeRecord[$name]['time']; |
|
| 89 | - $this->log($this->timeRecord[$name]['level'], 'process cost '. $name.' '. number_format($pass, 5).'s'); |
|
| 88 | + $pass = microtime(true) - $this->timeRecord[$name]['time']; |
|
| 89 | + $this->log($this->timeRecord[$name]['level'], 'process cost '.$name.' '.number_format($pass, 5).'s'); |
|
| 90 | 90 | return $pass; |
| 91 | 91 | } |
| 92 | 92 | return 0; |
@@ -9,13 +9,13 @@ |
||
| 9 | 9 | protected $ignorePath = [__FILE__]; |
| 10 | 10 | protected $backtrace; |
| 11 | 11 | |
| 12 | - public function __construct(array $backtrace, array $ignorePath =[]) |
|
| 12 | + public function __construct(array $backtrace, array $ignorePath = []) |
|
| 13 | 13 | { |
| 14 | 14 | $this->ignorePath = \array_merge($this->ignorePath, $ignorePath); |
| 15 | 15 | $this->backtrace = $backtrace; |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | - public function getCallerTrace():?array |
|
| 18 | + public function getCallerTrace(): ?array |
|
| 19 | 19 | { |
| 20 | 20 | foreach ($this->backtrace as $trace) { |
| 21 | 21 | if (array_key_exists('file', $trace)) { |
@@ -46,12 +46,12 @@ discard block |
||
| 46 | 46 | * @throws FileLoggerException |
| 47 | 47 | * @param array $config |
| 48 | 48 | */ |
| 49 | - public function __construct(array $config=[]) |
|
| 49 | + public function __construct(array $config = []) |
|
| 50 | 50 | { |
| 51 | 51 | $this->applyConfig($config); |
| 52 | 52 | $temp = tmpfile(); |
| 53 | 53 | if ($temp === false) { |
| 54 | - $this->tempname = $this->getConfig('save-path').'/log-'. microtime(true).'.tmp'; |
|
| 54 | + $this->tempname = $this->getConfig('save-path').'/log-'.microtime(true).'.tmp'; |
|
| 55 | 55 | $temp = fopen($this->tempname, 'w+'); |
| 56 | 56 | } |
| 57 | 57 | if ($temp !== false) { |
@@ -77,12 +77,12 @@ discard block |
||
| 77 | 77 | |
| 78 | 78 | protected function packLogFile() |
| 79 | 79 | { |
| 80 | - $logFile= $this->latest; |
|
| 81 | - $path=preg_replace('/[\\\\]+/', '/', $this->getConfig('save-zip-path') .'/'.date('Y-m-d').'.zip'); |
|
| 80 | + $logFile = $this->latest; |
|
| 81 | + $path = preg_replace('/[\\\\]+/', '/', $this->getConfig('save-zip-path').'/'.date('Y-m-d').'.zip'); |
|
| 82 | 82 | $zip = new ZipArchive; |
| 83 | 83 | $res = $zip->open($path, ZipArchive::CREATE); |
| 84 | 84 | if ($res === true) { |
| 85 | - if ($zip->addFile($logFile, date('Y-m-d'). '-'. $zip->numFiles .'.log')) { |
|
| 85 | + if ($zip->addFile($logFile, date('Y-m-d').'-'.$zip->numFiles.'.log')) { |
|
| 86 | 86 | array_push($this->removeFiles, $logFile); |
| 87 | 87 | } |
| 88 | 88 | if (\is_dir($this->getConfig('save-pack-path'))) { |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | $zip->close(); |
| 97 | 97 | } else { |
| 98 | 98 | if (is_file($logFile) && file_exists($logFile)) { |
| 99 | - rename($logFile, $this->getConfig('save-path') . '/' . date('Y-m-d'). '-'. substr(md5_file($logFile), 0, 8).'.log'); |
|
| 99 | + rename($logFile, $this->getConfig('save-path').'/'.date('Y-m-d').'-'.substr(md5_file($logFile), 0, 8).'.log'); |
|
| 100 | 100 | } |
| 101 | 101 | } |
| 102 | 102 | } |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | */ |
| 109 | 109 | protected function checkSize():bool |
| 110 | 110 | { |
| 111 | - $logFile= $this->latest; |
|
| 111 | + $logFile = $this->latest; |
|
| 112 | 112 | if (file_exists($logFile)) { |
| 113 | 113 | if (filesize($logFile) > $this->getConfig('max-file-size')) { |
| 114 | 114 | return true; |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | |
| 121 | 121 | public function log($level, string $message, array $context = []) |
| 122 | 122 | { |
| 123 | - if (LogLevel::compare($level, $this->getConfig('log-level')) >=0) { |
|
| 123 | + if (LogLevel::compare($level, $this->getConfig('log-level')) >= 0) { |
|
| 124 | 124 | $replace = []; |
| 125 | 125 | $message = $this->interpolate($message, $context); |
| 126 | 126 | $replace['%level%'] = $level; |
@@ -133,10 +133,10 @@ discard block |
||
| 133 | 133 | protected function rollLatest() |
| 134 | 134 | { |
| 135 | 135 | if (isset($this->latest)) { |
| 136 | - $size=ftell($this->temp); |
|
| 136 | + $size = ftell($this->temp); |
|
| 137 | 137 | fseek($this->temp, 0); |
| 138 | 138 | if ($size > 0) { |
| 139 | - $body=fread($this->temp, $size); |
|
| 139 | + $body = fread($this->temp, $size); |
|
| 140 | 140 | file_put_contents($this->latest, $body, FILE_APPEND); |
| 141 | 141 | } |
| 142 | 142 | fclose($this->temp); |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | { |
| 169 | 169 | $replace = []; |
| 170 | 170 | foreach ($context as $key => $val) { |
| 171 | - $replace['{' . $key . '}'] = $val; |
|
| 171 | + $replace['{'.$key.'}'] = $val; |
|
| 172 | 172 | } |
| 173 | 173 | return strtr($message, $replace); |
| 174 | 174 | } |
@@ -10,13 +10,13 @@ |
||
| 10 | 10 | { |
| 11 | 11 | public function log($level, string $message, array $context = []) |
| 12 | 12 | { |
| 13 | - print date('Y-m-d H:i:s') .' ' . $this->interpolate($message, $context) . PHP_EOL; |
|
| 13 | + print date('Y-m-d H:i:s').' '.$this->interpolate($message, $context).PHP_EOL; |
|
| 14 | 14 | } |
| 15 | 15 | public function interpolate(string $message, array $context) |
| 16 | 16 | { |
| 17 | 17 | $replace = []; |
| 18 | 18 | foreach ($context as $key => $val) { |
| 19 | - $replace['{' . $key . '}'] = $val; |
|
| 19 | + $replace['{'.$key.'}'] = $val; |
|
| 20 | 20 | } |
| 21 | 21 | return strtr($message, $replace); |
| 22 | 22 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace nebula\component\debug; |
| 3 | 3 | |
| 4 | -interface ConfigInterface { |
|
| 4 | +interface ConfigInterface { |
|
| 5 | 5 | public function applyConfig(array $config); |
| 6 | 6 | } |
@@ -20,7 +20,7 @@ |
||
| 20 | 20 | } |
| 21 | 21 | public function getConfig(string $name) { |
| 22 | 22 | $defaultConfig = $this->getDefaultConfig(); |
| 23 | - return $this->config[$name] ?? $defaultConfig[$name];; |
|
| 23 | + return $this->config[$name] ?? $defaultConfig[$name]; ; |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | abstract public function getDefaultConfig():array; |
@@ -15,14 +15,14 @@ |
||
| 15 | 15 | * @param int $expire 过期时间 |
| 16 | 16 | * @return bool |
| 17 | 17 | */ |
| 18 | - public function set(string $name, $value, int $expire=null):bool; |
|
| 18 | + public function set(string $name, $value, int $expire = null):bool; |
|
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * 获取值 |
| 22 | 22 | * @param string $name 名 |
| 23 | 23 | * @return mixed|null |
| 24 | 24 | */ |
| 25 | - public function get(string $name, $defalut=null); |
|
| 25 | + public function get(string $name, $defalut = null); |
|
| 26 | 26 | |
| 27 | 27 | /** |
| 28 | 28 | * 删除值 |
@@ -40,11 +40,11 @@ |
||
| 40 | 40 | $exstr = ''; |
| 41 | 41 | foreach ($array as $key => $value) { |
| 42 | 42 | $line = ''; |
| 43 | - $current=$arrname."['".addslashes($key)."']"; |
|
| 43 | + $current = $arrname."['".addslashes($key)."']"; |
|
| 44 | 44 | if (is_array($value)) { |
| 45 | 45 | $line .= self::parserArraySub($current, $value); |
| 46 | 46 | } else { |
| 47 | - $line = $current; |
|
| 47 | + $line = $current; |
|
| 48 | 48 | if (is_string($value)) { |
| 49 | 49 | $line .= "='".addslashes($value).'\';'.PHP_EOL; |
| 50 | 50 | } elseif (is_bool($value)) { |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | * @param mixed $def |
| 88 | 88 | * @return array 设置后的数组 |
| 89 | 89 | */ |
| 90 | - public static function set(array &$array, string $name, $value, $def=null):array |
|
| 90 | + public static function set(array &$array, string $name, $value, $def = null):array |
|
| 91 | 91 | { |
| 92 | 92 | $path = explode('.', $name); |
| 93 | 93 | $root = &$array; |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | $array[$key] = []; |
| 99 | 99 | } |
| 100 | 100 | } else { |
| 101 | - $array=[]; |
|
| 101 | + $array = []; |
|
| 102 | 102 | } |
| 103 | 103 | $array = &$array[$key]; |
| 104 | 104 | } |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | $array[$key] = []; |
| 122 | 122 | } |
| 123 | 123 | } else { |
| 124 | - $array=[]; |
|
| 124 | + $array = []; |
|
| 125 | 125 | } |
| 126 | 126 | $array = &$array[$key]; |
| 127 | 127 | } |