【发布时间】:2012-03-19 10:05:55
【问题描述】:
我有一个函数可以跟踪整个脚本中发生的事件。为了有效地使用我的资源,我决定压缩它生成的数据。但是,我不断收到此错误:
Unknown error type: [2] gzuncompress() [function.gzuncompress]: data error
函数如下:
function eventlog($type, $message){
// Types: account,run,queue,system
// Set up file name/location
$eventfile = '/myprivatedirectory/'.date('Ymd').$type.'.log';
if(file_exists($eventfile)){
while(!is_writable($eventfile)){clearstatcache();}
$fh_log = fopen($eventfile,'r+');
flock($fh_log, LOCK_EX);
$logcontents = gzuncompress(fread($fh_log,filesize($eventfile)));
rewind($fh_log);
ftruncate($fh_log, 0);
$logcompressed = gzcompress($logcontents.$message."\n");
fwrite($fh_log,$logcompressed);
flock($fh_log, LOCK_UN);
fclose($fh_log);
} else {
$fh_log = fopen($eventfile,'w');
flock($fh_log, LOCK_EX);
$logcompressed = gzcompress($message."\n");
fwrite($fh_log,$logcompressed);
flock($fh_log, LOCK_UN);
fclose($fh_log);
}
}
因此,每天午夜,当发生上述任何事件(帐户、运行、队列、系统)时,都会创建一个新的错误日志,否则每个新事件都会附加到相应的日志文件中。
我很想继续压缩,但我不能一直有这些错误,有人可以帮忙吗?提前致谢。
【问题讨论】:
-
file_put_contents会为您省去很多麻烦。 -
我正在尝试使用
file_get_contents,到目前为止没有问题。
标签: php file-io event-log compression