PHP生成文件缓存,微信调试工具

一个方便的写入log函数,第一个参数不写的话,生成到当前文件夹下logs文件夹,适用于接口对接调试

//写入log
function fog(){
	static $name;
	$arg = func_get_args();
	$backtrace=debug_backtrace();
	$actionflie=pathinfo($backtrace[0]['file']);
	if(is_string($arg[0]) && preg_match( '/\.\w{2,3}$/' , $arg[0] )){
		$path = $arg[0];
		unset($arg[0]);
	}else{
		$path = __DIR__ . '/logs/'. date('Y-m-d',$_SERVER['REQUEST_TIME']) . '.log'	;
	} 
	$path = iconv("gbk", "utf-8", $path);
	$info = pathinfo($path);
	if (!is_dir($info['dirname'])) {
		$arr = explode('/', $info['dirname']);
		foreach ($arr as $str)if (!file_exists($aimDir.= $str.'/'))mkdir($aimDir);
	}
	foreach ($arg as $k=>$v){
		$arr[str_replace('.php','',$actionflie['basename'].':'.$backtrace[0]['line'].'.'.$k)]=$v;
	}
	$text = var_export ($arr,true);
	$text = preg_replace('/^array \(|\s*\)$/', '', trim($text));
	$text = preg_replace('/\=\>\s+array/m', ' => array', $text);
	$text = str_replace('\\\\', '/', $text);
	$text = $name == $path ? $text : ("\r\n\r\n###### ".date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME'])." ( http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']." ) ######".$text);
	$name = $path;
	$hand = fopen($path,'a+');
	fwrite($hand,$text);
	fclose($hand);
}