php设置日志输出的方法:使用php的写入文件函数,把数据写入到事先定义好的文件中,代码为【file_put_contents(file,data,mode,context)】。

php设置日志输出的方法: 思想:在想要输出log日志的地方,使用php的写入文件函数,把数据写入到事先定义好的文件中。 php代码如下: //输出日志
public function outputLog() {
logOutput(time());
sleep(3);
$arr = array("k1" => "v1", "k2" => "v2");
logOutput($arr);
$this->display();
}
logOutput()函数:
/**
* @param string,array $data 需要输出到日志中的数据
* @return null
*/
function logOutput($data) {
//数据类型检测
if (is_array($data)) {
$data = json_encode($data);
}
$filename = "./log/".date("Y-m-d").".log";
$str = date("Y-m-d H:i:s")." $data"."\n";
file_put_contents($filename, $str, FILE_APPEND|LOCK_EX);
return null;
}file_put_contents() 函数把一个字符串写入文件中。 与依次调用 fopen(),fwrite() 以及 fclose() 功能一样。 语法 file_put_contents(file,data,mode,context) 参数 描述 file 必需。规定要写入数据的文件。如果文件不存在,则创建一个新文件。 data 可选。规定要写入文件的数据。可以是字符串、数组或数据流。 mode 可选。规定如何打开/写入文件。可能的值: FILE_USE_INCLUDE_PATH FILE_APPEND 追加数据而不是覆盖 LOCK_EX 写入数据时,锁住文件,防止其他人对文件的改动 context 可选。规定文件句柄的环境。(不懂何用) context 是一套可以修改流的行为的选项。若使用 null,则忽略。
意义: 相关免费学习推荐:php编程(视频)
以上就是php如何设置日志输出的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章! |