分享一个我自己用的在 WordPress 开发中用得到的一个调试日志类。
<?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 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\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.*/?>
以 WordPress 插件为例的用例:
在插件主文件的适当位置加入(假如上述代码放置在class-coolwp-debug-log.php文件中):
$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();}
如果在插件主文件中将__FILE__定义为常量 SOMEONE_THIS,那么,可以将SOMEONE_THIS 作为参数传给CoolWP_com_Log(),例如:
$this->Log = new CoolWP_com_Log(SOMEONE_THIS);
传不传参数的区别是日志文件的位置不同,如果不传参数,日志文件位于class-coolwp-debug-log.php所在目录下的debug目录下;如果传递了SOMEONE_THIS参数,那么,日志文件位于插件主目录下的debug目录下。日志文件的文件名称为debug_*******log。
日志条目默认采用北京时间。

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

PHP日志记录对于监视和调试Web应用程序以及捕获关键事件,错误和运行时行为至关重要。它为系统性能提供了宝贵的见解,有助于识别问题并支持更快的故障排除

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver CS6
视觉化网页开发工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器