logType) { throw new RuntimeException('log type is null'); } return $this->logType; } /** * @return $this */ public function setLogType(string $logType): static { if (! in_array($logType, self::ALLOWED_LOG_TYPES)) { throw new RuntimeException('undefined log type: ' . $logType); } $this->logType = $logType; return $this; } public function log(string $data): void { $prefix = $this->getPrefix(); $message = sprintf( '%s: %s%s', date($this->getDateFormat()), empty($prefix) ? '' : $prefix . ' ', $data . PHP_EOL ); if ('file' === $this->logType) { file_put_contents(self::LOG_FILE, $message, FILE_APPEND); } elseif ('error_log' === $this->logType) { /* @noinspection ForgottenDebugOutputInspection */ error_log($message, 3, self::LOG_FILE); } } public function getPrefix(): ?string { return $this->prefix; } /** * @return $this */ public function setPrefix(?string $prefix): static { $this->prefix = $prefix; return $this; } public function getDateFormat(): string { if (! $this->dateFormat) { throw new RuntimeException('date format is null'); } return $this->dateFormat; } /** * @return $this */ public function setDateFormat(string $dateFormat): static { $this->dateFormat = $dateFormat; return $this; } private function __clone() {} } ///*$message = 'Message from delivery service'; // //$logger = new Logger(); //$dateFormat = 'Y-m-d H:i:s' . (preg_match('/^win/i', PHP_OS) ? '' : '.u'); //$logger->setDateFormat($dateFormat)->setLogType('error_log'); //$logger->log($message);*/