Skip to content

Commit

Permalink
Update logic for creating new log in x days
Browse files Browse the repository at this point in the history
  • Loading branch information
sleemy1997 committed Oct 2, 2018
1 parent f38f58e commit ed3399f
Showing 1 changed file with 17 additions and 124 deletions.
141 changes: 17 additions & 124 deletions FileLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,24 @@
* Time: 12:39 PM
*/

namespace apollo11\fileLogger;
namespace apollo11\cliLogger;


class FileLogger
class CliLogger
{
//log file creation types

const FILE_CREATE_TYPE_BY_TIME = 1;

const FILE_CREATE_TYPE_BY_SIZE = 2;


public $saveLatestFileNumber = 1;

//color option for log text
public $enableColors = true;

//log file creation type property
public $fileCreateType = self::FILE_CREATE_TYPE_BY_SIZE;

//Log file recreation units
public $fileReCreateMinutes = 1;
public $fileReCreateHours = 0;
public $fileReCreateDays = 0;
public $fileReCreateMonths = 0;
public $fileReCreateYears = 0;

//Log file recreation size
public $filReCreateSize = 900; //size in bytes
public $fileReCreateDays = 1;

public $filReCreateSize = 900;

// Log file attributes
public $logFilePath;
Expand Down Expand Up @@ -61,70 +51,52 @@ public function __construct($config)


/**
* Log message
*
* Log message with color parameters in log file
*
* @param $message
* @param string $fColor
* @param null $bColor
* @param string $type
* @return string
* @throws \Exception
*/
public function log($message, $fColor = FileColor::F_WHITE, $bColor = null, $type = 'LOG')
public function log($message, $fColor = CliColor::F_WHITE, $bColor = null, $type = 'LOG')
{
return $this->writeLog($this->processLogTextTemplate($message, $type), $fColor, $bColor);
}


/**
* Log error
*
* @param $message
* @param string $type
* @return string
* @throws \Exception
*/
public function error($message, $type = 'ERROR')
{
return $this->writeLog($this->processLogTextTemplate($message, $type), FileColor::F_RED);
return $this->writeLog($this->processLogTextTemplate($message, $type), CliColor::F_RED);
}


/**
* Log info
*
* @param $message
* @param string $type
* @return string
* @throws \Exception
*/
public function info($message, $type = 'INFO')
{
return $this->writeLog($this->processLogTextTemplate($message, $type), FileColor::F_LIGHT_BLUE);
return $this->writeLog($this->processLogTextTemplate($message, $type), CliColor::F_LIGHT_BLUE);
}


/**
* Log success
*
* @param $message
* @param string $type
* @return string
* @throws \Exception
*/
public function success($message, $type = 'SUCCESS')
{
return $this->writeLog($this->processLogTextTemplate($message, $type), FileColor::F_LIGHT_GREEN);
return $this->writeLog($this->processLogTextTemplate($message, $type), CliColor::F_LIGHT_GREEN);
}


/**
* Log write
*
* Write log message with given type and text color parameters in log file
*
* @param $message
* @param $fColor
* @param null $bColor
Expand All @@ -137,30 +109,18 @@ private function writeLog($message, $fColor, $bColor = null)
throw new \Exception('logFilePath is invalid');
}
if ($this->enableColors === true) {
$message = FileColor::getColoredString($message, $fColor, $bColor);
$message = CliColor::getColoredString($message, $fColor, $bColor);
}

$expiredLogFile = $this->checkFileCreation();

file_put_contents($this->logFilePath . '/' . $this->processFileTemplate($expiredLogFile), $message, FILE_APPEND);


if ($this->saveLatestFileNumber > 1) {
/*check old logs and delete them*/
$this->deleteOldLogs();

}


return $message;
}


/**
* Constructor configuration
*
* Returns configuration object for constructor
*
* @param $object
* @param $properties
* @return mixed
Expand All @@ -176,10 +136,6 @@ private function configure($object, $properties)


/**
* Log file template
*
* Returns template for log file with chosen configuration
*
* @param $expiredLogFile
* @return bool|mixed|string
*/
Expand All @@ -199,28 +155,23 @@ private function processFileTemplate($expiredLogFile)
}

if ($expiredLogFile) {

$pathInfo = pathinfo($fileName);
if ($this->fileCreateType === self::FILE_CREATE_TYPE_BY_SIZE) {
if ($fileName === $expiredLogFile) {
$fileName = $expiredLogFile = $pathInfo['filename'] . '_' . time() . '.' . $pathInfo['extension'];
} else {
$expiredLogFile = false;
}
} else {
} elseif ($this->fileCreateType === self::FILE_CREATE_TYPE_BY_TIME) {
$expiredLogFile = false;
}
}


return $expiredLogFile ?: $fileName;
}


/**
* Log text template
*
* Returns template for log text with chosen configuration
* @param $message
* @param string $type
* @return string
Expand All @@ -237,77 +188,23 @@ private function processLogTextTemplate($message, $type = 'LOG')
}


function deleteOldLogs()
{
if (is_dir($this->logFilePath)) {

$logFilePath = $this->logFilePath;
$logFileName = $this->logFileName;
$files = glob("${$logFilePath}/*.${$logFileName}");
$allFilesArray = [];

foreach ($files as $key => $file) {
$allFilesArray[$key]['time'] = filemtime($this->logFilePath . '/' . $file);
$allFilesArray[$key]['name'] = $file;
}

usort($allFilesArray, function ($a, $b) {
return $b['time'] - $a['time'];
});

if (count($allFilesArray) > $this->saveLatestFileNumber && $this->saveLatestFileNumber > 1) {
for ($i = 0; $i < $this->saveLatestFileNumber; $i++) {
if ($allFilesArray[$i]['time'] >= date($this->logFileDateFormat)) {
unset($allFilesArray[$i]);
}
}

foreach ($allFilesArray as $deteFileNames) {
$file = $this->logFilePath . '/' . $deteFileNames['name'];
if (file_exists($file)) {
unlink($file);
echo 'Deleted file: Time:' . $deteFileNames['time'] . " Name: " . $deteFileNames['name'] . "<br>";
}
}
}


}
}


/**
* File creation check
*
* Function checks if log file was created according FILE_CREATE_TYPE option
*
* Returns log file name or boolean(false)
*
* @return bool|mixed
*/
private function checkFileCreation()
{

$logFileName = $this->getLatestLogFile();
$logFilePath = $this->logFilePath . '/' . $logFileName;
$lastModified = filemtime($logFilePath);
$ex = time() - $lastModified;
$t = (
($this->fileReCreateMinutes * 60)
+ ($this->fileReCreateHours * 3600)
+ ($this->fileReCreateDays * 86400)
+ ($this->fileReCreateMonths * 2592000)
+ ($this->fileReCreateYears * 31536000)
);

if ($this->fileCreateType === self::FILE_CREATE_TYPE_BY_SIZE) {
if (file_exists($logFilePath) && filesize($logFilePath) >= $this->filReCreateSize) {
return $logFileName;
}
} else {
} elseif ($this->fileCreateType === self::FILE_CREATE_TYPE_BY_TIME) {
// TODO get first create date from file

if (file_exists($logFilePath) && ($ex >= $t)) {
$lasElementInDir = count(scandir($this->logFilePath));
$lastModified = filemtime($logFilePath);
$lastModifiedLogFileDate = strtotime(explode('_', scandir($this->logFilePath)[$lasElementInDir - 1])[0] . "+" . $this->fileReCreateDays . " day");
if (file_exists($logFilePath) && $lastModified >= $lastModifiedLogFileDate) {
return $logFileName;
}
}
Expand All @@ -317,10 +214,6 @@ private function checkFileCreation()


/**
* Get latest log
*
* Return latest log file from log directory
*
* @return bool|mixed
*/
private function getLatestLogFile()
Expand Down

0 comments on commit ed3399f

Please sign in to comment.