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 ' huh? ', ' 0.9.0 '); } public Function __wakeup () {_doing_it_wrong (__function__, ' cheatin ' 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 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.
In the WordPress environment, you can use the filter hook:
Apply_filters (' Cwp_debug_log_file_name ', $file _name)
To modify the name of the default log file.
The first http://suoling.net/a-php-log-class-for-debuging/reprint to the blog park.
Share a PHP debug log class