分享一个我自己用的在 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。
日志条目默认采用北京时间。

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

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

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

Dreamweaver CS6
视觉化网页开发工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用