入住 CI 中国社区 登录
CodeIgniter 中国开发者社区 返回首页

bomb_2002的个人空间 https://codeigniter.org.cn/forums/?25512 [收藏] [复制] [分享] [RSS]

日志

CI中使用log4php调试程序

已有 2264 次阅读2013-6-6 14:48 | log4php, 调试

下载log4php。我下载的版本是:apache-log4php-2.3.0-src.zip。借压缩,将压缩文件中的src/main/php/文 件夹拷贝到CI的application/thrid_party/目录中,并将此文件夹(php),改名为log4php。
在log4php文件夹中建立log4php的配置文件,文件名为:log4php.properties。此配置文件内容如下:
[code=TEXT]
log4php.rootLogger=DEBUG, A1

#输出到页面
log4php.appender.A1 = LoggerAppenderEcho
log4php.appender.A1.layout = LoggerLayoutHtml

#输出到文件
log4php.appender.A2 = LoggerAppenderDailyFile
log4php.appender.A2.layout = LoggerLayoutPattern
log4php.appender.A2.layout.ConversionPattern = "%d{ISO8601} [%p] %c: %m (at %F line %L)%n"
log4php.appender.A2.datePattern = Ymd
log4php.appender.A2.file = logs/errorLog_%s.log
[/code]
log4php的信息会显示在页面上。
打开根目录下的index.php文件,在文件中添加入下代码:
[code=PHP]
// 载入Log4php
define('LOG4PHP_DIR', APPPATH.'third_party/log4php/');
require_once LOG4PHP_DIR.'Logger.php';
Logger::configure(LOG4PHP_DIR.'log4php.properties');

/*
 * --------------------------------------------------------------------
 * LOAD THE BOOTSTRAP FILE
 * --------------------------------------------------------------------
 *
 * And away we go...
 *
 */
require_once BASEPATH.'core/CodeIgniter.php';
[/code]
在需要调试的文件中,如/application/core/MY_Router.php文件中:
[code=PHP]
class MY_Router extends CI_Router {
    // 定义私有日志变量log
    private $log;
   
    /**
     * Constructor
     * Runs the route mapping function.
     */
    public function __construct() {
        parent::__construct();
        // 生成log
        $this->log = Logger::getLogger(__CLASS__);
        // 使用log
        $this->log->debug('core/MY_Router Class Initialized');
    }
   
    /**
     * Validates the supplied segments.  Attempts to determine the path to the controller.
     *
     * @see CI_Router::_validate_request()
     * @access    private
     * @param    array
     * @return    array
     */
    function _validate_request($segments) {
        if (count($segments) == 0) {
            return $segments;
        }
       
        // 测试:查看路径
        $this->log->debug('segments数组的大小:'.count($segments));
        $tempDir = array();
        for ($i = 0; $i < count($segments); $i++) {
            $tempDir[] = $segments[$i];
            $this->log->debug('segments['.$i.']:'.$segments[$i]);
            $this->log->debug(APPPATH.'controllers/'.implode('/', $tempDir));
            if(!is_dir(APPPATH.'controllers/'.implode('/', $tempDir))) {
                unset($tempDir[count($tempDir)-1]);
                break;
            }
        }
        $this->log->debug('segments = '.$segments[0]);

        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.$segments[0].'.php')) {
            return $segments;
        }

        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0])) {
            // Set the directory and remove it from the segment array
            //$this->set_directory($segments[0]);
            //$segments = array_slice($segments, 1);
           
            $controllerPath = array();
            $is = 0;
            for(; $is < count($segments); $is++) {
                $controllerPath[] = $segments[$is];
                if(!is_dir(APPPATH.'controllers/'.implode('/', $controllerPath))) {
                    unset($controllerPath[count($controllerPath)-1]);
                    break;
                }
            }
            $this->log->debug('传递给set_directory的参数:'.implode('/', $controllerPath));
            $this->set_directory(implode('/', $controllerPath));
            $segments = array_slice($segments, $is);
$this->log->debug($segments);
            if (count($segments) > 0) {
$this->log->debug(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT);
                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT)) {
$this->log->debug($this->routes['404_override']);
                    if ( ! empty($this->routes['404_override'])) {
                        $x = explode('/', $this->routes['404_override']);
$this->log->debug('x='.$x);
                        $this->set_directory('');
                        $this->set_class($x[0]);
                        $this->set_method(isset($x[1]) ? $x[1] : 'index');

                        return $x;
                    }
                    else {
                        show_404($this->fetch_directory().$segments[0]);
                    }
                }
            }
            else {
                // Is the method being specified in the route?
                if (strpos($this->default_controller, '/') !== FALSE) {
                    $x = explode('/', $this->default_controller);

                    $this->set_class($x[0]);
                    $this->set_method($x[1]);
                }
                else {
                    $this->set_class($this->default_controller);
                    $this->set_method('index');
                }

                // Does the default controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php')) {
                    $this->directory = '';
                    return array();
                }

            }

            return $segments;
        }


        // If we've gotten this far it means that the URI does not correlate to a valid
        // controller class.  We will now see if there is an override
        if ( ! empty($this->routes['404_override'])) {
            $x = explode('/', $this->routes['404_override']);

            $this->set_class($x[0]);
            $this->set_method(isset($x[1]) ? $x[1] : 'index');

            return $x;
        }


        // Nothing else to do at this point but show a 404
        show_404($segments[0]);
    }
   
    /**
     *  Set the directory name
     *
     * @access    public
     * @param    string
     * @return    void
     */
    function set_directory($dir) {
        // 原程序将$dir中的"/"或"."过滤掉,出于安全考虑吗?
        //$this->directory = str_replace(array('/', '.'), '', $dir).'/';
        $this->directory = $dir.'/';
    }
}
/* End of file MY_Routes.php */
/* Location: ./application/liberaies/MY_Routes.php */
[/code]

运行后,显示如下的信息:


路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 入住 CI 中国社区