Skip to content

Instantly share code, notes, and snippets.

@dastanaron
Last active January 29, 2018 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dastanaron/dcca4301acc903771d33ae03d4eaa862 to your computer and use it in GitHub Desktop.
Save dastanaron/dcca4301acc903771d33ae03d4eaa862 to your computer and use it in GitHub Desktop.
Logger
<?php
namespace console\components;
/**
* Class Logger
* The component of logging the script execution. Good for console applications
* @package console\components
*/
class Logger {
/**
* @var string
*/
public $logname;
/**
* @var string
*/
public $log_dir;
/**
* @var string
*/
public $string;
/**
* @var int
*/
public $logsize = 1048576; //1 MB
/**
* Logger constructor.
* @param string $logname
* @param string $log_dir
*/
public function __construct($logname = 'log.log', $log_dir = 'logs/')
{
$this->log_dir = $log_dir;
$this->logname = $logname;
}
/**
* The method of logging a string.
* Gets a string, adds time to it, and writes it to the log file.
* If you need to output a logical string, you must pass the second parameter to true.
* @example $log= new Logger(); $log->Log('hello world', true);
* @param $string
* @param bool $print
* @return bool
*/
public function Log ($string ,$print=false) {
$this->string = $string;
if (!file_exists ($this->getLogDir())) {
mkdir ($this->getLogDir(), 0755);
}
if (!$this->ValidateString()) {
$this->ErrorLogWrite('Ошибка, валидация строки не пройдена');
return false;
}
$log = $this->Logname();
$this->string = date('Y-m-d H:i:s') . '| ' . $this->string . "|" . PHP_EOL;
if(file_put_contents($log, $this->string, LOCK_EX | FILE_APPEND)) {
return true;
}
if ($print === true) {
echo $this->string;
}
}
private function Logname() {
$log = $this->getLogDir() . $this->logname;
if (file_exists($log) && filesize($log) > $this->logsize) {
$countlog = $this->logswitcher()+1;
if($countlog === false) {
$countlog = 1;
}
$old_log = $log . '.' . $countlog;
rename($log, $old_log);
}
return $log;
}
private function logswitcher()
{
$logFiles = scandir($this->getLogDir());
$countsArray = array();
foreach($logFiles as $file) {
if($file != '.' || $file != '..') {
if(preg_match('#(\d+)$#', $file, $match)) $countsArray[] = $match[1];
}
}
return !empty($countsArray) ? max($countsArray) : false;
}
private function ValidateString() {
if (empty($this->string)) {
$this->ErrorLogWrite('Ошибка, обязательный параметр пустой.');
return false;
}
elseif (is_array($this->string) || is_object($this->string)) {
$this->ErrorLogWrite('Ошибка, в параметр string передан массив или объект.');
return false;
}
else {
return true;
}
}
private function ErrorLogWrite($errorstring) {
$errorlog = 'error.log';
if(file_put_contents($errorlog, $errorstring, LOCK_EX | FILE_APPEND)) {
return true;
}else {
return false;
}
}
/**
* @return string
*/
public function getLogDir() {
return $this->log_dir;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment