溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在php中使用單例模式實(shí)現(xiàn)一個(gè)日志處理類

發(fā)布時(shí)間:2020-12-25 15:33:46 來源:億速云 閱讀:139 作者:Leah 欄目:開發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)怎么在php中使用單例模式實(shí)現(xiàn)一個(gè)日志處理類,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

對(duì)于現(xiàn)在的應(yīng)用程序來說,日志的重要性是不言而喻的。很難想象沒有任何日志記錄功能的應(yīng)用程序運(yùn)行在生產(chǎn)環(huán)境中。日志所能提供的功能是多種多樣的,包括記錄程序運(yùn)行時(shí)產(chǎn)生的錯(cuò)誤信息、狀態(tài)信息、調(diào)試信息和執(zhí)行時(shí)間信息等。在生產(chǎn)環(huán)境中,日志是查找問題來源的重要依據(jù)。應(yīng)用程序運(yùn)行時(shí)的產(chǎn)生的各種信息,都應(yīng)該通過日志類庫(kù)來進(jìn)行記錄。

復(fù)制代碼 代碼如下:


/**
 * 日志處理類
 *
 * @since alpha 0.0.1
 * @date 2014.03.04
 * @author genialx
 *
 */

class Log{

    //單例模式
    private static $instance    = NULL;
    //文件句柄
    private static $handle      = NULL;
    //日志開關(guān)
    private $log_switch     = NULL;
    //日志相對(duì)目錄
    private $log_file_path      = NULL;
    //日志文件最大長(zhǎng)度,超出長(zhǎng)度重新建立文件
    private $log_max_len        = NULL;
    //日志文件前綴,入 log_0
    private $log_file_pre       = 'log_';

        
    /**
     * 構(gòu)造函數(shù)
     *
     * @since alpha 0.0.1
     * @date 2014.02.04
     * @author genialx
     */
    protected function __construct(){//注意:以下是配置文件中的常量,請(qǐng)讀者自行更改

        $this->log_file_path     = LOG_FILE_PATH;

        $this->log_switch     = LOG_SWITCH; 

        $this->log_max_len    = LOG_MAX_LEN;

    }

    /**
     * 單利模式
     *
     * @since alpha 0.0.1
     * @date 2014.02.04
     * @author genialx
     */
    public static function get_instance(){
        if(!self::$instance instanceof self){
            self::$instance = new self;
        }
        return self::$instance;
    }

    /**
     *
     * 日志記錄
     *
     * @param int $type  0 -> 記錄(THING LOG) / 1 -> 錯(cuò)誤(ERROR LOG)
     * @param string $desc
     * @param string $time
     *
     * @since alpha 0.0.1
     * @date 2014.02.04
     * @author genialx
     *
     */
    public function log($type,$desc,$time){
        if($this->log_switch){

            if(self::$handle == NULL){
                $filename = $this->log_file_pre . $this->get_max_log_file_suf();
                self::$handle = fopen($this->log_file_path . $filename, 'a');
            }
            switch($type){
                case 0:
                    fwrite(self::$handle, 'THING LOG:' . ' ' . $desc . ' ' . $time . chr(13));
                    break;
                case 1:
                    fwrite(self::$handle, 'ERROR LOG:' . ' ' . $desc . ' ' . $time . chr(13));
                    break;
                default:
                    fwrite(self::$handle, 'THING LOG:' . ' ' . $desc . ' ' . $time . chr(13));
                    break;
            }

        }
    }

    /**
     * 獲取當(dāng)前日志的最新文檔的后綴
     *
     * @since alpha 0.0.1
     * @date 2014.02.04
     * @author genialx
     */
    private function get_max_log_file_suf(){
        $log_file_suf = null;
        if(is_dir($this->log_file_path)){
            if($dh = opendir($this->log_file_path)){
                while(($file = readdir($dh)) != FALSE){
                    if($file != '.' && $file != '..'){
                        if(filetype( $this->log_file_path . $file) == 'file'){
                            $rs = split('_', $file);
                            if($log_file_suf < $rs[1]){
                                $log_file_suf = $rs[1];
                            }
                        }
                    }
                }

                if($log_file_suf == NULL){
                    $log_file_suf = 0;
                }
                //截?cái)辔募?br/>                if( file_exists($this->log_file_path . $this->log_file_pre . $log_file_suf) && filesize($this->log_file_path . $this->log_file_pre . $log_file_suf) >= $this->log_max_len){
                    $log_file_suf = intval($log_file_suf) + 1;
                }

                return $log_file_suf;
            }  
        }

        return 0;

    }

    /**
     * 關(guān)閉文件句柄
     *
     * @since alpha 0.0.1
     * @date 2014.02.04
     * @author genialx
     */
    public function close(){
        fclose(self::$handle);
    }
}

功能說明:
該日志類利用單例模式,節(jié)省資源。自行判斷文件大小,超出指定大小則按序自行創(chuàng)建文件。如:文件log_0大于指定大小,則重新創(chuàng)建log_1文件(注意:創(chuàng)建文件是安裝文件名后綴的數(shù)字的,請(qǐng)勿隨意更改日志文件名)。

有待優(yōu)化:沒有指定文件的最大個(gè)數(shù),所以定期要手動(dòng)刪除過多的日志文件。

調(diào)用示例:

復(fù)制代碼 代碼如下:


//LOG
$L = Log::get_instance();
//第一個(gè)參數(shù) int 0代表事件記錄(THING LOG:),1代表錯(cuò)誤記錄(ERROR LOG:)
//第二個(gè)參數(shù) string 描述文字
//第三個(gè)參數(shù) string 時(shí)間
$L->log(1,'日志描述', date('Y-n-j H:m:s'));
$L->close();

上述就是小編為大家分享的怎么在php中使用單例模式實(shí)現(xiàn)一個(gè)日志處理類了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI