PHP_SMART_HOME_V3/app/models/LogManager.php

58 lines
1014 B
PHP
Raw Normal View History

2019-08-30 15:29:34 +00:00
<?php
/**
*
*/
class LogRecordType{
const WARNING = 'warning';
const ERROR = 'error';
const INFO = 'info';
}
2020-02-18 20:30:44 +00:00
class LogKeeper
{
function purge($days){
$todayFileName = date("Y-m-d").'.log';
$seconds = $days * 86400;
2020-04-28 09:52:14 +00:00
$logFiles = scandir('../logs/');
2020-02-18 20:30:44 +00:00
foreach ($logFiles as $key => $file) {
if (in_array($file,array(".","..", ".gitkeep", $todayFileName)))
{
continue;
}
if (filemtime($file) > $seconds) {
2020-04-28 09:52:14 +00:00
unlink('../logs/'.$file);
2020-02-18 20:30:44 +00:00
}
}
}
}
2019-08-30 15:29:34 +00:00
class LogManager
{
private $logFile;
function __construct($fileName = "")
{
if ($fileName == ""){
2020-04-28 09:52:14 +00:00
$fileName = '../logs/'. date("Y-m-d").'.log';
2019-08-30 15:29:34 +00:00
}
2020-04-28 09:52:14 +00:00
if(!is_dir("../logs/"))
2019-08-30 15:29:34 +00:00
{
2020-04-28 09:52:14 +00:00
mkdir("../logs/");
2019-08-30 15:29:34 +00:00
}
$this->logFile = fopen($fileName, "a") or die("Unable to open file!");
}
function write($value, $type = LogRecordType::ERROR){
2019-09-19 12:48:31 +00:00
$record = "[".date("H:m:s")."][".$type."]" . $value . "\n";
2019-08-30 15:29:34 +00:00
fwrite($this->logFile, $record);
}
function __destruct(){
if (isset($this->logFile)) {
fclose($this->logFile);
}
}
}