Final class LOGS { Private $ level; Private $ maxFileNum; Private $ maxFileSize; Private $ logPath; Private $ file; // Log level 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 $ logPath string log file storage path * @ Param $ file string the prefix of the log file name * @ 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 |! Is_dir ($ logPath )) { Return false; } $ This-> level = $ level; $ This-> maxFileNum = $ maxNum; $ This-> maxFileSize = $ maxSize; $ This-> logPath = $ logPath; $ This-> file = $ file; Return true; } /** * @ Desc get the formatted time string */ Public function formatTime () { $ Ustime = explode ("", microtime ()); Return "[". date ('Y-m-d H: I: S', time ()). ". ". ($ ustime [0] * 1000 ). "]"; } /** * @ Desc record log files in scroll mode */ Public function log ($ str) { $ Path = $ this-> logPath. DIRECTORY_SEPARATOR. $ this-> file. ". log "; Clearstatcache (); If (file_exists ($ path )) { If (filesize ($ path) >=$ this-> maxFileSize) { $ Index = 1; // Obtain the maximum number of rolling logs For (; $ index <$ this-> maxFileNum; $ index ++) { If (! File_exists ($ this-> logPath. DIRECTORY_SEPARATOR. $ this-> file. "_". $ index. ". log ")) { Break; } } // MaxFileNum log files already exist If ($ index = $ this-> maxFileNum) { $ Index --; } // Scroll logs 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 debugging information * @ Param string log information * @ Param string the file where the log is located * @ Param string the row of the log */ 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 string the file where the log is located * @ Param string the row of the log */ Public function msg ($ msg, $ file, $ line) { If ($ this-> level <= self: LOGS_MSG) { $ This-> log ($ this-> formatTime (). "[{$ file }:{ $ line}] MSG :$ {msg} \ n "); } } /** * @ Desc record the error message * @ Param string log information * @ Param string the file where the log is located * @ Param string the row of the log */ Public function err ($ msg, $ file, $ line) { If ($ this-> level <= self: LOGS_ERR) { $ This-> log ($ this-> formatTime (). "[{$ file }:{ $ line}] ERR :$ {msg} \ n "); } } } |