diff --git a/CliColor.php b/FileColor.php similarity index 97% rename from CliColor.php rename to FileColor.php index ce284f4..5cc28d7 100644 --- a/CliColor.php +++ b/FileColor.php @@ -6,10 +6,10 @@ * Time: 3:17 PM */ -namespace apollo11\cliLogger; +namespace apollo11\fileLogger; -class CliColor +class FileColor { // Text color options for log text const F_BLACK = '0;30'; diff --git a/CliLogger.php b/FileLogger.php similarity index 90% rename from CliLogger.php rename to FileLogger.php index accc12f..869dd78 100644 --- a/CliLogger.php +++ b/FileLogger.php @@ -6,10 +6,10 @@ * Time: 12:39 PM */ -namespace apollo11\cliLogger; +namespace apollo11\fileLogger; -class CliLogger +class FileLogger { //log file creation types const FILE_CREATE_TYPE_BY_TIME = 1; @@ -72,7 +72,7 @@ public function __construct($config) * @return string * @throws \Exception */ - public function log($message, $fColor = CliColor::F_WHITE, $bColor = null, $type = 'LOG') + public function log($message, $fColor = FileColor::F_WHITE, $bColor = null, $type = 'LOG') { return $this->writeLog($this->processLogTextTemplate($message, $type), $fColor, $bColor); } @@ -88,7 +88,7 @@ public function log($message, $fColor = CliColor::F_WHITE, $bColor = null, $type */ public function error($message, $type = 'ERROR') { - return $this->writeLog($this->processLogTextTemplate($message, $type), CliColor::F_RED); + return $this->writeLog($this->processLogTextTemplate($message, $type), FileColor::F_RED); } @@ -102,7 +102,7 @@ public function error($message, $type = 'ERROR') */ public function info($message, $type = 'INFO') { - return $this->writeLog($this->processLogTextTemplate($message, $type), CliColor::F_LIGHT_BLUE); + return $this->writeLog($this->processLogTextTemplate($message, $type), FileColor::F_LIGHT_BLUE); } @@ -116,7 +116,7 @@ public function info($message, $type = 'INFO') */ public function success($message, $type = 'SUCCESS') { - return $this->writeLog($this->processLogTextTemplate($message, $type), CliColor::F_LIGHT_GREEN); + return $this->writeLog($this->processLogTextTemplate($message, $type), FileColor::F_LIGHT_GREEN); } @@ -137,7 +137,7 @@ private function writeLog($message, $fColor, $bColor = null) throw new \Exception('logFilePath is invalid'); } if ($this->enableColors === true) { - $message = CliColor::getColoredString($message, $fColor, $bColor); + $message = FileColor::getColoredString($message, $fColor, $bColor); } $expiredLogFile = $this->checkFileCreation(); @@ -287,27 +287,16 @@ function deleteOldLogs() */ 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 { - // TODO get first create date from file - - if (file_exists($logFilePath) && ($ex >= $t)) { + } elseif ($this->fileCreateType === self::FILE_CREATE_TYPE_BY_TIME) { + $lasElementInDir = count(scandir($this->logFilePath)); + $lastModifiedLogFileDate = strtotime(explode('_', scandir($this->logFilePath)[$lasElementInDir - 1])[0] . '+' . $this->fileReCreateDays . ' day'); + if (file_exists($logFilePath) && strtotime(date($this->logFileDateFormat)) >= $lastModifiedLogFileDate) { return $logFileName; } } @@ -315,7 +304,6 @@ private function checkFileCreation() return false; } - /** * Get latest log * diff --git a/README.md b/README.md index e8f4b1d..8a23a81 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# cli-logger +# File-logger Installation ------------ @@ -8,13 +8,13 @@ The preferred way to install this extension is through [composer](http://getcomp Either run ``` -php composer.phar require --prefer-dist apollo11/cli-logger "*" +php composer.phar require --prefer-dist apollo11/file-logger "*" ``` or add ``` -"apollo11/cli-logger": "*" +"apollo11/file-logger": "*" ``` to the require section of your `composer.json` file. @@ -22,10 +22,10 @@ to the require section of your `composer.json` file. ## Basic usage ## ``` -include_once 'CliColor.php'; -include_once 'CliLogger.php'; +include_once 'FileColor.php'; +include_once 'FileLogger.php'; -$logger = new \apollo11\cliLogger\CliLogger([ +$logger = new \apollo11\fileLogger\FileLogger([ 'logFilePath' => dirname(__FILE__) . '/logs', 'logFileName' => 'test12.log', 'logFileDateFormat' => "Y_m_d_H_i_s", @@ -35,7 +35,7 @@ $logger = new \apollo11\cliLogger\CliLogger([ ]); for ($i = 0; $i < 20; $i++){ - $logger->log("My test $i", \apollo11\cliLogger\CliColor::F_WHITE, \apollo11\cliLogger\CliColor::B_GREEN); + $logger->log("My test $i", \apollo11\fileLogger\FileColor::F_WHITE, \apollo11\fileLogger\FileColor::B_GREEN); } ``` @@ -58,16 +58,13 @@ enableColors //colored text for logs , deafult value true fileCreateType //log file creation type , default value "FILE_CREATE_TYPE_BY_TIME" ``` -
Log file recreation units
+
Log file recreation days
``` -Add one of this properties if file recreation type set to "FILE_CREATE_TYPE_BY_TIME" +Add this properties if file recreation type set to "FILE_CREATE_TYPE_BY_TIME" -fileReCreateMinutes // (integer) -fileReCreateHours // (integer) fileReCreateDays // (integer) -fileReCreateMonths // (integer) -fileReCreateYears // (integer) + ```
Log file recreation size
diff --git a/composer.json b/composer.json index fd7432f..8a83222 100644 --- a/composer.json +++ b/composer.json @@ -1,16 +1,20 @@ { - "name": "apollo11/cli-logger", - "description": "Log any cli errors", + "name": "apollo11/file-logger", + "description": "Log any errors in file", "license": "Apache-2.0", "type": "library", "authors": [ { "name": "Giorgi Keshikashvili", "email": "keshikashviligio@gmail.com" + }, + { + "name": "Levan Khvedelidze", + "email": "levani19972@gmail.com" } ], "autoload": { - "psr-4": { "apollo11\\cliLogger\\": "" } + "psr-4": { "apollo11\\fileLogger\\": "" } }, "require": { "php": ">=5.6"