配色: 字号:
一个很精妙的PHP日志操作类
2016-08-16 | 阅:  转:  |  分享 
  
一个很精妙的PHP日志操作类classLogs{

private$FilePath;

private$FileName;

private$m_MaxLogFileNum;

private$m_RotaType;

private$m_RotaParam;

private$m_InitOk;

private$m_Priority;

private$m_LogCount;



/

@abstract初始化

@paramString$dir文件路径

@paramString$filename文件名

@return

/

functionLogs($dir,$filename,$priority=Logs::INFO,$maxlogfilenum=3,$rotatype=1,$rotaparam=5000000)

{

$dot_offset=strpos($filename,".");

if($dot_offset!==false)

$this->FileName=substr($filename,0,$dot_offset);

else

$this->FileName=$filename;

$this->FilePath=$dir;

$this->m_MaxLogFileNum=intval($maxlogfilenum);

$this->m_RotaParam=intval($rotaparam);

$this->m_RotaType=intval($rotatype);

$this->m_Priority=intval($priority);

$this->m_LogCount=0;



$this->m_InitOk=$this->InitDir();

umask(0000);

$path=$this->createPath($this->FilePath,$this->FileName);

if(!$this->isExist($path))

{

if(!$this->createDir($this->FilePath))

{

#echo("创建目录失败!");

}

if(!$this->createLogFile($path)){

#echo("创建文件失败!");

}

}

}

privatefunctionInitDir()

{

if(is_dir($this->FilePath)===false)

{

if(!$this->createDir($this->FilePath))

{

//echo("创建目录失败!");

//throwexception

returnfalse;

}

}

returntrue;

}



/

@abstract写入日志

@paramString$log内容

/



functionsetLog($log)

{

$this->Log(Logs::NOTICE,$log);

}

functionLogDebug($log)

{

$this->Log(Logs::DEBUG,$log);

}

functionLogError($log)

{

$this->Log(Logs::ERROR,$log);

}

functionLogNotice($log)

{

$this->Log(Logs::NOTICE,$log);

}

functionLog($priority,$log)

{

if($this->m_InitOk==false)

return;

if($priority>$this->m_Priority)

return;

$path=$this->getLogFwww.shanxiwang.netilePath($this->FilePath,$this->FileName).".log";

$handle=@fopen($path,"a+");

if($handle===false)

{

return;

}

$datestr=strftime("%Y-%m-%d%H:%M:%S");

$caller_info=$this->get_caller_info();

//var_dump($caller_info);

if(!@fwrite($handle,$caller_info.$datestr.$log."\n")){//写日志失败

//echo("写入日志失败");

}

@fclose($handle);

$this->RotaLog();

}

privatefunctionget_caller_info()

{

$ret=debug_backtrace();

foreach($retas$item)

{

if(isset($item[''class''])&&''Logs''==$item[''class''])

{

continue;

}

$file_name=basename($item[''file'']);

return<<
{$file_name}:{$item[''line'']}

S;



}

}

privatefunctionRotaLog()

{

$file_path=$this->getLogFilePath($this->FilePath,$this->FileName).".log";

if($this->m_LogCount%10==0)

clearstatcache();

++$this->m_LogCount;

$file_stat_info=stat($file_path);

if($file_stat_info===FALSE)

return;

if($this->m_RotaType!=1)

return;



//echo"file:".$file_path."vs".$this->m_RotaParam."\n";

if($file_stat_info[''size'']<$this->m_RotaParam)

return;



$raw_file_path=$this->getLogFilePath($this->FilePath,$this->FileName);

$file_path=$raw_file_path.($this->m_MaxLogFileNum-1).".log";

//echo"lastestfile:".$file_path."\n";

if($this->isExist($file_path))

{

unlink($file_path);

}

for($i=$this->m_MaxLogFileNum-2;$i>=0;$i--)

{

if($i==0)

$file_path=$raw_file_path.".log";

else

$file_path=$raw_file_path.$i.".log";



if($this->isExist($file_path))

{

$new_file_path=$raw_file_path.($i+1).".log";

if(rename($file_path,$new_file_path)<0)

{

continue;

}

}

}

}



functionisExist($path){

returnfile_exists($path);

}



/

@abstract创建目录

@param$dir目录名

@returnbool

/

functioncreateDir($dir){

returnis_dir($dir)or($this->createDir(dirname($dir))and@mkdir($dir,0777));

}



/

@abstract创建日志文件

@paramString$path

@returnbool

/

functioncreateLogFile($path){

$handle=@fopen($path,"w");//创建文件

@fclose($handle);

return$this->isExist($path);

}



/

@abstract创建路径

@paramString$dir目录名

@paramString$filename

/

functiongetLogFilePath($dir,$filename){

return$dir."/".$filename;

}

constEMERG=0;

constFATAL=0;

constALERT=100;

constCRIT=200;

constERROR=300;

constWARN=400;

constNOTICE=500;

constINFO=600;

constDEBUG=700;

}

?>





使用方法:

require("../log.class.php");

$log_path=DEBUG_LOG_PATH.''account_check/'';

$log_file_name=''debug.log'';

$log_obj=newLogs($log_path,$log_file_name);

$log_obj->setLog("paramis:userid:{$userip},account:{$account},pwd:{$pwd},rid:{$rid}");



献花(0)
+1
(本文系网络学习天...首藏)