PHP中file_put_contents函数使用参数FILE_APPEND追加和换行
关于环境变量获取:Env::get()
Thinkphp 5.1 版本取消了所有的系统常量,这些预定义常量被定义为了项目的环境变量,所以现在原来的系统路径常量改为使用think\Env类来存取,在开发中我们可以引入think\Env的门面类(think\facade\Env)
/*
* 写入日志
*/
public function error_log($param,$log_err)
{
$path = Env::get('runtime_path').'error'.'\\'.date('Y-m-d').$param.".log";
$json = json_encode($log_err,JSON_UNESCAPED_UNICODE);
$dir = dirname($path);
if(!is_dir($dir)){
mkdir($dir,0777);
}
file_put_contents($path,$json.date('H:i:s').PHP_EOL,FILE_APPEND);
return ['code'=>1,'msg'=>'错误日志插入成功'];
}
生成结果