用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3369|回复: 2
收起左侧

[库 Library] 提供一个优化了的日志类

[复制链接]
发表于 2012-8-28 10:08:15 | 显示全部楼层 |阅读模式
在执行析构函数的时候才会将日志信息写入文件,只需要一次IO即可,如果日志记录的多,那么效果会比较明显了
如果有问题,还希望大家指正

PHP复制代码
 
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Logging Class
 *
 * @package             CodeIgniter
 * @subpackage  Libraries
 * @category    Logging
 * @author              Zhang Hao
 */

class CI_Log {
 
        protected $_message             = '';
        protected $_log_path    = '';
        protected $_threshold   = 1;
        protected $_date_fmt    = 'Y-m-d H:i:s';
        protected $_enabled             = TRUE;
        protected $_levels              = array('ERROR' => '1', 'DEBUG' => '2',  'INFO' => '3', 'ALL' => '4');
 
        /**
         * Constructor
         */

        public function __construct() {
                $config =& get_config();
 
                //析构函数中运行在apache时,当前工作目录会变为apache的,所以用绝对路径
                $this->_log_path = ($config['log_path'] != '') ? $config['log_path'] : FCPATH.APPPATH.'logs/';
 
                if ( ! is_dir($this->_log_path) OR ! is_really_writable($this->_log_path)) {
                        $this->_enabled = FALSE;
                }
 
                if (is_numeric($config['log_threshold'])) {
                        $this->_threshold = $config['log_threshold'];
                }
 
                if ($config['log_date_format'] != '') {
                        $this->_date_fmt = $config['log_date_format'];
                }
        }
 
        /**
         * @deprecated 释放实例化时一次性写入,减少IO,提高效率
         */

        public function __destruct() {
                if($this->_message != '') {
                        file_put_contents($this->_log_path.'log-'.date('Y-m-d').'.php', $this->_message, FILE_APPEND);
                }
        }
 
        /**
         * @deprecated 追加日志
         */

        public function write_log($level = 'error', $msg) {
                $level = strtoupper($level);
 
                if ($this->_enabled === FALSE) {
                        return FALSE;
                }
 
                if ( !isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold)) {
                        return FALSE;
                }
 
                $this->_message .= $level.' - '.date($this->_date_fmt). ' --> '.$msg."\n";
        }
}
 
复制代码

Log.rar

979 Bytes, 下载次数: 27

类文件

发表于 2012-10-18 22:12:33 | 显示全部楼层
日志类还没有研究透,还不能发表有建设意义的意见
发表于 2012-10-19 13:12:51 | 显示全部楼层
思路有点同kohana_log

本版积分规则