Share a debug log class that I used to get in WordPress development.
<?php/** * @author: suifengtec coolwp.com * @date: 2013-02-03 09:55:55 * @last modified by: suifengtec coolwp.com * @last modified time: 2015-07-12 18:40:02 */if (function_exists (' add_ Action ') { defined (' abspath ') or exit;} if (!class_exists (' Coolwp_com_log ')) { final class coolwp_com_log{ private $dir = null; private $debug _file_name = null; private $f _path = null; public function __clone () { _doing_it_wrong ( __FUNCTION__, ' cheatin& #8217; huh ', ' 0.9.0 ' ); } public function __wakeup () { _ Doing_it_wrong ( __FUNCTION__, ' cheatin& #8217; huh? ', ' 0.9.0 ' ); } public function __ Construct ($main _file= ") { $main _file = (' = = $main _file) __file__: $main _file; $this->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 add ($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 master file (if the above code is placed in theclass-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 directory of class-coolwp-debug-log.php in 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.
This article is from the "Suifengtec blog" blog, make sure to keep this source http://suifengtec.blog.51cto.com/5746046/1673469
A PHP Log class for debuging