<?php

namespace Planaday\Utility\Debug\Log;

use DateTime;

class ErrorLogFileSaver implements LogListener {
	public function addLogToFile( LogEntry $entry ){
		file_put_contents($this->getCurrentFile(), $entry->__toString() . "\n", FILE_APPEND);
	}

	public function handleLog( LogEntry $entry ): void {
		if ( $entry->getType() === "error" )
			$this->addLogToFile($entry);
	}

	public function getCurrentFile(): string {
		$logFolder = dirname( __DIR__, 5 ) . "/assets/logs/" . (new DateTime())->format('Y/m/');
		if(!is_dir($logFolder)) mkdir($logFolder, 0777, true);
		return $logFolder . (new DateTime())->format('Y-m-d') . ".log";
	}
}