| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace nebula\component\debug\log\logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use nebula\component\debug\log\LogLevel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use nebula\component\debug\log\AbstractLogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use nebula\component\debug\log\logger\filelogger\AttachTrait; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use nebula\component\debug\log\logger\filelogger\AttachAttribute; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class FileLogger extends AbstractLogger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * 配置信息 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $config  = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         'save-path' => './logs', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         'save-zip-path' => './logs/zip', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         'save-pack-path' => './logs/dump', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         'max-file-size' => 2097152, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         'file-name' => 'latest.log', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         'log-level' => 'debug', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         'log-format' => '[%level%] %message%', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * 文件 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     protected $temp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * 临时文件名 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected $tempname; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * 移除文件 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     protected $removeFiles = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * 最后的日志 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     protected $latest; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     public function __construct(array $config=[]) | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $this->applyConfig($config); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $this->temp = tmpfile(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         if ($this->temp === false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             $this->tempname = $this->config['save-path'].'/log-'. microtime(true).'.tmp'; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             $this->temp = fopen($this->tempname, 'w+'); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         $this->latest = $this->config['save-path'].'/'.$this->config['file-name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     protected function packLogFile() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $logFile= $this->latest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $path=preg_replace('/[\\\\]+/', '/', $this->config['save-zip-path'] .'/'.date('Y-m-d').'.zip'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $zip = new ZipArchive; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $res = $zip->open($path, ZipArchive::CREATE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if ($res === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             if ($zip->addFile($logFile, date('Y-m-d'). '-'. $zip->numFiles .'.log')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 array_push($this->removeFiles, $logFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $it = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->config['save-pack-path'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             foreach ($it as $dumpLog) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 if ($zip->addFile($dumpLog, 'pack/'.basename($dumpLog))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                     array_push($this->removeFiles, $dumpLog); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $zip->close(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             if (is_file($logFile) && file_exists($logFile)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 rename($logFile, $this->config['save-path'] . '/' . date('Y-m-d'). '-'. substr(md5_file($logFile), 0, 8).'.log'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public function applyConfig(array $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         foreach ($config as $name => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             if (in_array($name, array_keys($this->config))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 $this->config[$name] = $config[$name] ?? $this->config[$name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 throw new \Exception(sprintf(__CLASS__.' : unknown config property %s', $name)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * 检查日志文件大小 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     protected function checkSize():bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $logFile= $this->latest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         if (file_exists($logFile)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             if (filesize($logFile) > $this->config['max-file-size']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     public function log($level, string $message, array $context = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         if (LogLevel::compare($level, $this->config['log-level']) >=0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $replace = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             $message = $this->interpolate($message, $context); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             $replace['%level%'] = $level; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             $replace['%message%'] = $message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $write = \strtr($this->config['log-format'], $replace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             \fwrite($this->temp, $write.PHP_EOL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     public function interpolate(string $message, array $context) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         $replace = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         foreach ($context as $key => $val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             $replace['{' . $key . '}'] = $val; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         return strtr($message, $replace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     protected function rollLatest() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         if (isset($this->latest)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             $size=ftell($this->temp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             fseek($this->temp, 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             if ($size > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 $body=fread($this->temp, $size); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                 file_put_contents($this->latest, $body, FILE_APPEND); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             fclose($this->temp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             if (isset($this->tempname)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                 unlink($this->tempname); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     protected function removePackFiles() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         foreach ($this->removeFiles as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |             if (\is_file($file) && \file_exists($file)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                 unlink($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     public function save() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         if ($this->checkSize()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             $this->packLogFile(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         $this->rollLatest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         $this->removePackFiles(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 174 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |  | 
            
                        
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths