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.