PHP记录日志文件类

<?php
header('content-type:text/html;charset=utf-8');  
include_once '/var/www/html/api/com/com_inc.php';

class Log {
   
    const EXCEPT = 1;   //error info
    const INFO = 2;   //critical info
    const DEBUG = 3;  //all info 
    const MYSQL_QUERY = 4;  //mysql info
              
    const IS_OPEN = true;  //switch
    const LOG_LEVEL = Log::DEBUG;  //level
	public static function info($msg, $level) {
        if (Log::IS_OPEN && Log::LOG_LEVEL >= $level) {
    		try { 
    			$_filename = Config::getLogFilePath() . "." . date("Y-m-d-H");
                
                if (file_exists($_filename) && 1024 * 1024 * 100 <= filesize($_filename)) {
                    rename($_filename, Config::getLogFilePath() . "." . date("Y-m-d-H-i-s"));
                }            
                
    			$_fp = fopen($_filename, 'a+');
    			$msg = date("Y-m-d H:i:s") . " " . posix_getpid() . "\t" . $msg;
    			fwrite($_fp, $msg . "\n");
                
    			fclose($_fp);
    		} catch (Exception $e) {
    		}  
        }    
	}
    
    public static function substrFormat($text, $length, $replace = '..', $encoding='UTF-8') {
        if ($text && mb_strlen($text, $encoding) > $length) {
            return mb_substr($text, 0, $length, $encoding) . $replace;
        }
        
        return $text;
    }
}
?>
分类:

发表回复