PHP uses single case mode to implement log processing class library

Source: Internet
Author: User
Tags chr file size

  This log class uses a single case pattern to conserve resources. The file size is determined by itself, and the file is created by itself in order to exceed the specified size.

For today's applications, the importance of journaling is self-evident. It is hard to imagine an application without any logging capabilities running in a production environment. The functions that a log can provide are varied, including error messages, status information, debugging information, and execution time information that is generated when the program is running. In a production environment, logs are an important basis for finding the source of a problem. All kinds of information generated when the application is running should be logged through the Log class library.     Code as follows:/**  * log processing class  *   * @since Alpha 0.0.1  * @date 2014.03.04  * @author Gen Ialx  *   */  class log{     /single case mode     private static $instance   &N Bsp;= NULL;    //file handle     private static $handle      = NULL;    //log switch     private $log _switch     = NULL;    //Log relative directory     private $log _file_path      = NULL;    //log file max length, exceeding length rebuild file     Private $log _max_len        = NULL;    //log file prefix, into log_0     private $log _file_pre       = ' log_ ';                /**      * construction function      *       * @since Alpha 0.0.1      * @date 2014.02.04      * @author genialx     &NBSP ; */    protected function __construct () {//NOTE: The following are constants in the configuration file, please change the reader's own           $this-> Log_file_path     = Log_file_path;           $this->log_switch     = Log_switch;             $this->log_max_len    = Log_max_len;      }      /**      * Simple mode      *      &NBSP ; * @since Alpha 0.0.1      * @date 2014.02.04      * @author genialx      */     public static function Get_instance () {        If!self:: $instance instanceof self) {&NB Sp           self:: $instance = new Self;        }         return self:: $instance;    }      /**      *       * log record      *       * @param int $type  0-> record (THING log)/1-> error (Error LOG)      * @param string $desc   &NB Sp  * @param string $time      *       * @since Alpha 0.0.1      * @da Te 2014.02.04      * @author genialx      *       */    Publi C function log ($type, $desc, $time) {        if ($this->log_switch) {        &NBSP ;     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:               & nbsp     fwrite (self:: $handle, ' THING LOG: '. ' ' . $desc. ' ' . $time. Chr (13));                     break;                 Case 1:                 &N Bsp   Fwrite (self:: $handle, ' ERROR LOG: '. ' ' . $desc. ' ' . $time. Chr (13));                     break;                 default:                 & nbsp   Fwrite (self:: $handle, ' THING LOG: '. ' ' . $desc. ' ' . $time. Chr (13));                     break;                          /**         &NBSP  * gets the current log's latest document suffix      *       * @since Alpha 0.0.1      * @date 20 14.02.04      * @author genialx      */    Private Function Get_max_log_file_suf ( {        $log _file_suf = null;         if (Is_dir ($this->log_file_path)) {&nbs P           if ($dh = Opendir ($this->log_file_path)) {            & nbsp   while ($file = Readdir ($DH))!= FALSE) {                    if ($f Ile!= '. ' && $file!= ' ... ') {                        filetype ($this->log_file_path . $file) = = ' file ') {                            $RS = Split (' _ ', $file);                             if ($log _file_suf < $rs [1]) {                                $log _file_suf = $rs [1];                            }       & nbsp                                   & nbsp {               }                   if ($log _file_suf = NULL) {                    $log _file_suf = 0; &nbsp ;               {               //truncate file   & nbsp             file_exists ($this->log_file_path $this->log_file_pre. $log _file_su f) && filesize ($this->log_file_path. $this->Log_file_pre. $log _file_suf) >= $this->log_max_len) {                    $log _fi Le_suf = Intval ($log _file_suf) + 1;                                   R Eturn $log _file_suf;                                  return 0;      }      /**      * closing file handle      *      &NB sp;* @since Alpha 0.0.1      * @date 2014.02.04      * @author genialx      * /    Public Function close () {        fclose (self:: $handle);    }}   &NBSP ; Function Description: This log class utilizes a single example mode to save resources. The file size is determined by itself, and the file is created by itself in order to exceed the specified size. If the file log_0 is larger than the specified size, recreate the log_1 file (Note: The Create file is a number that installs the filename suffix, do not change the log file name arbitrarily).   To be optimized: Do not specify the maximum number of files, so periodically manually delete too many log files.   Calling Example:     Copying codeThe code is as follows://log $L = Log::get_instance (); The first parameter int 0 represents the event record (THING log:), and 1 represents the error record (Error log:)///The second argument string describes text//third parameter string time $L->log (1, ' Log description ', date (' Y-n-j H:m:s ')); $L->close ();  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.