`

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

阅读更多

写个日志封装器....

<?php
/**
 * 日志系统抽象类
 *
 */
abstract class LogWriterAbstract {
		
    /**
     * 是否启用日志记录
     *
     * @var boolean
     */
    protected $_enabled = true;    
    
    abstract function append($msg, $title = '', $level = 'info');
}

/**
 * LogFirePHPWriter 类提供对 FirePHP 的支持
 */
class LogFirePHPWriter extends LogWriterAbstract {
	
	/**
	 * FirePHP 具体实现对象
	 *
	 * @var FirePHP
	 */
    private $_kernel;
        
    function __construct(){
    	$this->_enabled = App::ini('_log_enabled',true);
    	if (!$this->_enabled) return;
    	
    	require_once 'FirePHP.class.php';
    	$this->_kernel = FirePHP::getInstance(true);
    }
    
    function append($msg, $title = '', $level = 'info'){
    	if ($this->_enabled)
	    	$this->_kernel->fb($msg, $title, FirePHP::INFO);
    }
}

/**
 * LogFile 类提供基本的文件日志服务
 */
class LogFileWriter extends LogWriterAbstract {
	
	/**
     * 保存运行期间的日志
     *
     * @var string
     */
    private $_log = '';

    /**
     * 日期格式
     *
     * @var string
     */
    private $dateFormat = 'Y-m-d H:i:s';

    /**
     * 保存日志文件的目录
     *
     * @var string
     */
    private $_logFileDir;

    /**
     * 保存日志的文件名
     *
     * @var string
     */
    private $_logFilename;

    /**
     * 要写入日志文件的错误级别
     *
     * @var array
     */
    private $_errorLevel;
    
    /**
     * 构造函数
     */
    function __construct()
    {
    	$this->_enabled = App::ini('_log_enabled',true);
    	if (!$this->_enabled) return;
    	
        $dir = App::ini('_log_file_dir',null);
        if (empty($dir)){
        	$this->_enabled = false;
        	return;
        }
        
        $dir = realpath($dir);
        if (substr($dir, -1) != DS) {
            $dir .= DS;
        }
        if (!is_dir($dir) || !is_writable($dir)) {
            $this->_enabled = false;
        } else {
            $this->_logFileDir = $dir;
            $this->_logFilename = $this->_logFileDir . App::ini('_log_filename','access.log');
            $errorLevel = explode(',', strtolower(App::ini('_log_levels','notice, debug, warning, error, exception, info')));
            $errorLevel = array_map('trim', $errorLevel);
            $errorLevel = array_filter($errorLevel, 'trim');
            $this->_errorLevel = array();
            foreach ($errorLevel as $e) {
               $this->_errorLevel[$e] = true;
            }
			
            global $___sfw_loaded_time;
            $sec = (int) $___sfw_loaded_time;
        	$usec = $___sfw_loaded_time - $sec;
            
            $this->_startTag = sprintf("[%s %s] ======= SFW Loaded =======\n",
                date($this->dateFormat, $sec), $usec);

            if (isset($_SERVER['REQUEST_URI'])) {
                $this->_startTag .= sprintf("[%s] REQUEST_URI: %s\n",
                        date($this->dateFormat),
                        $_SERVER['REQUEST_URI']);
            }
			
            // 注册脚本结束时要运行的方法,将缓存的日志内容写入文件
            ShutdownCallback::getInstance()->add(array($this, '__writeLog'));
            
            // 检查文件是否已经超过指定大小
            if (file_exists($this->_logFilename)) {
                $filesize = filesize($this->_logFilename);
            } else {
                $filesize = 0;
            }
            $maxsize = (int)App::ini('_log_file_maxsize',512);
            if ($maxsize >= 512) {
                $maxsize = $maxsize * 1024;
                if ($filesize >= $maxsize) {
                    // 使用新的日志文件名
                    $pathinfo = pathinfo($this->_logFilename);
                    $newFilename = $pathinfo['dirname'] . DS .
                        basename($pathinfo['basename'], '.' . $pathinfo['extension']) .
                        date('-Ymd-His') . '.' . $pathinfo['extension'];
                    rename($this->_logFilename, $newFilename);
                }
            }
        }
    }

    /**
     * 追加日志信息
     *
     * @param string $msg
     * @param string $title
     * @param string $level
     */
    function append($msg, $title = '', $level = 'info')
    {
    	if ($this->_enabled && isset($this->_errorLevel[strtolower($level)])){
        	$this->_log .= sprintf("[%s] [%s] %s:%s\n", date($this->dateFormat), $level, $title, print_r($msg, true));
    	}
    }
	
	/**
     * 将缓存的日志信息写入实际存储,并清空缓存
     * 此方法由系统自动调用
     * 
     */
    function __writeLog(){	
    	
		if (!$this->_enabled) return;
		
		if (empty($this->_log)) return;
		
		global $___sfw_loaded_time;
        
		$shutdown_time = microtime(true);
        $sec = (int) $shutdown_time;
        $usec = $shutdown_time - $sec;
        
        $elapsedTime = $shutdown_time - $___sfw_loaded_time;
        
        $content = $this->_startTag . $this->_log . sprintf("[%s %s] ======= SFW End (elapsed: %f seconds) =======\n\n",date($this->dateFormat, $sec), $usec, $elapsedTime);

        $fp = fopen($this->_logFilename, 'a');
        if (!$fp) { return; }
        flock($fp, LOCK_EX);
        fwrite($fp, str_replace("\r", '', $content));
        flock($fp, LOCK_UN);
        fclose($fp);
    }
}

 

<?php
/**
 * 应用程序基本启动文件,提供应用程序运行的关键设置信息
 */
$root_dir = dirname(dirname(dirname(__FILE__)));

/**
 * 如果要集成第三方的 PHP 库,错误报告也许要修改为:
 *
 * error_reporting(E_ALL & ~(E_STRICT | E_NOTICE));
 */
error_reporting(E_ALL | E_STRICT);

$config = array(

    /**
     * 应用程序的 ID,用于唯一标识一个应用程序
     */
    'APPID'                 => 'todo',
	
	/**
     * 应用程序根目录
     */
    'ROOT_DIR'              => $root_dir,

    /**
     * 主程序所在目录
     */
    'APP_DIR'               => "{$root_dir}/_code/app",

	/**
	 * 辅助库目录
	 */
	'LIBRARY_DIR'            => "{$root_dir}/_code/lib",	

    /**
     * 配置文件所在目录
     */
    'CONFIG_DIR'            => "{$root_dir}/_code/config",

    /**
     * 临时(缓存)文件所在目录
     */
    'TMP_DIR'            => "{$root_dir}/_code/tmp",

    /**
     * 日志文件所在目录
     */
    'LOG_DIR'            => "{$root_dir}/_code/log",

    /**
     * 所有扩展模块所在的目录
     */
    'MODULE_DIR'            => "{$root_dir}/_code/app/modules",
	
);

// 设置类文件基本路径
//set_include_path('.' . PATH_SEPARATOR . $config['LIBRARY_DIR'] . PATH_SEPARATOR . get_include_path());

require_once("{$config['LIBRARY_DIR']}/app.php");

App::replaceIni($config);

// 加载不同命名空间下的配置文件 {namespace}.sys.php,{namespace}.route.php

$configForNamepace = array(
	
	/**
	 * 国际化和本地化
	 */	
	'_i18n_multi_languages' => false,// 指示是否启用多语言支持	
	'_l10n_default_timezone' => 'Asia/Shanghai',// 默认的时区设置


	/**
	 * 日志设置
	 */
	'_log_enabled' => true ,
	'_log_writer' => 'LogFileWriter' , //LogFirePHPWriter
	'_log_file_dir' => App::ini('LOG_DIR'),
	'_log_filename' => "{$namespace}-access.log" ,
	'_log_file_maxsize' => 512 ,
	'_log_levels' => 'notice, debug, warning, error, exception, info' ,
	
	/**
	 * 数据库链接DSN设置
	 */	
	'_db_default_dsn' => array(
	    'driver'   => 'mysqlt',
	    'host'     => 'localhost',
	    'login'    => 'root',
	    'password' => 'root',
	    'database' => 'smallcms',
	    'charset'  => 'utf8',
	),	

); 

App::replaceIni($configForNamepace);

/**
 * 初始化应用程序
 */

// 设置默认的时区
date_default_timezone_set(App::ini('_i10n_default_timezone','Asia/Shanghai'));

// 设置应用的日志记录服务
require_once("{$config['LIBRARY_DIR']}/log.php");
App::$_log_ = App::singleton(App::ini('_log_writer'));

// 设置应用的缓存服务

// 加载数据库操作组件
require_once("{$config['LIBRARY_DIR']}/db.php");

// 设置分发器对象,并进行请求分发
require_once("{$config['LIBRARY_DIR']}/web.php");
$wd = WebDispatcher::getInstance();
$wd->setNamespace($namespace);

// 设置过滤器
//$wd->addBeforeFilter(new RequestAuthFilter());


$response = $wd->dispatching();
if ($response) echo $response;
 
分享到:
评论
11 楼 vb2005xu 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) =======



10 楼 vb2005xu 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);
		}
	}
}


9 楼 vb2005xu 2011-05-30  
上面的代码经过测试有问题,不能自定义日志级别的输出 汗死 犯了很低级的错误 ,在 append 方法里面

原来是 校验 两个条件
$this->_enabled && isset($this->_errorLevel[$level])


更正后
$this->_enabled && isset($this->_errorLevel[$level]) && $this->_errorLevel[$level]


应该加上 $this->_errorLevel[$level] 这个值的校验,缺省就是false 所以 isset肯定是true ,....

....
8 楼 抢街饭 2011-05-11  
PHP 恶心的变量名字 看的我头疼
7 楼 crskyp 2011-05-05  
1927105 写道
LZ邪恶的变量命名

支持楼主  
6 楼 1927105 2011-05-05  
LZ邪恶的变量命名
5 楼 Tank03 2011-05-05  
嘿~~排版不错。下啦。
4 楼 vb2005xu 2011-05-03  
发了 在 sfw最初版本里面 那个页面上 你看看 应该在首页上
3 楼 VE星辰 2011-05-03  
建议顺便发个demo给我们下载,嘿嘿
2 楼 vb2005xu 2011-04-17  
更新后的版本:
<?php
/**
 * 日志系统抽象类
 *
 */
abstract class LogWriterAbstract {
		
    /**
     * 是否启用日志记录
     *
     * @var boolean
     */
    protected $_enabled = true;
    
    /**
     * 日志记录的错误级别
     *
     * @var array
     */
    protected $_errorLevel = array(
    	'notice' => false,
    	'debug' => false,
    	'warning' => false,
    	'error' => false,
    	'exception' => false,
    	'info' => false,
    ); 
    
    abstract function append($msg, $title = '', $level = 'info');
}

/**
 * LogFirePHPWriter 类提供对 FirePHP 的支持
 */
class LogFirePHPWriter extends LogWriterAbstract {
	        
    function __construct(){
    	$this->_enabled = App::ini('_log/enabled',true);
    	if (!$this->_enabled) return;
    	
    	$errorLevel = normalize(strtolower(App::ini('_log/levels','exception,error')),',');
            
        if (!empty($errorLevel))
            foreach ($errorLevel as $e)
               $this->_errorLevel[$e] = true;
    }
    
    function append($msg, $title = '', $level = 'info'){
    	$level = strtolower(trim($level));
    	if ($this->_enabled && isset($this->_errorLevel[$level])){
    		switch($level){
    			case 'info':
    			case 'debug':
    				FirePhpHelper::getInstance()->info($msg, $title);
    				break;
    			case 'notice':
    				FirePhpHelper::getInstance()->log($msg, $title);
    				break;
    			case 'exception':
    				FirePhpHelper::getInstance()->warn($msg, $title);
    				break;
    			case 'error':
    				FirePhpHelper::getInstance()->error($msg, $title);
    				break;
    		}
    	}
    }
}

/**
 * LogFile 类提供基本的文件日志服务
 */
class LogFileWriter extends LogWriterAbstract {
	
	/**
     * 保存运行期间的日志
     *
     * @var string
     */
    private $_log = '';

    /**
     * 日期格式
     *
     * @var string
     */
    private $dateFormat = 'Y-m-d H:i:s';

    /**
     * 保存日志文件的目录
     *
     * @var string
     */
    private $_logFileDir;

    /**
     * 保存日志的文件名
     *
     * @var string
     */
    private $_logFilename;
    
    /**
     * 构造函数
     */
    function __construct()
    {
    	$this->_enabled = App::ini('_log/enabled',true);
    	if (!$this->_enabled) return;
    	
        $dir = App::ini('_log/file_dir',null);
        if (empty($dir)){
        	$this->_enabled = false;
        	return;
        }
        
        $dir = realpath($dir);
        if (substr($dir, -1) != DS) {
            $dir .= DS;
        }
        if (!is_dir($dir) || !is_writable($dir)) {
            $this->_enabled = false;
        } else {
            $this->_logFileDir = $dir;
            $this->_logFilename = $this->_logFileDir . App::ini('_log/filename','access.log');
            $errorLevel = normalize(strtolower(App::ini('_log/levels','exception,error')),',');
            
            if (!empty($errorLevel)){
	            foreach ($errorLevel as $e){
	            	if (isset($this->_errorLevel[$e]))
	            		$this->_errorLevel[$e] = true;
	            }
            }
            
            global $___sfw_loaded_time;
            $sec = (int) $___sfw_loaded_time;
        	$usec = $___sfw_loaded_time - $sec;
            
            $this->_startTag = sprintf("[%s %s] ======= SFW Loaded =======\n",
                date($this->dateFormat, $sec), $usec);

            if (isset($_SERVER['REQUEST_URI'])) {
                $this->_startTag .= sprintf("[%s] REQUEST_URI: %s\n",
                        date($this->dateFormat),
                        $_SERVER['REQUEST_URI']);
            }
			
            // 注册脚本结束时要运行的方法,将缓存的日志内容写入文件
            ShutdownCallback::getInstance()->add(array($this, '__writeLog'));
            
            // 检查文件是否已经超过指定大小
            if (file_exists($this->_logFilename)) {
                $filesize = filesize($this->_logFilename);
            } else {
                $filesize = 0;
            }
            $maxsize = (int)App::ini('_log/file_maxsize',512);
            if ($maxsize >= 512) {
                $maxsize = $maxsize * 1024;
                if ($filesize >= $maxsize) {
                    // 使用新的日志文件名
                    $pathinfo = pathinfo($this->_logFilename);
                    $newFilename = $pathinfo['dirname'] . DS .
                        basename($pathinfo['basename'], '.' . $pathinfo['extension']) .
                        date('-Ymd-His') . '.' . $pathinfo['extension'];
                    rename($this->_logFilename, $newFilename);
                }
            }
        }
    }

    /**
     * 追加日志信息
     *
     * @param string $msg
     * @param string $title
     * @param string $level
     */
    function append($msg, $title = '', $level = 'info')
    {
    	if ($this->_enabled && isset($this->_errorLevel[strtolower($level)])){
        	$this->_log .= sprintf("[%s] [%s] %s:%s\n", date($this->dateFormat), $level, $title, print_r($msg, true));
    	}
    }
	
	/**
     * 将缓存的日志信息写入实际存储,并清空缓存
     * 此方法由系统自动调用
     * 
     */
    function __writeLog(){	
    	
		if (!$this->_enabled) return;
		
		if (empty($this->_log)) return;
		
		global $___sfw_loaded_time;
        
		$shutdown_time = microtime(true);
        $sec = (int) $shutdown_time;
        $usec = $shutdown_time - $sec;
        
        $elapsedTime = $shutdown_time - $___sfw_loaded_time;
        
        $content = $this->_startTag . $this->_log . sprintf("[%s %s] ======= SFW End (elapsed: %f seconds) =======\n\n",date($this->dateFormat, $sec), $usec, $elapsedTime);

        $fp = fopen($this->_logFilename, 'a');
        if (!$fp) { return; }
        flock($fp, LOCK_EX);
        fwrite($fp, str_replace("\r", '', $content));
        flock($fp, LOCK_UN);
        fclose($fp);
    }
}
1 楼 vb2005xu 2011-04-15  
/**
 * 缓存组件抽象类
 * 
 */
abstract class CacheWriterAbstract {
	
	/**
	 * 写入缓存
	 *
	 * @param string $id
	 * @param mixed $data
	 * @param array $policy
	 */
	abstract function set($id, $data, array $policy = null);
	
	/**
	 * 读取缓存,失败或缓存撒失效时返回 false
	 *
	 * @param string $id
	 * @param array $policy
	 *
	 * @return mixed
	 */
	abstract function get($id, array $policy = null);
	
	/**
	 * 删除指定的缓存
	 *
	 * @param string $id
	 * @param array $policy
	 */
	abstract function remove($id, array $policy = null);
}

相关推荐

Global site tag (gtag.js) - Google Analytics