您的位置:首页 > 编程语言 > PHP开发

php中记录日志的几种方法

2017-08-22 21:48 295 查看
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);
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: