2020-05-16 15:18:27 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
class LogRecordType{
|
|
|
|
const WARNING = 'warning';
|
|
|
|
const ERROR = 'error';
|
|
|
|
const INFO = 'info';
|
|
|
|
}
|
|
|
|
|
|
|
|
class LogKeeper
|
|
|
|
{
|
2020-07-20 17:15:23 +00:00
|
|
|
function cleaningDir ($dir, $seconds) {
|
|
|
|
$todayFileName = date ("Y-m-d").'.log';
|
|
|
|
$logFiles = scandir ($dir);
|
2020-05-16 15:18:27 +00:00
|
|
|
foreach ($logFiles as $key => $file) {
|
2020-07-20 17:15:23 +00:00
|
|
|
if (in_array ($file,array (".", "..", ".gitkeep", $todayFileName)))
|
2020-05-16 15:18:27 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2020-07-20 17:15:23 +00:00
|
|
|
if (!is_dir($dir . $file)) {
|
|
|
|
if (strtotime(str_replace(".log", "", $file)) < (strtotime("now") - $seconds)) {
|
|
|
|
unlink ($dir . $file);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->cleaningDir ($path . $file . "/", $seconds);
|
2020-05-16 15:18:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-20 17:15:23 +00:00
|
|
|
|
|
|
|
function purge ($days) {
|
|
|
|
$seconds = $days * 86400;
|
|
|
|
$this->cleaningDir ('../logs/', $seconds);
|
|
|
|
}
|
2020-05-16 15:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class LogManager
|
|
|
|
{
|
|
|
|
|
|
|
|
private $logFile;
|
|
|
|
function __construct($fileName = "")
|
|
|
|
{
|
|
|
|
if ($fileName == ""){
|
|
|
|
$fileName = '../logs/'. date("Y-m-d").'.log';
|
|
|
|
}
|
|
|
|
if(!is_dir("../logs/"))
|
|
|
|
{
|
|
|
|
mkdir("../logs/");
|
|
|
|
}
|
|
|
|
$this->logFile = fopen($fileName, "a") or die("Unable to open file!");
|
|
|
|
}
|
|
|
|
|
|
|
|
function write($value, $type = LogRecordType::ERROR){
|
|
|
|
$record = "[".date("H:m:s")."][".$type."]" . $value . "\n";
|
|
|
|
fwrite($this->logFile, $record);
|
|
|
|
}
|
|
|
|
|
|
|
|
function __destruct(){
|
|
|
|
if (isset($this->logFile)) {
|
|
|
|
fclose($this->logFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|