2020-05-16 15:18:27 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-07-28 07:02:46 +00:00
|
|
|
|
2020-05-16 15:18:27 +00:00
|
|
|
|
|
|
|
class LogManager
|
|
|
|
{
|
|
|
|
private $logFile;
|
2021-04-05 08:46:27 +00:00
|
|
|
private $filePath = null;
|
2020-07-28 07:02:46 +00:00
|
|
|
private $logLevel = 1;
|
|
|
|
|
|
|
|
public function __construct($fileName = "")
|
2020-05-16 15:18:27 +00:00
|
|
|
{
|
|
|
|
if ($fileName == ""){
|
|
|
|
$fileName = '../logs/'. date("Y-m-d").'.log';
|
|
|
|
}
|
2021-04-05 08:46:27 +00:00
|
|
|
|
2020-05-16 15:18:27 +00:00
|
|
|
if(!is_dir("../logs/"))
|
|
|
|
{
|
|
|
|
mkdir("../logs/");
|
|
|
|
}
|
2020-07-28 07:02:46 +00:00
|
|
|
|
2021-04-05 08:46:27 +00:00
|
|
|
$this->filePath = $fileName;
|
2020-05-16 15:18:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 07:02:46 +00:00
|
|
|
public function setLevel($type = LogRecordTypess::WARNING){
|
2020-07-29 06:44:11 +00:00
|
|
|
$this->logLevel = $type['level'];
|
2020-07-28 07:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function write($value, $type = LogRecordTypess::ERROR){
|
2021-04-05 08:46:27 +00:00
|
|
|
if ($this->logFile == null) {
|
|
|
|
$this->logFile = fopen($this->filePath, "a") or die("Unable to open file!");
|
|
|
|
}
|
|
|
|
|
2020-07-28 07:02:46 +00:00
|
|
|
if ($type['level'] <= $this->logLevel) {
|
|
|
|
$record = "[".date("H:m:s")."][".$type['identifier']."]" . $value . "\n";
|
|
|
|
fwrite($this->logFile, $record);
|
|
|
|
}
|
2020-05-16 15:18:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 07:02:46 +00:00
|
|
|
public function __destruct(){
|
2020-09-03 20:15:59 +00:00
|
|
|
if (isset($this->logFile) && $this->logFile != "Unable to open file!") {
|
2020-05-16 15:18:27 +00:00
|
|
|
fclose($this->logFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|