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.

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

报告相同问题?

悬赏问题

  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?