论坛首页 编程语言技术论坛

写个日志封装器....感觉用起来很爽

浏览 9120 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-05-30  
这里记录下 一点 为什么在 LogFirePHPWriter 的构造函数里面 使用 自定义的

# // 注册脚本结束时要运行的方法,将缓存的日志内容写入文件 
#             ShutdownCallback::getInstance()->add(array($this, '__writeLog'));

而不使用 类的 析构函数 来自动设置写入... 这个原因 在于 异常的出现 , 当出现异常时 完全就抛出异常了 不会再进行 这个操作了....

所以 使用 自定义的 ShutdownCallback 来保证会被脚本100%执行....

这些都是 sfw 里面的代码 .... 呵呵...

ShutdownCallback  很简单 ... 就一个基本类

<?php
/**
 * 对register_shutdown_function 函数栈的扩展 
 *
 */
final class ShutdownCallback {
	
	private $_stack = NULL;
	
	private function __construct(){
		$this->_stack = array();
		register_shutdown_function(array($this, 'flush'));
	}
	
	/**
	 * 返回 ShutdownCallback 类的单例对象
	 *
	 * @return ShutdownCallback
	 */
	static function getInstance(){
		static $inst = NULL;
		if (!$inst)
			$inst = new self();
		return $inst;
	}
	
	/**
	 * 向框架注册一个脚本结束时要调用的方法
	 * 此方法用于取代 类的 析构函数,当脚本运行出现异常或者错误时,析构函数并不执行
	 * 
	 * $callback 类型:
	 * 	  1. 字符串 -- str_func_name
	 * 	  2. 数组   -- array($class_or_obj,$method) 
	 * @param mixed $callback
	 * @param array $params
	 */
	function add($callback,array $params=null){
		if (is_callable($callback))
    		$this->_stack[] = array($callback,$params);
	}
	
	function flush(){
		while(!empty($this->_stack)){
			list($callback,$params) = array_pop($this->_stack);
			call_user_func_array($callback,$params);
		}
	}
}


0 请登录后投票
   发表时间:2011-05-30  
输出结果如下:
引用

[2011-05-27 16:15:14 0.625001907349] ======= SFW Loaded =======
[2011-05-27 16:15:14] REQUEST_URI: /index.php?controller=about123
[2011-05-27 16:15:14] [error] error:需要的类文件 "controllers/about123.php" 没有找到.
[2011-05-27 16:15:14 0.693089962006] ======= SFW End (elapsed: 0.068088 seconds) =======

[2011-05-28 18:17:21 0.343753099442] ======= SFW Loaded =======
[2011-05-28 18:17:21] REQUEST_URI: /index.php?q=/product/index/module/bbs
[2011-05-28 18:17:21] [error] error:需要的类文件 "modules/bbs/controllers/product.php" 没有找到.
[2011-05-28 18:17:21 0.414000988007] ======= SFW End (elapsed: 0.070248 seconds) =======

[2011-05-28 18:22:13 0.390628099442] ======= SFW Loaded =======
[2011-05-28 18:22:13] REQUEST_URI: /index.php?q=contace/index/
[2011-05-28 18:22:13] [error] error:需要的类文件 "controllers/ontace.php" 没有找到.
[2011-05-28 18:22:13 0.461007118225] ======= SFW End (elapsed: 0.070379 seconds) =======

[2011-05-30 12:19:10 0.875003099442] ======= SFW Loaded =======
[2011-05-30 12:19:10] REQUEST_URI: /index.php
[2011-05-30 12:19:10] [error] error:需要的文件 "代码地址/_code/app/views/_elements/repproductimgs_element.php" 没有找到.
[2011-05-30 12:19:10 0.995488882065] ======= SFW End (elapsed: 0.120486 seconds) =======

[2011-05-30 12:19:11 0.125001907349] ======= SFW Loaded =======
[2011-05-30 12:19:11] REQUEST_URI: /index.php?q=/application/index/
[2011-05-30 12:19:11] [error] error:需要的文件 "代码地址/_code/app/views/_elements/repproductimgs_element.php" 没有找到.
[2011-05-30 12:19:11 0.245165109634] ======= SFW End (elapsed: 0.120163 seconds) =======

[2011-05-30 12:19:11 0.343750953674] ======= SFW Loaded =======
[2011-05-30 12:19:11] REQUEST_URI: /index.php?q=/application/index/
[2011-05-30 12:19:11] [error] error:需要的文件 "代码地址/_code/app/views/_elements/repproductimgs_element.php" 没有找到.
[2011-05-30 12:19:11 0.466424942017] ======= SFW End (elapsed: 0.122674 seconds) =======

[2011-05-30 12:19:11 0.546875953674] ======= SFW Loaded =======
[2011-05-30 12:19:11] REQUEST_URI: /index.php?q=/application/index/
[2011-05-30 12:19:11] [error] error:需要的文件 "代码地址/_code/app/views/_elements/repproductimgs_element.php" 没有找到.
[2011-05-30 12:19:11 0.675940990448] ======= SFW End (elapsed: 0.129065 seconds) =======

[2011-05-30 12:19:11 0.781251907349] ======= SFW Loaded =======
[2011-05-30 12:19:11] REQUEST_URI: /index.php?q=/application/index/
[2011-05-30 12:19:11] [error] error:需要的文件 "代码地址/_code/app/views/_elements/repproductimgs_element.php" 没有找到.
[2011-05-30 12:19:11 0.894905090332] ======= SFW End (elapsed: 0.113653 seconds) =======

[2011-05-30 12:19:12 1.90734863281E-006] ======= SFW Loaded =======
[2011-05-30 12:19:12] REQUEST_URI: /index.php?q=/application/index/
[2011-05-30 12:19:12] [error] error:需要的文件 "代码地址/_code/app/views/_elements/repproductimgs_element.php" 没有找到.
[2011-05-30 12:19:12 0.118478059769] ======= SFW End (elapsed: 0.118476 seconds) =======

[2011-05-30 12:19:12 0.203125953674] ======= SFW Loaded =======
[2011-05-30 12:19:12] REQUEST_URI: /index.php?q=/application/index/
[2011-05-30 12:19:12] [error] error:需要的文件 "代码地址/_code/app/views/_elements/repproductimgs_element.php" 没有找到.
[2011-05-30 12:19:12 0.324215888977] ======= SFW End (elapsed: 0.121090 seconds) =======

[2011-05-30 12:19:12 0.406250953674] ======= SFW Loaded =======
[2011-05-30 12:19:12] REQUEST_URI: /index.php?q=/application/index/
[2011-05-30 12:19:12] [error] error:需要的文件 "代码地址/_code/app/views/_elements/repproductimgs_element.php" 没有找到.
[2011-05-30 12:19:12 0.518239021301] ======= SFW End (elapsed: 0.111988 seconds) =======

[2011-05-30 12:19:12 0.609375953674] ======= SFW Loaded =======
[2011-05-30 12:19:12] REQUEST_URI: /index.php?q=/application/index/
[2011-05-30 12:19:12] [error] error:需要的文件 "代码地址/_code/app/views/_elements/repproductimgs_element.php" 没有找到.
[2011-05-30 12:19:12 0.729964971542] ======= SFW End (elapsed: 0.120589 seconds) =======

[2011-05-30 12:19:12 0.812503099442] ======= SFW Loaded =======
[2011-05-30 12:19:12] REQUEST_URI: /index.php?q=/application/index/
[2011-05-30 12:19:12] [error] error:需要的文件 "代码地址/_code/app/views/_elements/repproductimgs_element.php" 没有找到.
[2011-05-30 12:19:12 0.924803972244] ======= SFW End (elapsed: 0.112301 seconds) =======

[2011-05-30 12:19:13 0.0156269073486] ======= SFW Loaded =======
[2011-05-30 12:19:13] REQUEST_URI: /index.php?q=/application/index/
[2011-05-30 12:19:13] [error] error:需要的文件 "代码地址/_code/app/views/_elements/repproductimgs_element.php" 没有找到.
[2011-05-30 12:19:13 0.1363260746] ======= SFW End (elapsed: 0.120699 seconds) =======

[2011-05-30 12:19:13 0.234378099442] ======= SFW Loaded =======
[2011-05-30 12:19:13] REQUEST_URI: /index.php?q=/application/index/
[2011-05-30 12:19:13] [error] error:需要的文件 "代码地址/_code/app/views/_elements/repproductimgs_element.php" 没有找到.
[2011-05-30 12:19:13 0.355084896088] ======= SFW End (elapsed: 0.120707 seconds) =======

[2011-05-30 12:19:13 0.437500953674] ======= SFW Loaded =======
[2011-05-30 12:19:13] REQUEST_URI: /index.php?q=/application/index/
[2011-05-30 12:19:13] [error] error:需要的文件 "代码地址/_code/app/views/_elements/repproductimgs_element.php" 没有找到.
[2011-05-30 12:19:13 0.55776309967] ======= SFW End (elapsed: 0.120262 seconds) =======

[2011-05-30 12:19:13 0.640625953674] ======= SFW Loaded =======
[2011-05-30 12:19:13] REQUEST_URI: /index.php?q=/application/index/
[2011-05-30 12:19:13] [error] error:需要的文件 "代码地址/_code/app/views/_elements/repproductimgs_element.php" 没有找到.
[2011-05-30 12:19:13 0.770454883575] ======= SFW End (elapsed: 0.129829 seconds) =======

[2011-05-30 12:19:13 0.859378099442] ======= SFW Loaded =======
[2011-05-30 12:19:13] REQUEST_URI: /index.php?q=/application/index/
[2011-05-30 12:19:13] [error] error:需要的文件 "代码地址/_code/app/views/_elements/repproductimgs_element.php" 没有找到.
[2011-05-30 12:19:13 0.979728937149] ======= SFW End (elapsed: 0.120351 seconds) =======

[2011-05-30 12:19:14 0.0625019073486] ======= SFW Loaded =======
[2011-05-30 12:19:14] REQUEST_URI: /index.php?q=/application/index/
[2011-05-30 12:19:14] [error] error:需要的文件 "代码地址/_code/app/views/_elements/repproductimgs_element.php" 没有找到.
[2011-05-30 12:19:14 0.183491945267] ======= SFW End (elapsed: 0.120990 seconds) =======

[2011-05-30 12:19:14 0.265626907349] ======= SFW Loaded =======
[2011-05-30 12:19:14] REQUEST_URI: /index.php?q=/application/index/
[2011-05-30 12:19:14] [error] error:需要的文件 "代码地址/_code/app/views/_elements/repproductimgs_element.php" 没有找到.
[2011-05-30 12:19:14 0.38857793808] ======= SFW End (elapsed: 0.122951 seconds) =======

[2011-05-30 12:19:14 0.484375953674] ======= SFW Loaded =======
[2011-05-30 12:19:22] REQUEST_URI: /index.php?q=/application/index/
[2011-05-30 12:19:22] [error] error:需要的文件 "代码地址/_code/app/views/_elements/repproductimgs_element.php" 没有找到.
[2011-05-30 12:19:22 0.12629199028] ======= SFW End (elapsed: 7.641916 seconds) =======



0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics