取消在codeigniter中前一天创建的日志文件

时间:2016-08-04 09:09:39

标签: codeigniter

我正在尝试取消链接前一天创建的文件

我有自定义应用程序>核心> MY_Log.php文件,它为每个错误级别创建一个日志。为了便于阅读。

  • logs> DEBUG-04-08-2016.php
  • logs> ERROR-04-08-2016.php
  • logs> INFO-04-08-2016.php
  • logs> DEBUG-03-08-2016.php
  • logs> ERROR-03-08-2016.php
  • logs> INFO-03-08-2016.php
  

问题我如何修改write_log,以便删除/取消链接前一天创建的文件?

<?php

class MY_Log extends CI_Log {

    public function write_log($level, $msg)
    {
        if ($this->_enabled === FALSE)
        {
            return FALSE;
        }

        $level = strtoupper($level);

        if (( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
            && ! isset($this->_threshold_array[$this->_levels[$level]]))
        {
            return FALSE;
        }

        $filepath = $this->_log_path . $level .'-'. date('d-m-Y').'.'.$this->_file_ext;

        $message = '';

        if ( ! file_exists($filepath))
        {
            $newfile = TRUE;
            // Only add protection to php files
            if ($this->_file_ext === 'php')
            {
                $message .= "";
            }
        }

        if ( ! $fp = @fopen($filepath, 'ab'))
        {
            return FALSE;
        }

        flock($fp, LOCK_EX);

        // Instantiating DateTime with microseconds appended to initial date is needed for proper support of this format
        if (strpos($this->_date_fmt, 'u') !== FALSE)
        {
            $microtime_full = microtime(TRUE);
            $microtime_short = sprintf("%06d", ($microtime_full - floor($microtime_full)) * 1000000);
            $date = new DateTime(date('d-m-Y H:i:s.'.$microtime_short, $microtime_full));
            $date = $date->format($this->_date_fmt);
        }
        else
        {
            $date = date($this->_date_fmt);
        }

        $message .= $this->_format_line($level, $date, $msg);

        for ($written = 0, $length = strlen($message); $written < $length; $written += $result)
        {
            if (($result = fwrite($fp, substr($message, $written))) === FALSE)
            {
                break;
            }
        }

        flock($fp, LOCK_UN);
        fclose($fp);

        if (isset($newfile) && $newfile === TRUE)
        {
            chmod($filepath, $this->_file_permissions);
        }

        return is_int($result);
    }
}

1 个答案:

答案 0 :(得分:1)

首先使用

$config['log_threshold'] = 1;  

仅用于错误消息,因此文件数量会减少

$filepath;之前添加以下代码以删除以前的日期日志

$unlink_date = date('Y-m-d',strtotime("-1 days"));

$filepath_unlink = $this->_log_path . $level .'-'. $unlink_date.'.'.$this->_file_ext;

if ( file_exists($filepath_unlink))
{
   unlink($filepath_unlink);
}