logFile = $logFile; $this->printLog = $printLog; if (function_exists('ini_set')) { ini_set('log_errors', true); } } /** * logging * * @param string $message * @param int $level * @return void */ private function logging(string $message, int $level): void { $date = (new DateTime())->format('d-M-Y H:i:s \U\T\C'); $levelString = match ($level) { self::DEBUG => 'DEBUG', self::INFO => 'INFO', self::WARNING => 'WARNING', self::ERROR => 'ERROR', self::FATAL => 'FATAL ERROR', default => throw new Exception('Invalid level type!') }; $log = sprintf("[%s] [%s] %s\n", $date, $levelString, $message); if ($this->printLog) { print($log); } file_put_contents($this->logFile, $log, FILE_APPEND | LOCK_EX); if ($level === self::FATAL) { throw new Exception($message); } } /** * debug * * @param string $message * @return void */ public function debug(string $message): void { $this->logging($message, self::DEBUG); } /** * info * * @param string $message * @return void */ public function info(string $message): void { $this->logging($message, self::INFO); } /** * warning * * @param string $message * @return void */ public function warning(string $message): void { $this->logging($message, self::WARNING); } /** * error * * @param string $message * @return void */ public function error(string $message): void { $this->logging($message, self::ERROR); } /** * fatal * * @param string $message * @return void */ public function fatal(string $message): void { $this->logging($message, self::FATAL); } }