ds342222222 2014-11-18 07:05
浏览 24
已采纳

在PHP中自动维护每日日志文件?

I want to create automatically log files on daily basis for one month and after that old files will be unlinked itself. Following is my code. Am i doing right. If yes then is there any way to reduces filesize on disk.

 $KeepDays = 30;  # how many days of log files we'll keep on hand
    $logname  = date("Ymd") . '.status.log';
    $OldFile  = date('Ymd',mktime(0,0,0,date("m"),(date("d") - ($KeepDays + 1)),date("Y"))) . '.status.log';
    if (file_exists($OldFile)) { unlink($OldFile); } # erase oldest log file 

    if (file_exists('logs/'.$logname)) {
        $fp=fopen('logs/'.$logname,'a');
    }else{
        $fp=fopen('logs/'.$logname,'w');
    }
    $chunk = "Var1_".$var1.'_'.$Var2.' var3_'.$var3.' var_'.$var.' var_'.$var4 ;
    fwrite($fp,$chunk. PHP_EOL);
    fclose($fp);

Thanks in Advance.

  • 写回答

1条回答 默认 最新

  • drl92080 2014-11-18 07:33
    关注

    Don't think it's going to work. You basically remove oldfile and do not get its contents. Wherea re all those $var1 etc defined? Is there any kind of loop?

    $KeepDays = 30;  # how many days of log files we'll keep on hand
    $logname  = date("Ymd") . '.status.log';
    // Would give '20141118.status.log ' for example
    
    $OldFile  = date('Ymd',mktime(0,0,0,date("m"),(date("d") - ($KeepDays + 1)),date("Y"))) . '.status.log';
    // Would give '20141018.status.log' for example
    
    if (file_exists($OldFile)) { unlink($OldFile); } # erase oldest log file 
    // Remove old file, but you remove it and can't get it's contents anymore. So you delete it wihtout copying its contents anywhere
    
    if (file_exists('logs/'.$logname)) {
        $fp=fopen('logs/'.$logname,'a');
    }else{
        $fp=fopen('logs/'.$logname,'w');
    }
    $chunk = "Var1_".$var1.'_'.$Var2.' var3_'.$var3.' var_'.$var.' var_'.$var4 ;
    // Where are all those $var* defined? What you put here?
    
    
    fwrite($fp,$chunk. PHP_EOL);
    fclose($fp);
    

    Additionally, you can zip or gzip contents of file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?