A PHP Log class for debuging

Source: Internet
Author: User
Tags flock
Share a debug log class that I used to get in WordPress development.

 dir = dirname ($main _file). Directory_separator. ' Debug '.            Directory_separator; $file _name = ' debug_ '. MD5 ($main _file).            Log '; $this->debug_file_name = (function_exists (' apply_filters '))? Apply_filters (' Cwp_debug_log_file_name ', $file _ name). '. Log ': $file _name.            Log ';            $this->f_path = $this->dir. $this->debug_file_name;         $this->check_log_file (); /** * Adding Log Item * @param string $text: Adding content */Public function A            DD ($text) {date_default_timezone_set (' Asia/shanghai '); if (Is_array ($text) | |            Is_obeject ($text)) {$text = Json_encode ($text);            } $fp = fopen ($this->f_path, "a");            Flock ($FP, LOCK_EX); Fwrite ($FP, "". Date ("Y-m-d h:i:s", Time ()). " \ n ". $text."            \ n ");            Flock ($FP, lock_un);            Fclose ($FP);            return true; }/** * Checking the log file anD path.            * @return NULL */Private Function Check_log_file () {$is _dir = Is_dir ($this->dir);            $is _file = file_exists ($this->f_path);            if ($is _dir && $is _file) return; if (! $is _dir| |!                $is _file) {if (! $is _dir) {$md = mkdir ($this->dir,0777,true);                      } if (! $is _file) {$fp = fopen ($this->f_path, "a");                 Fclose ($FP); }}}}/*//class*/}/*all done.*/?>

Take WordPress plugin as an example of use cases:
Add in the appropriate location of the plugin main file (if the above code is placed in the class-coolwp-debug-log.php file):

$this->is_debug = true;if ($this->is_debug) {    require_once (Plugin_dir_path (__file__)). ' Classes/class-coolwp-debug-log.php ');    $this->log = new Coolwp_com_log ();}

If __file__ is defined as a constant someone_this in the plug-in main file, you can pass the Someone_this as a parameter to Coolwp_com_log (), for example:

$this->log = new Coolwp_com_log (someone_this);

The difference between the pass-through parameter is the location of the log file, if the parameter is not passed, the log file is located in the class-coolwp-debug-log.php directory under the Debug directory, if the Someone_this parameter is passed, then, The log files are located under the Debug directory in the plug-in home directory. The file name for the log file is Debug_*******log.

Log entries default to Beijing time.

  • Related Article

    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.

    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.