php中記錄日志的幾種方法


1、使用fwrite()函數寫入文件

<?php
$now=date('y-m-d',time());
$filename='log/tongbu_'.$now.'.log';
$file=fopen($filename,"a+"); //a+表示文件可讀寫方式打開
fwrite($file,'開始寫入'."\n");
$time=time();
$res="www.geiliyou.com";
$result='ok';
fwrite($file,$time."\t".$res."\t".$result."\n");
fwrite($file,'寫入完成'."\n");

2、使用error_log()函數寫入
<?php
$now=date('y-m-d',time());
$filename='log/tongbu_'.$now.'.log';
$time=time();
$re="www.geiliyou.com";
$result='ok';
error_log($time."\t".$re."\t".$result."\n",3,$filename);//3表示消息被發送到后面的文件
?>
3、使用file_put_contents()函數創建寫入文件

<?php
function writelog($loginfo){
$file='log/tongbu_'.date('y-m-d').'.log';
if(!is_file($file)){
file_put_contents($file,'',FILE_APPEND);//如果文件不存在,則創建一個新文件。
}
$contents=$loginfo."\r\n";
file_put_contents($file, $contents,FILE_APPEND);
}

?>

————————————————
版權聲明:本文為CSDN博主「君臨天下_Sugar」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/tsummerb/article/details/77488300


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM