runtimeLogPath = runtime_path() . '/logs/'; $this->channelDirName = $channelDirName; $this->level=$level; $dateDir = date('Ym').'/'; $filename = date('d') .'.log'; $fullFilePath = empty($channelDirName) ? $this->runtimeLogPath . $dateDir .$filename : $this->runtimeLogPath . $this->channelDirName . '/' . $dateDir . $filename; $this->maxFileSize = (int)$maxFileSize; if ($maxFileSize <= 0) { throw new \Exception('Max file size must be higher than 0'); } parent::__construct($fullFilePath, $level, $bubble, $filePermission, $useLocking); } /** * {@inheritdoc} */ public function close(): void { parent::close(); if ($this->mustRotate) { $this->rotate(); } } /** * {@inheritdoc} */ public function reset() { parent::reset(); if ($this->mustRotate) { $this->rotate(); } } /** * {@inheritdoc} */ protected function write(array $record): void { $dateDir = date('Ym') . '/'; $logBasePath = empty($this->channelDirName) ? $this->runtimeLogPath . $dateDir : $this->runtimeLogPath . $this->channelDirName . '/' . $dateDir; $fullLogFilename = $logBasePath . date('d').'.log'; clearstatcache(true, $fullLogFilename); if (file_exists($fullLogFilename)) { $fileSize = filesize($fullLogFilename); if ($fileSize >= $this->maxFileSize) { $this->mustRotate = true; $this->close(); }else{ $this->stream = null; $this->url = $fullLogFilename; } }else{ // 解决WebMan启动后删除日志文件无法写入的问题 $this->mustRotate = true; $this->close(); } parent::write($record); } /** * Rotates the files. */ protected function rotate() { // skip GC of old logs if file size is unlimited if ($this->maxFileSize === 0) { return; } $dateDir = date('Ym') . '/'; $logBasePath = empty($this->channelDirName) ? $this->runtimeLogPath . $dateDir : $this->runtimeLogPath . $this->channelDirName . '/' . $dateDir; if($this->level==200){ $filename = date('d').'.log'; }else{ $filename = date('d').'_error.log'; } $fullLogFilename = $logBasePath . $filename; // archive latest file clearstatcache(true, $fullLogFilename); if (file_exists($fullLogFilename)) { $target = $logBasePath. $filename; rename($fullLogFilename, $target); }else{ if (!is_dir($logBasePath)) { mkdir($logBasePath,0755,true); } $this->url = $fullLogFilename; } $this->mustRotate = false; } }