stream = $stream; $this->dir = dirname($this->stream); $this->createDir(); } /** * write log data into to file * @param array $log */ public function write(array $log){ $log = (string)json_encode($log, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); if( !empty($log) ){ $stream = fopen($this->stream, 'a'); flock($stream, LOCK_EX); fwrite($stream, $log . PHP_EOL); flock($stream, LOCK_UN); fclose($stream); } } /** * create directory */ private function createDir(){ // Do not try to create dir if it has already been tried. if ($this->dirCreated){ return; } if ($this->dir && !is_dir($this->dir)){ $status = mkdir($this->dir, 0777, true); if (false === $status) { throw new \UnexpectedValueException(sprintf(self::ERROR_DIR_CREATE, $this->dir)); } } $this->dirCreated = true; } }