PHP_SMART_HOME_V3/app/class/LogManager.php

61 lines
1.1 KiB
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;
$logFiles = scandir('./app/logs/');
foreach ($logFiles as $key => $file) {
if (in_array($file,array(".","..", ".gitkeep", $todayFileName)))
{
continue;
}
if (filemtime($file) > $seconds) {
unlink('./app/logs/'.$file);
}
}
}
}
2019-08-30 15:29:34 +00:00
class LogManager
{
private $logFile;
function __construct($fileName = "")
{
if ($fileName == ""){
2019-09-19 12:48:31 +00:00
$fileName = './app/logs/'. date("Y-m-d").'.log';
2019-08-30 15:29:34 +00:00
}
2019-09-19 12:48:31 +00:00
if(!is_dir("./app/logs/"))
2019-08-30 15:29:34 +00:00
{
2019-09-19 12:48:31 +00:00
mkdir("./app/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";
2020-03-16 13:52:36 +00:00
if (strlen($record) > 65 ) {
$record = Utilities::stringInsert($record,"\n",65);
}
2019-08-30 15:29:34 +00:00
fwrite($this->logFile, $record);
}
function __destruct(){
if (isset($this->logFile)) {
fclose($this->logFile);
}
}
}