|
Final class LOGS {
Private $level;
Private $maxFileNum;
Private $maxFileSize;
Private $logPath;
Private $file;
The level of the log Debug,msg,err
Const LOGS_DEBUG = 0;
Const LOGS_MSG = 1;
Const LOGS_ERR = 2;
private static $instance = null;
Private Function __construct () {}
public static function getinstance ()
{
if (self:: $instance = = null)
{
Self:: $instance = new self ();
}
Return self:: $instance;
}
/**
* @Desc Initialization
* @Param $level int Record level
* @Param $maxNum int maximum number of log files
* @Param $maxSize int maximum log file size
* @Param save path $logPath string log file
* @Param $file string log file name prefix
* @Return Boolean
*/
Public function init ($level, $maxNum, $maxSize, $logPath, $file)
{
$level = Intval ($level);
$maxNum = Intval ($maxNum);
$maxSize = Intval ($maxSize);
!is_dir ($logPath) && mkdir ($logPath, 0777, true);
if (!in_array ($level, Array (self::logs_debug, self::logs_msg, self::logs_err)) | | $maxNum <= 0 | | $maxSize <= 0 | |!i S_dir ($logPath))
{
return false;
}
$this->level = $level;
$this->maxfilenum = $maxNum;
$this->maxfilesize = $maxSize;
$this->logpath = $logPath;
$this->file = $file;
return true;
}
/**
* @Desc Get formatted time string
*/
Public Function Formattime ()
{
$ustime = Explode ("", Microtime ());
Return "[". Date (' y-m-d h:i:s ', Time ()). ".". ($ustime [0] * 1000). "]";
}
/**
* Log files are recorded @Desc scrolling way
*/
Public function log ($STR)
{
$path = $this->logpath.directory_separator. $this->file. ". Log ";
Clearstatcache ();
if (file_exists ($path))
{
if (FileSize ($path) >= $this->maxfilesize)
{
$index = 1;
Get the maximum number of scroll logs
for (; $index < $this->maxfilenum; $index + +)
{
if (!file_exists ($this->logpath.directory_separator. $this.->file. " _ ". $index.". Log "))
{
Break
}
}
There are already maxfilenum log files.
if ($index = = $this->maxfilenum)
{
$index--;
}
Scrolling log
for (; $index > 1; $index-)
{
$new = $this->logpath.directory_separator. $this->file. " _ ". $index.". Log ";
$old = $this->logpath.directory_separator. $this->file. " _". ($index-1). ". Log ";
Rename ($old, $new);
}
$newFile = $this->logpath.directory_separator. $this->file. " _1.log ";
Rename ($path, $newFile);
}
}
$fp = fopen ($path, "a+b");
Fwrite ($fp, $str, strlen ($STR));
Fclose ($FP);
return true;
}
/**
* @Desc Record Debug information
* @Param string log information
* @Param the file where the string log is located
* @Param string log is in the row
*/
Public Function Debug ($msg, $file, $line)
{
if ($this->level <= self::logs_debug)
{
$this->log ($this->formattime (). " [{$file}:{$line}] DEBUG: ${msg}\n ");
}
}
/**
* @Desc Record information
* @Param string log information
* @Param the file where the string log is located
* @Param string log is in the row
*/
Public Function msg ($msg, $file, $line)
{
if ($this->level <= self::logs_msg)
{
$this->log ($this->formattime (). " [{$file}:{$line}] MSG: ${msg}\n ");
}
}
/**
* @Desc Log error messages
* @Param string log information
* @Param the file where the string log is located
* @Param string log is in the row
*/
Public Function Err ($msg, $file, $line)
{
if ($this->level <= self::logs_err)
{
$this->log ($this->formattime (). " [{$file}:{$line}] ERR: ${msg}\n ");
}
}
}
|