PHP 日誌工具類

來源:互聯網
上載者:User
好久沒寫php了,突然做個實驗,發現竟然沒有日誌,哎

FileName = substr($filename,0, $dot_offset);        else            $this->FileName = $filename;        $this->FilePath = $dir;        $this->m_MaxLogFileNum = intval($maxlogfilenum);        $this->m_RotaParam = intval($rotaparam);        $this->m_RotaType = intval($rotatype);        $this->m_Priority = intval($priority);        $this->m_LogCount = 0;        $this->m_InitOk = $this->InitDir();        umask(0000);         $path=$this->createPath($this->FilePath,$this->FileName);        if(!$this->isExist($path))        {            if(!$this->createDir($this->FilePath))            {                #echo("建立目錄失敗!");            }            if(!$this->createLogFile($path)){                #echo("建立檔案失敗!");            }        }    }                private function createPath($dir,$file)    {                $file = fopen($dir.DIRECTORY_SEPARATOR.$file,'w');        if($file)        {          fclose($file);        }                return $dir.DIRECTORY_SEPARATOR.$file;    }        private function InitDir()    {        if (is_dir($this->FilePath) === false)        {            if(!$this->createDir($this->FilePath))            {                //echo("建立目錄失敗!");                //throw exception                return false;            }        }        return true;    }  function console($priority, $log)      {          if ($this->m_InitOk == false)              return;          if ($priority > $this->m_Priority)              return;          $path = $this->getLogFilePath($this->FilePath, $this->FileName).Log::LOG_SUFFIXED;          $handle= fopen($path,"a+");          if ($handle === false)          {              return;          }                  $datestr = strftime("%Y-%m-%d %H:%M:%S ");          $caller_info = $this->get_caller_info();                 $status = fwrite($handle, $caller_info.$datestr.$log."\n");        if(!$status){//寫日誌失敗              echo("寫入日誌失敗");          }          fclose($handle);          $this->RotaLog();      }      /**     * @abstract 寫入日誌     * @param String $log 內容     */    function setLog($log)    {        $this->console(Log::NOTICE, $log);    }    function LogDebug($log)    {        $this->console(Log::DEBUG, $log);    }    function LogError($log)    {        $this->console(Log::ERROR, $log);    }    function LogNotice($log)    {        $this->console(Log::NOTICE, $log);    }       private function get_caller_info()    {    $ret = debug_backtrace();    foreach ($ret as $item)    {    if(isset($item['class']) && 'Logs' == $item['class'])    {    continue;    }    $file_name = basename($item['file']);    return <<getLogFilePath($this->FilePath, $this->FileName).LOG::LOG_SUFFIXED;        if ($this->m_LogCount%10==0)            clearstatcache();        ++$this->m_LogCount;        $file_stat_info = stat($file_path);        if ($file_stat_info === FALSE)            return;        if ($this->m_RotaType != 1)            return;             //echo "file: ".$file_path." vs ".$this->m_RotaParam."\n";        if ($file_stat_info['size'] < $this->m_RotaParam)            return;        $raw_file_path = $this->getLogFilePath($this->FilePath, $this->FileName);        $file_path = $raw_file_path.($this->m_MaxLogFileNum - 1).LOG::LOG_SUFFIXED;        //echo "lastest file:".$file_path."\n";        if ($this->isExist($file_path))        {            unlink($file_path);        }        for ($i = $this->m_MaxLogFileNum - 2; $i >= 0; $i--)        {            if ($i == 0)                $file_path = $raw_file_path.LOG::LOG_SUFFIXED;            else                $file_path = $raw_file_path.$i.LOG::LOG_SUFFIXED;            if ($this->isExist($file_path))            {                $new_file_path = $raw_file_path.($i+1).LOG::LOG_SUFFIXED;                if (rename($file_path, $new_file_path) < 0)                {                    continue;                }            }        }    }    function isExist($path){        return file_exists($path);    }    /**     * @abstract 建立目錄     * @param  $dir 目錄名     * @return bool     */    function createDir($dir){        //判斷目錄是否存在        return is_dir($dir) or ($this->createDir(dirname($dir)) and mkdir($dir, 0777));    }    /**     * @abstract 建立記錄檔     * @param String $path     * @return bool     */    function createLogFile($path){        $handle=fopen($path,"w"); //建立檔案        if($handle)        {        fclose($handle);        }        return $this->isExist($path);    }    /**     * @abstract 建立路徑     * @param String $dir 目錄名     * @param String $filename      */    function getLogFilePath($dir,$filename){        return $dir."/".$filename;    }}?>
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.